vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DynamicAttribute.h
Go to the documentation of this file.
1 
13 // header definitions
14 #ifndef _DYNAMIC_ATTRIBUTE_H_
15 #define _DYNAMIC_ATTRIBUTE_H_
16 
17 // attributes description includes
18 #include "Attribute.h"
19 
20 // STL includes
21 #include <string>
22 #include <list>
23 #include <map>
24 
25 // namespaces used
26 using namespace std;
27 
28 // VRECKO namespace
29 namespace vrecko
30 {
31 
43 {
44 public:
45 
54  DynamicAttribute(const string& strName, const string& strDescription,
55  unsigned int minInstancesCount = 0, unsigned int maxInstancesCount = 10)
56  : Attribute(strName, Attribute::TYPE_DYNAMIC, strDescription)
57  {
58  // initialize instances range
59  this->minInstancesCount = minInstancesCount;
60  this->maxInstancesCount = maxInstancesCount;
61  }
62 
67  {
68  // prepare the internal iterator
69  attributesListIter = attributesList.begin();
70 
71  // while the iterator is not at the end
72  while (attributesListIter != attributesList.end())
73  {
74  // if the pointer is not null
75  if (*attributesListIter != null)
76  {
77  // delete the data and null the pointer
78  delete *attributesListIter;
79  *attributesListIter = null;
80  }
81 
82  // increment the iterator
83  attributesListIter++;
84  }
85  }
86 
92  inline unsigned int getMinInstancesCount() const
93  {
94  return minInstancesCount;
95  }
96 
102  inline unsigned int getMaxInstancesCount() const
103  {
104  return maxInstancesCount;
105  }
106 
119  bool addAttribute(Attribute* pAttribute)
120  {
121  // if the name already exists or the attribute is dynamic
122  if (containsAttribute(pAttribute->getName()) || pAttribute->isDynamic())
123  {
124  // delete the data and null the pointer
125  delete pAttribute;
126  pAttribute = null;
127 
128  return false;
129  }
130 
131  // insert the attribute into both containers
132  attributesList.push_back(pAttribute);
133  attributesMap[pAttribute->getName()] = pAttribute;
134 
135  return true;
136  }
137 
143  inline unsigned int attributesCount() const
144  {
145  return attributesList.size();
146  }
147 
155  inline bool containsAttribute(const string& strName) const
156  {
157  return (attributesMap.find(strName) != attributesMap.end());
158  }
159 
167  inline const Attribute* getAttributePtr(const string& strName) const
168  {
169  return attributesMap.find(strName)->second;
170  }
171 
175  inline void startIteration()
176  {
177  attributesListIter = attributesList.begin();
178  }
179 
191  inline bool hasNextAttribute() const
192  {
193  return (attributesListIter != attributesList.end());
194  }
195 
207  inline const Attribute* nextAttributePtr()
208  {
209  return *attributesListIter++;
210  }
211 
212 private:
213 
214  // instances range
215  unsigned int minInstancesCount;
216  unsigned int maxInstancesCount;
217 
218  // containers
219  list<Attribute*> attributesList;
220  map<string, const Attribute*> attributesMap;
221 
222  // internal list iterator
223  list<Attribute*>::iterator attributesListIter;
224 };
225 
226 // end of vrecko namespace
227 }
228 
229 // end header definitions
230 #endif
231