Schnek
blockclasses.hpp
1 /*
2  * blockclasses.hpp
3  *
4  * Created on: 26 Apr 2012
5  * Author: Holger Schmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_BLOCKCLASSES_HPP_
28 #define SCHNEK_BLOCKCLASSES_HPP_
29 
30 #include <set>
31 #include <map>
32 #include <string>
33 #include <memory>
34 
35 #include "block.hpp"
36 #include "../exception.hpp"
37 
38 namespace schnek {
39 
41 {
42  public:
44 };
45 
47 {
48  public:
49  virtual ~BlockFactory() {}
50  virtual pBlock makeBlock(const std::string &name) = 0;
51 };
52 typedef std::shared_ptr<BlockFactory> pBlockFactory;
53 
54 template<class B>
56 {
57  public:
58  pBlock makeBlock(const std::string &name)
59  {
60  pBlock pb(new B);
61  pb->setName(name);
62  return pb;
63  }
64 };
65 
67 {
68  private:
69  std::set<std::string> allowedChildren;
70  pBlockFactory blockFactory;
71 
72  void doAddChild(std::string child);
73  public:
75  {
76  private:
77  friend class BlockClassDescriptor;
78  BlockClassDescriptor &descriptor;
79  BlockClassChildAdder(BlockClassDescriptor &descriptor_) : descriptor(descriptor_) {}
80  public:
81  BlockClassChildAdder(const BlockClassChildAdder &adder) : descriptor(adder.descriptor) {}
82  BlockClassChildAdder &operator()(std::string child);
83  };
85 
86 
87  public:
88  BlockClassChildAdder addChildren(std::string child);
89  bool hasChild(std::string child);
90 
91  template<class B> void setClass()
92  {
93  pBlockFactory bf(new GenericBlockFactory<B>());
94  blockFactory = bf;
95  }
96 
97  bool hasBlockFactory() { return (bool)blockFactory; }
98  pBlock makeBlock(const std::string &name) { return blockFactory->makeBlock(name); }
99 };
100 
101 typedef std::shared_ptr<BlockClassDescriptor> pBlockClassDescriptor;
102 
104 {
105  private:
106  typedef std::map<std::string, pBlockClassDescriptor> BlockClassDescriptors;
107  std::shared_ptr<BlockClassDescriptors> classDescriptors;
108  bool restr;
109  public:
110 
111  BlockClasses(bool restr_ = true) : classDescriptors(new BlockClassDescriptors), restr(restr_) {}
112  BlockClasses(const BlockClasses& blockClasses)
113  : classDescriptors(blockClasses.classDescriptors), restr(blockClasses.restr) {}
114 
115  BlockClassDescriptor &registerBlock(std::string blockClass);
116  BlockClassDescriptor &operator()(std::string blockClass) { return this->get(blockClass); }
117  BlockClassDescriptor &get(std::string blockClass);
118 
119  bool hasChild(std::string parent, std::string child);
120  bool restrictBlocks() {return restr;}
121 };
122 
123 } // namespace
124 
125 #endif // SCHNEK_BLOCKCLASSES_HPP_
Definition: blockclasses.hpp:55
Definition: algo.hpp:30
Definition: blockclasses.hpp:40
Definition: blockclasses.hpp:103
Definition: blockclasses.hpp:66
Definition: blockclasses.hpp:46
Definition: exception.hpp:33