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 SetInt64(const Int64AttrId& attrId, int64_t val);
60 int64_t GetInt64(const Int64AttrId& attrId) const;
62 int64_t GetInt64(const Int64AttrId& attrId, int64_t defaultValue) const;
64 void SetString(const StringAttrId& attrId, const Util::String& val);
66 const Util::String& GetString(const StringAttrId& attrId) const;
68 const Util::String& GetString(const StringAttrId& attrId, const Util::String& defaultValue) const;
70 void SetFloat4(const Vec4AttrId& attrId, const Math::vec4& val);
72 Math::vec4 GetVec4(const Vec4AttrId& attrId) const;
74 Math::vec4 GetVec4(const Vec4AttrId& attrId, const Math::vec4& defaultValue) const;
76 void SetMatrix44(const Mat4AttrId& attrId, const Math::mat4& val);
78 const Math::mat4& GetMat4(const Mat4AttrId& attrId) const;
80 const Math::mat4& GetMat4(const Mat4AttrId& attrId, const Math::mat4& defaultValue) const;
82 void SetGuid(const GuidAttrId& attrId, const Util::Guid& guid);
84 const Util::Guid& GetGuid(const GuidAttrId& attrId) const;
86 const Util::Guid& GetGuid(const GuidAttrId& attrId, const Util::Guid& defaultValue) const;
88 void SetBlob(const BlobAttrId& attrId, const Util::Blob& blob);
90 const Util::Blob& GetBlob(const BlobAttrId& attrId) const;
92 const Util::Blob& GetBlob(const BlobAttrId& attrId, const Util::Blob& defaultValue) const;
93
94private:
96 int FindAttrIndex(const Attr::AttrId& attrId) const;
97
99};
100
101//------------------------------------------------------------------------------
104inline void
106{
107 this->SetAttr(Attribute(attrId, val));
108}
109
110//------------------------------------------------------------------------------
113inline bool
115{
116 n_assert(this->attrs.Contains(attrId));
117 return this->attrs[attrId].GetBool();
118}
119
120//------------------------------------------------------------------------------
123inline bool
124AttributeContainer::GetBool(const BoolAttrId& attrId, bool defaultValue) const
125{
126 if (this->HasAttr(attrId))
127 {
128 return this->GetBool(attrId);
129 }
130 else
131 {
132 return defaultValue;
133 }
134}
135
136//------------------------------------------------------------------------------
139inline void
141{
142 this->SetAttr(Attribute(attrId, val));
143}
144
145//------------------------------------------------------------------------------
148inline float
150{
151 n_assert(this->attrs.Contains(attrId));
152 return this->attrs[attrId].GetFloat();
153}
154
155//------------------------------------------------------------------------------
158inline float
159AttributeContainer::GetFloat(const FloatAttrId& attrId, float defaultValue) const
160{
161 if (this->HasAttr(attrId))
162 {
163 return this->GetFloat(attrId);
164 }
165 else
166 {
167 return defaultValue;
168 }
169}
170
171//------------------------------------------------------------------------------
174inline void
176{
177 this->SetAttr(Attribute(attrId, val));
178}
179
180//------------------------------------------------------------------------------
183inline int
185{
186 n_assert(this->attrs.Contains(attrId));
187 return this->attrs[attrId].GetInt();
188}
189
190//------------------------------------------------------------------------------
193inline int
194AttributeContainer::GetInt(const IntAttrId& attrId, int defaultValue) const
195{
196 if (this->HasAttr(attrId))
197 {
198 return this->GetInt(attrId);
199 }
200 else
201 {
202 return defaultValue;
203 }
204}
205
206//------------------------------------------------------------------------------
209inline void
210AttributeContainer::SetInt64(const Int64AttrId& attrId, int64_t val)
211{
212 this->SetAttr(Attribute(attrId, val));
213}
214
215//------------------------------------------------------------------------------
218inline int64_t
220{
221 n_assert(this->attrs.Contains(attrId));
222 return this->attrs[attrId].GetInt64();
223}
224
225//------------------------------------------------------------------------------
228inline int64_t
229AttributeContainer::GetInt64(const Int64AttrId& attrId, int64_t defaultValue) const
230{
231 if (this->HasAttr(attrId))
232 {
233 return this->GetInt64(attrId);
234 }
235 else
236 {
237 return defaultValue;
238 }
239}
240
241//------------------------------------------------------------------------------
244inline void
246{
247 this->SetAttr(Attribute(attrId, val));
248}
249
250//------------------------------------------------------------------------------
253inline const Util::String&
255{
256 n_assert(this->attrs.Contains(attrId));
257 return this->attrs[attrId].GetString();
258}
259
260//------------------------------------------------------------------------------
263inline const Util::String&
264AttributeContainer::GetString(const StringAttrId& attrId, const Util::String& defaultValue) const
265{
266 if (this->HasAttr(attrId))
267 {
268 return this->GetString(attrId);
269 }
270 else
271 {
272 return defaultValue;
273 }
274}
275
276//------------------------------------------------------------------------------
279inline void
281{
282 this->SetAttr(Attribute(attrId, val));
283}
284
285//------------------------------------------------------------------------------
288inline Math::vec4
290{
291 n_assert(this->attrs.Contains(attrId));
292 return this->attrs[attrId].GetVec4();
293}
294
295//------------------------------------------------------------------------------
298inline Math::vec4
299AttributeContainer::GetVec4(const Vec4AttrId& attrId, const Math::vec4& defaultValue) const
300{
301 if (this->HasAttr(attrId))
302 {
303 return this->GetVec4(attrId);
304 }
305 else
306 {
307 return defaultValue;
308 }
309}
310
311//------------------------------------------------------------------------------
314inline void
316{
317 this->SetAttr(Attribute(attrId, val));
318}
319
320//------------------------------------------------------------------------------
323inline const Math::mat4&
325{
326 n_assert(this->attrs.Contains(attrId));
327 return this->attrs[attrId].GetMat4();
328}
329
330//------------------------------------------------------------------------------
333inline const Math::mat4&
334AttributeContainer::GetMat4(const Mat4AttrId& attrId, const Math::mat4& defaultValue) const
335{
336 if (this->HasAttr(attrId))
337 {
338 return this->GetMat4(attrId);
339 }
340 else
341 {
342 return defaultValue;
343 }
344}
345
346//------------------------------------------------------------------------------
349inline void
350AttributeContainer::SetGuid(const GuidAttrId& attrId, const Util::Guid& val)
351{
352 this->SetAttr(Attribute(attrId, val));
353}
354
355//------------------------------------------------------------------------------
358inline const Util::Guid&
360{
361 n_assert(this->attrs.Contains(attrId));
362 return this->attrs[attrId].GetGuid();
363}
364
365//------------------------------------------------------------------------------
368inline const Util::Guid&
369AttributeContainer::GetGuid(const GuidAttrId& attrId, const Util::Guid& defaultValue) const
370{
371 if (this->HasAttr(attrId))
372 {
373 return this->GetGuid(attrId);
374 }
375 else
376 {
377 return defaultValue;
378 }
379}
380
381//------------------------------------------------------------------------------
384inline void
386{
387 this->SetAttr(Attribute(attrId, val));
388}
389
390//------------------------------------------------------------------------------
393inline const Util::Blob&
395{
396 n_assert(this->attrs.Contains(attrId));
397 return this->attrs[attrId].GetBlob();
398}
399
400//------------------------------------------------------------------------------
403inline const Util::Blob&
404AttributeContainer::GetBlob(const BlobAttrId& attrId, const Util::Blob& defaultValue) const
405{
406 if (this->HasAttr(attrId))
407 {
408 return this->GetBlob(attrId);
409 }
410 else
411 {
412 return defaultValue;
413 }
414}
415
416} // namespace Attr
417//------------------------------------------------------------------------------
An attribute ID is used to carry attribute types (no values) around.
Definition attrid.h:20
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:175
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:385
const Util::Guid & GetGuid(const GuidAttrId &attrId) const
get guid value
Definition attributecontainer.h:359
void SetFloat4(const Vec4AttrId &attrId, const Math::vec4 &val)
set float4 value
Definition attributecontainer.h:280
int64_t GetInt64(const Int64AttrId &attrId) const
get int64 value
Definition attributecontainer.h:219
void SetInt64(const Int64AttrId &attrId, int64_t val)
set int64 value
Definition attributecontainer.h:210
void SetString(const StringAttrId &attrId, const Util::String &val)
set string value
Definition attributecontainer.h:245
Math::vec4 GetVec4(const Vec4AttrId &attrId) const
get float4 value
Definition attributecontainer.h:289
bool GetBool(const BoolAttrId &attrId) const
get bool value
Definition attributecontainer.h:114
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:350
~AttributeContainer()
destructor
Definition attributecontainer.cc:23
int GetInt(const IntAttrId &attrId) const
get int value
Definition attributecontainer.h:184
void SetMatrix44(const Mat4AttrId &attrId, const Math::mat4 &val)
set matrix44 value
Definition attributecontainer.h:315
const Util::Blob & GetBlob(const BlobAttrId &attrId) const
get blob value
Definition attributecontainer.h:394
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:149
const Util::String & GetString(const StringAttrId &attrId) const
get string value
Definition attributecontainer.h:254
AttributeContainer()
constructor
Definition attributecontainer.cc:15
void SetFloat(const FloatAttrId &attrId, float val)
set float value
Definition attributecontainer.h:140
const Math::mat4 & GetMat4(const Mat4AttrId &attrId) const
get matrix44 value
Definition attributecontainer.h:324
int FindAttrIndex(const Attr::AttrId &attrId) const
find index for attribute (SLOW!)
Util::Dictionary< Attr::AttrId, Attribute > attrs
Definition attributecontainer.h:98
void SetBool(const BoolAttrId &attrId, bool val)
set bool value
Definition attributecontainer.h:105
A compiletime-typesafe key/value pair.
Definition attribute.h:33
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
Definition intattrid.h:97
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:35
#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.cs:8