Created
October 13, 2010 21:03
-
-
Save brycelelbach/624913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template<typename Data, typename Function> | |
| struct function_object_adaptor { | |
| public: | |
| typedef typename Function::result_type value_type; | |
| typedef value_type& reference; | |
| typedef value_type* pointer; | |
| private: | |
| Data data; | |
| Function f; | |
| // shared Assignable implementation | |
| void copy (function_object_adaptor const& x) { | |
| data = x.data; | |
| f = f.data; | |
| } | |
| public: | |
| // STL DefaultConstructible | |
| function_object_adaptor (Data d_ = Data(), Function f_ = Function()): | |
| data(d_), f(f_) | |
| { | |
| f(data); // call f on data | |
| } | |
| // STL Assignable | |
| function_object_adaptor (function_object_adaptor const& x) { | |
| copy(x); | |
| } | |
| // STL EqualityComparable | |
| bool operator== (function_object_adaptor const& rhs) { | |
| return (data == rhs.data) | |
| && (f == rhs.f); | |
| } | |
| // STL EqualityComparable | |
| bool operator!= (function_object_adaptor const& rhs) { | |
| return (data != rhs.data) | |
| && (f != rhs.f); | |
| } | |
| // STL Assignable | |
| function_object_adaptor& operator= (function_object_adaptor const& x) { | |
| if (*this != x) copy(x); | |
| return *this; | |
| } | |
| // STL TrivialIterator | |
| reference operator* (void) const { | |
| return f.get(); | |
| } | |
| // STL TrivialIterator | |
| pointer operator-> (void) const { | |
| return f.ptr(); | |
| } | |
| // STL ForwardIterator | |
| function_object_adaptor& operator++ (void) { | |
| f(++data); return *this; | |
| } | |
| // STL ForwardIterator | |
| function_object_adaptor operator++ (int) { | |
| function_object_adaptor tmp = *this; | |
| return ++tmp; | |
| } | |
| bool at (Data const& d_) const { | |
| return data == d_; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment