Guest User

Untitled

a guest
Sep 26th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  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. #include "node.h"
  42. class Tree;
  43. class First{
  44.   public:
  45.     static const std::string name;
  46.     First(const Tree *baseTree) : myTree(baseTree) {}
  47.     virtual ~First();
  48.   protected:
  49.     const Tree *myTree;
  50. };
  51.  
  52. template <typename Type> class TypedFirst : public First{
  53.   public:
  54.     static const std::string name;
  55.     TypedFirst(const Tree *baseTree) : First(baseTree) {}
  56.     Type &value();
  57.   private:
  58.     Type _value;
  59. };
  60.  
  61. #endif // FIRST_H
  62.  
  63. // first.tpp
  64. #ifndef TPP_FIRST
  65. #define TPP_FIRST
  66.  
  67. #include "first.h"
  68.  
  69. template <typename Type>
  70. const std::string TypedFirst<Type>::name = "default typed";
  71. template <typename Type>
  72. Type& TypedFirst::value() {return this->_value;}
  73.  
  74. #endif // TPP_FIRST
  75.  
  76. // first.cpp
  77. #include "first.h"
  78.  
  79. First::~First() {}
  80. const std::string First::name = "default";
  81.  
  82. // concretefirsta.h
  83. #ifndef CONCRETEFIRSTA_H
  84. #define CONCRETEFIRSTA_H
  85.  
  86. #include "first.h"
  87. class ConcreteFirstA : public TypedFirst<int>{
  88.   public:
  89.     static const std::string name;
  90.     ConcreteFirstA(const Tree *baseTree);
  91.     virtual ~ConcreteFirstA();
  92. };
  93.  
  94. #endif // CONCRETEFIRSTA_H
  95.  
  96. // concretefirsta.cpp
  97. #include "concretefirsta.h"
  98.  
  99. const std::string ConcreteFirstA::name = "firstA";
  100. ConcreteFirstA::ConcreteFirstA(const Tree* baseTree) : TypedFirst<int>(baseTree) {}
  101. ConcreteFirstA::~ConcreteFirstA() {}
  102.  
  103. // main.cpp
  104. #include "tree.h"
  105. #include "first.h"
  106.  
  107. #include "concretefirsta.h"
  108.  
  109. using namespace std;
  110.  
  111. int main()
  112. {
  113.     Tree *myTree = new Tree();
  114.     myTree->addFirstToTree<ConcreteFirstA>();
  115.     delete myTree;
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment