diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/sibs/Functional.hpp | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/include/sibs/Functional.hpp b/include/sibs/Functional.hpp new file mode 100644 index 0000000..53d8ff0 --- /dev/null +++ b/include/sibs/Functional.hpp @@ -0,0 +1,113 @@ +#pragma once + +#include <vector> + +namespace sibs +{ + template <typename T> + class Function; + + template <typename T> + class FunctionIterator + { + public: + FunctionIterator(T *_it, Function<T> *_func, std::size_t _funcIndex) : + it(_it), + func(_func), + funcIndex(_funcIndex) + { + + } + + bool operator == (FunctionIterator<T> &rhs) const + { + return it == rhs.it && funcIndex == rhs.funcIndex; + } + + bool operator != (FunctionIterator<T> &rhs) const + { + return !(*this == rhs); + } + + T& operator * () + { + return *it; + } + + void operator ++ () + { + ++it; + if(it == func->endIt) + { + if(funcIndex < func->connections.size()) + { + it = func->connections[funcIndex].beginIt; + ++funcIndex; + } + } + } + private: + T *it; + Function<T> *func; + std::size_t funcIndex; + }; + + template <typename T> + class Function + { + friend class FunctionIterator<T>; + public: + typedef FunctionIterator<T> iterator; + typedef const FunctionIterator<T> const_iterator; + + Function(T *begin, T *end) : + beginIt(begin), + endIt(end) + { + + } + + Function(std::vector<T> &vec) : + beginIt(vec.data()), + endIt(vec.data() + vec.size()) + { + + } + + Function& merge(Function &otherFunc) + { + connections.push_back(otherFunc); + return *this; + } + + iterator begin() + { + return FunctionIterator<T>(beginIt, this, 0); + } + + iterator end() + { + T *it = endIt; + if(!connections.empty()) + it = connections.back().endIt; + return FunctionIterator<T>(it, this, connections.size()); + } + + const_iterator begin() const + { + return FunctionIterator<T>(beginIt, this, 0); + } + + const_iterator end() const + { + T *it = endIt; + if(!connections.empty()) + it = connections.back().endIt; + return FunctionIterator<T>(it, this, connections.size()); + } + private: + T *beginIt; + T *endIt; + std::vector<Function> connections; + }; +} |