HOPS
HOPS class reference
MHO_ExtensibleElement.hh
Go to the documentation of this file.
1 #ifndef MHO_ExtensibleElement_HH__
2 #define MHO_ExtensibleElement_HH__
3 
4 #include <vector>
5 
6 namespace hops
7 {
8 
21 //forward declare these types
22 class MHO_Visitor;
23 class MHO_Element;
24 class MHO_ExtensibleElement;
25 template< class XExtensionType > class MHO_ExtendedElement;
26 
27 //visitor interface
32 {
33  public:
35  virtual ~MHO_Visitor(){};
36 };
37 
42 {
43  public:
45  virtual ~MHO_Element(){};
46 
53  virtual void Accept(MHO_Visitor* aVisitor) = 0;
54 };
55 
60 {
61  public:
63 
65  {
66  for(auto it = fExtensions.begin(); it != fExtensions.end(); it++)
67  {
68  delete (*it);
69  (*it) = nullptr;
70  }
71  fExtensions.clear();
72  }
73 
79  void Accept(MHO_Visitor* aVisitor) override
80  {
81  //the MHO_ExtensibleElement class by-itself is just a container,
82  //no need to visit it by itself, just visit all the extentions
83  for(auto it = fExtensions.begin(); it != fExtensions.end(); it++)
84  {
85  (*it)->Accept(aVisitor);
86  }
87  }
88 
89  public:
95  template< class XExtensionType > bool HasExtension() const;
102  template< class XExtensionType > MHO_ExtendedElement< XExtensionType >* MakeExtension();
109  template< class XExtensionType > MHO_ExtendedElement< XExtensionType >* AsExtension();
110 
111  protected:
112  std::vector< MHO_Element* > fExtensions;
113 };
114 
117 
123 template< class XExtensionType > inline bool MHO_ExtensibleElement::HasExtension() const
124 {
126  for(auto it = fExtensions.begin(); it != fExtensions.end(); it++)
127  {
128  extention = dynamic_cast< MHO_ExtendedElement< XExtensionType >* >(*it);
129  if(extention != nullptr)
130  {
131  return true;
132  }
133  }
134  return false;
135 }
136 
143 {
145  for(auto it = fExtensions.begin(); it != fExtensions.end(); it++)
146  {
147  extention = dynamic_cast< MHO_ExtendedElement< XExtensionType >* >(*it);
148  if(extention != nullptr)
149  {
150  delete extention;
151  fExtensions.erase(it);
152  break;
153  }
154  }
155  extention = new MHO_ExtendedElement< XExtensionType >(this);
156  fExtensions.push_back(extention);
157  return extention;
158 }
159 
166 {
168  for(auto it = fExtensions.begin(); it != fExtensions.end(); it++)
169  {
170  extention = dynamic_cast< MHO_ExtendedElement< XExtensionType >* >(*it);
171  if(extention != nullptr)
172  {
173  return extention;
174  };
175  }
176  return nullptr;
177 }
178 
181 
185 template< class XExtensionType > class MHO_ExtendedElement: public MHO_Element, public XExtensionType
186 {
187  public:
189 
190  virtual ~MHO_ExtendedElement(){};
191 
192  public:
197  {
198  public:
200  virtual ~ExtendedVisitor(){};
201 
209  };
210 
211  public:
218  virtual void Accept(MHO_Visitor* aVisitor) override
219  {
220  //visit this extension
221  auto* extVisitor = dynamic_cast< typename MHO_ExtendedElement< XExtensionType >::ExtendedVisitor* >(aVisitor);
222  if(extVisitor != nullptr)
223  {
224  extVisitor->VisitExtendedElement(this);
225  };
226  }
227 
228  protected:
230 };
231 
232 } // namespace hops
233 
234 #endif
Class MHO_Element.
Definition: MHO_ExtensibleElement.hh:42
virtual void Accept(MHO_Visitor *aVisitor)=0
Visits all extensions of this extensible element using the given visitor.
MHO_Element()
Definition: MHO_ExtensibleElement.hh:44
virtual ~MHO_Element()
Definition: MHO_ExtensibleElement.hh:45
Class ExtendedVisitor.
Definition: MHO_ExtensibleElement.hh:197
virtual ~ExtendedVisitor()
Definition: MHO_ExtensibleElement.hh:200
ExtendedVisitor()
Definition: MHO_ExtensibleElement.hh:199
virtual void VisitExtendedElement(MHO_ExtendedElement< XExtensionType > *anElement)=0
Function VisitExtendedElement.
Class MHO_ExtendedElement.
Definition: MHO_ExtensibleElement.hh:186
MHO_ExtendedElement(MHO_ExtensibleElement *parent)
Definition: MHO_ExtensibleElement.hh:188
MHO_ExtensibleElement * fParent
Definition: MHO_ExtensibleElement.hh:229
virtual ~MHO_ExtendedElement()
Definition: MHO_ExtensibleElement.hh:190
virtual void Accept(MHO_Visitor *aVisitor) override
Visits all extensions of this extensible element using the given visitor.
Definition: MHO_ExtensibleElement.hh:218
Class MHO_ExtensibleElement.
Definition: MHO_ExtensibleElement.hh:60
MHO_ExtensibleElement()
Definition: MHO_ExtensibleElement.hh:62
MHO_ExtendedElement< XExtensionType > * AsExtension()
Returns an extension of type XExtensionType if found in the list of extensions.
Definition: MHO_ExtensibleElement.hh:165
virtual ~MHO_ExtensibleElement()
Definition: MHO_ExtensibleElement.hh:64
void Accept(MHO_Visitor *aVisitor) override
Visits all extensions of MHO_ExtensibleElement using given visitor.
Definition: MHO_ExtensibleElement.hh:79
bool HasExtension() const
Checks if an extensible element has a specific extension type.
Definition: MHO_ExtensibleElement.hh:123
std::vector< MHO_Element * > fExtensions
Definition: MHO_ExtensibleElement.hh:112
MHO_ExtendedElement< XExtensionType > * MakeExtension()
Creates and adds a new extension of type XExtensionType to the list of extensions.
Definition: MHO_ExtensibleElement.hh:142
Class MHO_Visitor.
Definition: MHO_ExtensibleElement.hh:32
MHO_Visitor()
Definition: MHO_ExtensibleElement.hh:34
virtual ~MHO_Visitor()
Definition: MHO_ExtensibleElement.hh:35
Definition: MHO_ChannelLabeler.hh:17