mirror of
https://github.com/boostorg/callable_traits.git
synced 2026-02-09 11:02:29 +00:00
adding interface example
fixing build errors adding Boost to travis.yml fixing travis.yml fixing travis.yml fixing travis.yml, fixing for libstdc++ bug fixing travis.yml fixing travis.yml adding is_like_function fixing interface example, doc gen removing rogue tab characters fixing build error adding FunctionTypes section in documentation removing empty note fixing misspelling of inheritance
This commit is contained in:
95
example/interface_example.cpp
Normal file
95
example/interface_example.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/*<-
|
||||
(C) Copyright Tobias Schwinger
|
||||
(C) Copyright 2016 (Modified Work) Barrett Adair
|
||||
|
||||
Use modification and distribution are subject to the boost Software License,
|
||||
Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
-----------------------------------------------------------------------------
|
||||
See interface.hpp in this directory for details.
|
||||
->*/
|
||||
|
||||
#include <callable_traits/config.hpp>
|
||||
#ifdef CALLABLE_TRAITS_MSVC
|
||||
int main(){ return 0; }
|
||||
#elif defined CALLABLE_TRAITS_GCC_OLDER_THAN_4_9_2
|
||||
int main(){ return 0; }
|
||||
#else
|
||||
|
||||
//[ interface_example
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "interface.hpp"
|
||||
|
||||
DEFINE_INTERFACE(interface_x,
|
||||
(( a_func, void(int) const ))
|
||||
(( a_func, void(long) const ))
|
||||
(( another_func, int() ))
|
||||
(( some_data, const char* ))
|
||||
);
|
||||
|
||||
// two classes that implement interface_x
|
||||
struct a_class {
|
||||
void a_func(int v) const {
|
||||
std::cout << "a_class::void a_func(int v = " << v << ")" << std::endl;
|
||||
}
|
||||
|
||||
void a_func(long v) const {
|
||||
std::cout << "a_class::void a_func(long v = " << v << ")" << std::endl;
|
||||
}
|
||||
|
||||
int another_func() {
|
||||
std::cout << "a_class::another_func() = 3" << std::endl;
|
||||
return 3;
|
||||
}
|
||||
|
||||
const char* some_data = "a_class's data";
|
||||
};
|
||||
|
||||
struct another_class {
|
||||
|
||||
// Notice a_func is implemented as a function template? No problem for our interface.
|
||||
template<typename T>
|
||||
void a_func(T v) const {
|
||||
std::cout <<
|
||||
"another_class::void a_func(T v = " << v << ")"
|
||||
" [ T = " << typeid(T).name() << " ]" << std::endl;
|
||||
}
|
||||
|
||||
int another_func() {
|
||||
std::cout << "another_class::another_func() = 5" << std::endl;
|
||||
return 5;
|
||||
}
|
||||
|
||||
const char* some_data = "another_class's data";
|
||||
};
|
||||
|
||||
void print_data(interface_x obj) {
|
||||
std::cout << obj.some_data() << std::endl;
|
||||
}
|
||||
|
||||
// Both classes above can be assigned to the interface variable and their
|
||||
// member functions can be called through it.
|
||||
int main() {
|
||||
a_class x;
|
||||
another_class y;
|
||||
|
||||
interface_x i(x);
|
||||
i.a_func(12);
|
||||
i.a_func(77L);
|
||||
i.another_func();
|
||||
|
||||
print_data(i);
|
||||
i.some_data() = "x's data has changed.";
|
||||
print_data(x);
|
||||
|
||||
//reusing the same interface object
|
||||
i = y;
|
||||
i.a_func(13);
|
||||
i.a_func(21L);
|
||||
i.another_func();
|
||||
print_data(y);
|
||||
}
|
||||
//]
|
||||
#endif
|
||||
Reference in New Issue
Block a user