Schnek
blockcontainer.hpp
1 /*
2  * blockcontainer.hpp
3  *
4  * Created on: 14 Sep 2016
5  * Author: Holger Schmitz
6  */
7 
8 #ifndef SCHNEK_BLOCKCONTAINER_HPP_
9 #define SCHNEK_BLOCKCONTAINER_HPP_
10 
11 #include "block.hpp"
12 
13 #include <boost/range.hpp>
14 
15 #include <memory>
16 #include <list>
17 
18 namespace schnek
19 {
20 
21 template<class BlockType>
22 class ChildBlock;
23 
38 template<class ChildType>
40 {
41  friend ChildBlock<ChildType>;
42  public:
44  typedef boost::iterator_range<typename std::list<std::shared_ptr<ChildType> >::const_iterator> iterator_range;
46  typedef typename iterator_range::iterator iterator;
47  private:
49  std::list<std::shared_ptr<ChildType> > children;
50 
55  void addChild(std::shared_ptr<ChildType> child)
56  {
57  children.push_back(child);
58  }
59  protected:
66  iterator_range childBlocks()
67  {
68  return iterator_range(children.begin(), children.end());
69  }
70 
72  size_t numChildren()
73  {
74  return children.size();
75  }
76 };
77 
78 template<class BlockType>
79 class ChildBlock : public schnek::Block, public std::enable_shared_from_this<ChildBlock<BlockType> >
80 {
81  public:
82  ChildBlock(pBlock parent = pBlock()) : schnek::Block(parent) {}
83  protected:
84  void preInit()
85  {
86  schnek::Block *pParent = getParent().get();
87  BlockContainer<BlockType>* parent = dynamic_cast<BlockContainer<BlockType> *>(pParent);
88  std::shared_ptr<BlockType> self = std::dynamic_pointer_cast<BlockType>(this->shared_from_this());
89  if (parent && self) parent->addChild(self);
90  }
91 };
92 
93 } // namespace
94 
95 #endif // SCHNEK_BLOCKCONTAINER_HPP_
boost::iterator_range< typename std::list< std::shared_ptr< ChildType > >::const_iterator > iterator_range
The iterator range type that is returned by childBlocks()
Definition: blockcontainer.hpp:44
A container for child blocks of a given type.
Definition: blockcontainer.hpp:39
Definition: algo.hpp:30
Definition: blockcontainer.hpp:22
iterator_range childBlocks()
Return the child blocks.
Definition: blockcontainer.hpp:66
size_t numChildren()
Returns the number of child blocks.
Definition: blockcontainer.hpp:72
Definition: block.hpp:65
iterator_range::iterator iterator
The iterator type.
Definition: blockcontainer.hpp:46