Nebula
Loading...
Searching...
No Matches
attributecontainer.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "util/dictionary.h"
13#include "attribute.h"
14
15//------------------------------------------------------------------------------
16namespace Attr
17{
19{
20public:
26 bool HasAttr(const AttrId& attrId) const;
28 void SetAttr(const Attribute& attr);
30 const Attribute& GetAttr(const AttrId& attrId) const;
34 void RemoveAttr(const AttrId& attrId);
36 void Clear();
38 void AddAttr(const Attribute& attr);
40 void SetBool(const BoolAttrId& attrId, bool val);
42 bool GetBool(const BoolAttrId& attrId) const;
44 bool GetBool(const BoolAttrId& attrId, bool defaultValue) const;
46 void SetFloat(const FloatAttrId& attrId, float val);
48 float GetFloat(const FloatAttrId& attrId) const;
50 float GetFloat(const FloatAttrId& attrId, float defaultValue) const;
52 void SetInt(const IntAttrId& attrId, int val);
54 int GetInt(const IntAttrId& attrId) const;
56 int GetInt(const IntAttrId& attrId, int defaultValue) const;
58 void SetString(const StringAttrId& attrId, const Util::String& val);
60 const Util::String& GetString(const StringAttrId& attrId) const;
62 const Util::String& GetString(const StringAttrId& attrId, const Util::String& defaultValue) const;
64 void SetFloat4(const Vec4AttrId& attrId, const Math::vec4& val);
66 Math::vec4 GetVec4(const Vec4AttrId& attrId) const;
68 Math::vec4 GetVec4(const Vec4AttrId& attrId, const Math::vec4& defaultValue) const;
70 void SetMatrix44(const Mat4AttrId& attrId, const Math::mat4& val);
72 const Math::mat4& GetMat4(const Mat4AttrId& attrId) const;
74 const Math::mat4& GetMat4(const Mat4AttrId& attrId, const Math::mat4& defaultValue) const;
76 void SetGuid(const GuidAttrId& attrId, const Util::Guid& guid);
78 const Util::Guid& GetGuid(const GuidAttrId& attrId) const;
80 const Util::Guid& GetGuid(const GuidAttrId& attrId, const Util::Guid& defaultValue) const;
82 void SetBlob(const BlobAttrId& attrId, const Util::Blob& blob);
84 const Util::Blob& GetBlob(const BlobAttrId& attrId) const;
86 const Util::Blob& GetBlob(const BlobAttrId& attrId, const Util::Blob& defaultValue) const;
87
88private:
90 int FindAttrIndex(const Attr::AttrId& attrId) const;
91
93};
94
95//------------------------------------------------------------------------------
98inline void
100{
101 this->SetAttr(Attribute(attrId, val));
102}
103
104//------------------------------------------------------------------------------
107inline bool
109{
110 n_assert(this->attrs.Contains(attrId));
111 return this->attrs[attrId].GetBool();
112}
113
114//------------------------------------------------------------------------------
117inline bool
118AttributeContainer::GetBool(const BoolAttrId& attrId, bool defaultValue) const
119{
120 if (this->HasAttr(attrId))
121 {
122 return this->GetBool(attrId);
123 }
124 else
125 {
126 return defaultValue;
127 }
128}
129
130//------------------------------------------------------------------------------
133inline void
135{
136 this->SetAttr(Attribute(attrId, val));
137}
138
139//------------------------------------------------------------------------------
142inline float
144{
145 n_assert(this->attrs.Contains(attrId));
146 return this->attrs[attrId].GetFloat();
147}
148
149//------------------------------------------------------------------------------
152inline float
153AttributeContainer::GetFloat(const FloatAttrId& attrId, float defaultValue) const
154{
155 if (this->HasAttr(attrId))
156 {
157 return this->GetFloat(attrId);
158 }
159 else
160 {
161 return defaultValue;
162 }
163}
164
165//------------------------------------------------------------------------------
168inline void
170{
171 this->SetAttr(Attribute(attrId, val));
172}
173
174//------------------------------------------------------------------------------
177inline int
179{
180 n_assert(this->attrs.Contains(attrId));
181 return this->attrs[attrId].GetInt();
182}
183
184//------------------------------------------------------------------------------
187inline int
188AttributeContainer::GetInt(const IntAttrId& attrId, int defaultValue) const
189{
190 if (this->HasAttr(attrId))
191 {
192 return this->GetInt(attrId);
193 }
194 else
195 {
196 return defaultValue;
197 }
198}
199
200//------------------------------------------------------------------------------
203inline void
205{
206 this->SetAttr(Attribute(attrId, val));
207}
208
209//------------------------------------------------------------------------------
212inline const Util::String&
214{
215 n_assert(this->attrs.Contains(attrId));
216 return this->attrs[attrId].GetString();
217}
218
219//------------------------------------------------------------------------------
222inline const Util::String&
223AttributeContainer::GetString(const StringAttrId& attrId, const Util::String& defaultValue) const
224{
225 if (this->HasAttr(attrId))
226 {
227 return this->GetString(attrId);
228 }
229 else
230 {
231 return defaultValue;
232 }
233}
234
235//------------------------------------------------------------------------------
238inline void
240{
241 this->SetAttr(Attribute(attrId, val));
242}
243
244//------------------------------------------------------------------------------
247inline Math::vec4
249{
250 n_assert(this->attrs.Contains(attrId));
251 return this->attrs[attrId].GetVec4();
252}
253
254//------------------------------------------------------------------------------
257inline Math::vec4
258AttributeContainer::GetVec4(const Vec4AttrId& attrId, const Math::vec4& defaultValue) const
259{
260 if (this->HasAttr(attrId))
261 {
262 return this->GetVec4(attrId);
263 }
264 else
265 {
266 return defaultValue;
267 }
268}
269
270//------------------------------------------------------------------------------
273inline void
275{
276 this->SetAttr(Attribute(attrId, val));
277}
278
279//------------------------------------------------------------------------------
282inline const Math::mat4&
284{
285 n_assert(this->attrs.Contains(attrId));
286 return this->attrs[attrId].GetMat4();
287}
288
289//------------------------------------------------------------------------------
292inline const Math::mat4&
293AttributeContainer::GetMat4(const Mat4AttrId& attrId, const Math::mat4& defaultValue) const
294{
295 if (this->HasAttr(attrId))
296 {
297 return this->GetMat4(attrId);
298 }
299 else
300 {
301 return defaultValue;
302 }
303}
304
305//------------------------------------------------------------------------------
308inline void
310{
311 this->SetAttr(Attribute(attrId, val));
312}
313
314//------------------------------------------------------------------------------
317inline const Util::Guid&
319{
320 n_assert(this->attrs.Contains(attrId));
321 return this->attrs[attrId].GetGuid();
322}
323
324//------------------------------------------------------------------------------
327inline const Util::Guid&
328AttributeContainer::GetGuid(const GuidAttrId& attrId, const Util::Guid& defaultValue) const
329{
330 if (this->HasAttr(attrId))
331 {
332 return this->GetGuid(attrId);
333 }
334 else
335 {
336 return defaultValue;
337 }
338}
339
340//------------------------------------------------------------------------------
343inline void
345{
346 this->SetAttr(Attribute(attrId, val));
347}
348
349//------------------------------------------------------------------------------
352inline const Util::Blob&
354{
355 n_assert(this->attrs.Contains(attrId));
356 return this->attrs[attrId].GetBlob();
357}
358
359//------------------------------------------------------------------------------
362inline const Util::Blob&
363AttributeContainer::GetBlob(const BlobAttrId& attrId, const Util::Blob& defaultValue) const
364{
365 if (this->HasAttr(attrId))
366 {
367 return this->GetBlob(attrId);
368 }
369 else
370 {
371 return defaultValue;
372 }
373}
374
375} // namespace Attr
376//------------------------------------------------------------------------------
An attribute ID is used to carry attribute types (no values) around.
Definition attrid.h:20
A simple container for attributes.
Definition attributecontainer.h:19
const Attribute & GetAttr(const AttrId &attrId) const
get a single attribute
Definition attributecontainer.cc:78
const Util::Dictionary< AttrId, Attribute > & GetAttrs() const
read access to the attribute array
Definition attributecontainer.cc:97
void SetAttr(const Attribute &attr)
set a single attribute, new or existing
Definition attributecontainer.cc:46
void SetInt(const IntAttrId &attrId, int val)
set int value
Definition attributecontainer.h:169
void RemoveAttr(const AttrId &attrId)
remove an attribute
Definition attributecontainer.cc:117
void SetBlob(const BlobAttrId &attrId, const Util::Blob &blob)
set blob value
Definition attributecontainer.h:344
const Util::Guid & GetGuid(const GuidAttrId &attrId) const
get guid value
Definition attributecontainer.h:318
void SetFloat4(const Vec4AttrId &attrId, const Math::vec4 &val)
set float4 value
Definition attributecontainer.h:239
void SetString(const StringAttrId &attrId, const Util::String &val)
set string value
Definition attributecontainer.h:204
Math::vec4 GetVec4(const Vec4AttrId &attrId) const
get float4 value
Definition attributecontainer.h:248
bool GetBool(const BoolAttrId &attrId) const
get bool value
Definition attributecontainer.h:108
bool HasAttr(const AttrId &attrId) const
check if an attribute exists in the container
Definition attributecontainer.cc:33
void SetGuid(const GuidAttrId &attrId, const Util::Guid &guid)
set guid value
Definition attributecontainer.h:309
~AttributeContainer()
destructor
Definition attributecontainer.cc:23
int GetInt(const IntAttrId &attrId) const
get int value
Definition attributecontainer.h:178
void SetMatrix44(const Mat4AttrId &attrId, const Math::mat4 &val)
set matrix44 value
Definition attributecontainer.h:274
const Util::Blob & GetBlob(const BlobAttrId &attrId) const
get blob value
Definition attributecontainer.h:353
void AddAttr(const Attribute &attr)
add a new attribute, faster then SetAttr(), but attribute may not exist!
Definition attributecontainer.cc:67
void Clear()
clear the attribute container
Definition attributecontainer.cc:107
float GetFloat(const FloatAttrId &attrId) const
get float value
Definition attributecontainer.h:143
const Util::String & GetString(const StringAttrId &attrId) const
get string value
Definition attributecontainer.h:213
AttributeContainer()
constructor
Definition attributecontainer.cc:15
void SetFloat(const FloatAttrId &attrId, float val)
set float value
Definition attributecontainer.h:134
const Math::mat4 & GetMat4(const Mat4AttrId &attrId) const
get matrix44 value
Definition attributecontainer.h:283
int FindAttrIndex(const Attr::AttrId &attrId) const
find index for attribute (SLOW!)
Util::Dictionary< Attr::AttrId, Attribute > attrs
Definition attributecontainer.h:92
void SetBool(const BoolAttrId &attrId, bool val)
set bool value
Definition attributecontainer.h:99
A compiletime-typesafe key/value pair.
Definition attribute.h:32
Typed attribute id for blob type.
Definition blobattrid.h:18
Typed attribute id for bool type.
Definition boolattrid.h:18
Typed attribute id for float type.
Definition floatattrid.h:18
Typed attribute id for guid type.
Definition guidattrid.h:18
Typed attribute id for integer type.
Definition intattrid.h:18
Typed attribute id for mat4 type.
Definition mat4attrid.h:18
Typed attribute id for string type.
Definition stringattrid.h:18
Typed attribute id for vec4 type.
Definition vec4attrid.h:18
The Util::Blob class encapsulates a chunk of raw memory into a C++ object which can be copied,...
Definition blob.h:22
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:34
Implements a GUID.
#define n_assert(exp)
Definition debug.h:50
Definition accessmode.h:13
A 4x4 single point precision float matrix.
Definition mat4.h:49
A 4D vector.
Definition vec4.h:24
Nebula's universal string class.
Definition string.h:50