Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <functional>
- struct custom
- {
- custom(const std::string& str = "bar")
- : _str(str)
- {
- std::cout << "constructor default custom" << std::endl;
- }
- custom(const custom& other)
- : _str(other._str)
- {
- std::cout << "constructor copy custom" << std::endl;
- }
- custom(custom&& other)
- : _str(std::move(other._str))
- {
- std::cout << "constructor move custom" << std::endl;
- }
- ~custom() {
- std::cout << "destructor custom" << std::endl;
- }
- void swap(custom& other)
- {
- std::cout << "swap custom" << std::endl;
- using std::swap;
- std::swap(_str, other._str);
- }
- custom& operator=(const custom& other)
- {
- std::cout << "operator copy custom" << std::endl;
- custom(other).swap(*this);
- return *this;
- }
- custom& operator=(custom&& other)
- {
- std::cout << "operator move custom" << std::endl;
- custom(std::move(other)).swap(*this);
- return *this;
- }
- std::string str() const {return _str;}
- public:
- std::string _str;
- };
- template <int...>
- struct int_sequence
- {
- };
- template <int N, int... Is>
- struct make_int_sequence : make_int_sequence<N - 1, N - 1, Is...>
- {
- };
- template <int... Is>
- struct make_int_sequence<0, Is...> : int_sequence<Is...>
- {
- };
- template <int>
- struct placeholder_template
- {
- };
- namespace std
- {
- template <int N>
- struct is_placeholder<placeholder_template<N>> : integral_constant<int, N + 1>
- {
- };
- }
- template <typename... Args>
- class method
- {
- public:
- using return_type = void;
- using function = std::function<return_type(const Args&...)>;
- template <typename FUNCTION>
- method(FUNCTION&& m)
- : _method(std::forward<FUNCTION>(m))
- {
- }
- template <typename T, typename ... PARMS>
- method(T* obj, return_type (T::*ptr_func)(PARMS...))
- : method(obj, ptr_func, make_int_sequence<sizeof...(PARMS)>{})
- {
- }
- template <typename T, typename ... PARMS, int... Is>
- method(T* obj, return_type (T::*ptr_func)(PARMS...), int_sequence<Is...>)
- : method(std::bind(ptr_func, obj, placeholder_template<Is>{}...))
- {
- }
- method(const method& other) = delete;
- method& operator=(const method& other) = delete;
- ~method() { ; }
- template <typename ... PARMS>
- return_type operator()(PARMS&&... data) const
- {
- _method(std::forward<PARMS>(data)...);
- }
- protected:
- function _method;
- };
- method<custom> m( [](const custom& parm){std::cout << "parm lambda: " << parm.str() << "." << std::endl;} );
- struct my_method
- {
- void call(const custom& parm)
- {
- std::cout << "parm functor: " << parm.str() << "." << std::endl;
- }
- };
- my_method mm;
- method<custom> m2(&mm, &my_method::call);
- template <typename T>
- void pr(T&& parm)
- {
- // lambda currified
- m(std::forward<T>(parm));
- // functor
- m2(std::forward<T>(parm));
- }
- template <typename T, typename ... Args>
- void pr(T&& parm, Args&&... data)
- {
- pr(std::forward<T>(parm));
- pr(std::forward<Args>(data)...);
- }
- template <typename ... Args>
- struct bar
- {
- template <typename ... PARMS>
- void call(PARMS&&... data) const
- {
- pr(std::forward<PARMS>(data)...);
- std::cout << std::endl;
- }
- };
- template <typename ... Args>
- struct foo
- {
- template <typename ... PARMS>
- void call(PARMS&&... data) const
- {
- b.call(std::forward<PARMS>(data)...);
- }
- bar<Args...> b;
- };
- int main()
- {
- foo<custom, custom, custom> f;
- custom a("a");
- custom b("b");
- custom c("c");
- f.call(a, b, c);
- std::cout << "---------------" << std::endl;
- foo<custom, custom, custom> f2;
- f2.call(custom(), custom(), custom());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement