Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 1.15 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. factory pattern - with templates
  2. template<typename Iterator_T>
  3. struct Tree
  4. {
  5.     template<typename Node_T>
  6.     struct TypedNode;
  7.  
  8.     struct AbstractNode
  9.     {
  10.         AbstractNode(Iterator_T t)
  11.         {
  12.             ...
  13.         }
  14.  
  15.         template<typename Rule_T>
  16.         TypedNode<Rule_T>* NewChild()
  17.         {
  18.             TypedNode<Rule_T>* ret = new TypedNode<Rule_T>(this);
  19.             AddChild(ret);
  20.             return ret;
  21.         }
  22.  
  23.         template<typename T>
  24.         TypedNode<T>* GetFirstTypedChild();
  25.     };
  26.  
  27.     template<typename Node_T>
  28.     struct TypedNode : AbstractNode
  29.     {
  30.         ...
  31.     };
  32.  
  33.     template<typename Rule_T, typename ParserState_T>
  34.     void CreateNode(ParserState_T& p)
  35.     {
  36.         current = current->template NewChild<Rule_T>();
  37.     }
  38. };
  39.  
  40. template<typename Node_T>
  41. struct TreeBuilder
  42. {
  43.     Tree<Iterator_T> tree;
  44.  
  45.     template<typename Rule_T>
  46.     void CreateNode()
  47.     {
  48.         tree.CreateNode<Rule_T>(*this);
  49.     }
  50. };
  51.        
  52. template<>
  53. struct Tree<const char*>::TypedNode<SpecificRuleType>
  54. {
  55.     ...
  56. };
  57.        
  58. template<> // for Tree<...>
  59. template<> // for TypedNode<...>
  60. struct Tree<const char*>::TypedNode<SpecificRuleType>
  61. {
  62.     ...
  63. };