Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // tree.h
- #ifndef TREE_H
- #define TREE_H
- #include <map>
- #include <vector>
- #include <string>
- class First;
- class Tree{
- public:
- Tree() {}
- ~Tree() {}
- template<class TF> // where TF is a ConcreteFirst
- void addFirstToTree();
- private:
- std::map<std::string, First *> firstCollection; // <- "First"'s here
- };
- #include "tree.tpp"
- #endif // TREE_H
- // tree.tpp
- #ifndef TPP_TREE
- #define TPP_TREE
- #include "tree.h"
- template <class TF> // where TF is a ConcreteFirst
- void Tree::addFirstToTree(){
- this->firstCollection[TF::name] = new TF(this);
- }
- #endif // TPP_TREES
- // first.h
- #ifndef FIRST_H
- #define FIRST_H
- class Tree;
- class First{
- public:
- static const std::string name;
- First(const Tree *baseTree) : myTree(baseTree) {}
- virtual ~First();
- protected:
- const Tree *myTree;
- };
- template <typename Type> class TypedFirst : public First{
- public:
- static const std::string name;
- TypedFirst(const Tree *baseTree) : First(baseTree) {}
- Type &value();
- private:
- Type _value;
- };
- #endif // FIRST_H
- // first.tpp
- #ifndef TPP_FIRST
- #define TPP_FIRST
- #include "first.h"
- template <typename Type>
- const std::string TypedFirst<Type>::name = "default typed";
- template <typename Type>
- Type& TypedFirst::value() {return this->_value;}
- #endif // TPP_FIRST
- // first.cpp
- #include "first.h"
- First::~First() {}
- const std::string First::name = "default";
- // concretefirsta.h
- #ifndef CONCRETEFIRSTA_H
- #define CONCRETEFIRSTA_H
- #include "first.h"
- class ConcreteFirstA : public TypedFirst<int>{
- public:
- static const std::string name;
- ConcreteFirstA(const Tree *baseTree);
- virtual ~ConcreteFirstA();
- };
- #endif // CONCRETEFIRSTA_H
- // concretefirsta.cpp
- #include "concretefirsta.h"
- const std::string ConcreteFirstA::name = "firstA";
- ConcreteFirstA::ConcreteFirstA(const Tree* baseTree) : TypedFirst<int>(baseTree) {}
- ConcreteFirstA::~ConcreteFirstA() {}
- // main.cpp
- #include "tree.h"
- #include "first.h"
- #include "concretefirsta.h"
- using namespace std;
- int main()
- {
- Tree *myTree = new Tree();
- myTree->addFirstToTree<ConcreteFirstA>();
- delete myTree;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement