Advertisement
Guest User

Untitled

a guest
Sep 26th, 2014
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // tree.h
  2. #ifndef TREE_H
  3. #define TREE_H
  4.  
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8.  
  9. class First;
  10. class Tree{
  11.   public:
  12.     Tree() {}
  13.     ~Tree() {}
  14.     template<class TF> // where TF is a ConcreteFirst
  15.     void addFirstToTree();
  16.   private:
  17.     std::map<std::string, First *> firstCollection; // <- "First"'s here
  18. };
  19. #include "tree.tpp"
  20.  
  21. #endif // TREE_H
  22.  
  23. // tree.tpp
  24. #ifndef TPP_TREE
  25. #define TPP_TREE
  26.  
  27. #include "tree.h"
  28.  
  29. template <class TF> // where TF is a ConcreteFirst
  30. void Tree::addFirstToTree(){
  31.   this->firstCollection[TF::name] = new TF(this);
  32. }
  33.  
  34.  
  35. #endif // TPP_TREES
  36.  
  37. // first.h
  38. #ifndef FIRST_H
  39. #define FIRST_H
  40.  
  41. class Tree;
  42. class First{
  43.   public:
  44.     static const std::string name;
  45.     First(const Tree *baseTree) : myTree(baseTree) {}
  46.     virtual ~First();
  47.   protected:
  48.     const Tree *myTree;
  49. };
  50.  
  51. template <typename Type> class TypedFirst : public First{
  52.   public:
  53.     static const std::string name;
  54.     TypedFirst(const Tree *baseTree) : First(baseTree) {}
  55.     Type &value();
  56.   private:
  57.     Type _value;
  58. };
  59.  
  60. #endif // FIRST_H
  61.  
  62. // first.tpp
  63. #ifndef TPP_FIRST
  64. #define TPP_FIRST
  65.  
  66. #include "first.h"
  67.  
  68. template <typename Type>
  69. const std::string TypedFirst<Type>::name = "default typed";
  70. template <typename Type>
  71. Type& TypedFirst::value() {return this->_value;}
  72.  
  73. #endif // TPP_FIRST
  74.  
  75. // first.cpp
  76. #include "first.h"
  77.  
  78. First::~First() {}
  79. const std::string First::name = "default";
  80.  
  81. // concretefirsta.h
  82. #ifndef CONCRETEFIRSTA_H
  83. #define CONCRETEFIRSTA_H
  84.  
  85. #include "first.h"
  86. class ConcreteFirstA : public TypedFirst<int>{
  87.   public:
  88.     static const std::string name;
  89.     ConcreteFirstA(const Tree *baseTree);
  90.     virtual ~ConcreteFirstA();
  91. };
  92.  
  93. #endif // CONCRETEFIRSTA_H
  94.  
  95. // concretefirsta.cpp
  96. #include "concretefirsta.h"
  97.  
  98. const std::string ConcreteFirstA::name = "firstA";
  99. ConcreteFirstA::ConcreteFirstA(const Tree* baseTree) : TypedFirst<int>(baseTree) {}
  100. ConcreteFirstA::~ConcreteFirstA() {}
  101.  
  102. // main.cpp
  103. #include "tree.h"
  104. #include "first.h"
  105.  
  106. #include "concretefirsta.h"
  107.  
  108. using namespace std;
  109.  
  110. int main()
  111. {
  112.     Tree *myTree = new Tree();
  113.     myTree->addFirstToTree<ConcreteFirstA>();
  114.     delete myTree;
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement