newspeoplefor developersdocumentationdownloads

attribute.h

Go to the documentation of this file.
00001 #ifndef DB_ATTR_H
00002 #define DB_ATTR_H
00003 //------------------------------------------------------------------------------
00029 #include "attr/attributeid.h"
00030 #include "attr/voidattributeid.h"
00031 #include "attr/boolattributeid.h"
00032 #include "attr/intattributeid.h"
00033 #include "attr/floatattributeid.h"
00034 #include "attr/stringattributeid.h"
00035 #include "attr/vector3attributeid.h"
00036 #include "attr/vector4attributeid.h"
00037 #include "attr/matrix44attributeid.h"
00038 #include "mathlib/vector.h"
00039 #include "util/nvariant.h"
00040 
00041 //------------------------------------------------------------------------------
00042 namespace Db
00043 {
00044 class Attribute
00045 {
00046 public:
00048     enum Type
00049     {
00050         Void     = nVariant::Void,
00051         Int      = nVariant::Int,
00052         Float    = nVariant::Float,
00053         Bool     = nVariant::Bool,
00054         Vector3  = nVariant::Vector3,
00055         Vector4  = nVariant::Vector4,
00056         String   = nVariant::String,
00057         Matrix44 = nVariant::Matrix44,
00058     };        
00059 
00061     Attribute();
00063     Attribute(const Attribute& rhs);
00065     Attribute(const Attr::AttributeID& id);
00067     Attribute(const Attr::VoidAttributeID& id);
00069     Attribute(const Attr::StringAttributeID& id, const nString& val);
00071     Attribute(const Attr::IntAttributeID& id, int val);
00073     Attribute(const Attr::FloatAttributeID& id, float val);
00075     Attribute(const Attr::BoolAttributeID& id, bool val);
00077     Attribute(const Attr::Vector3AttributeID& id, const vector3& val);
00079     Attribute(const Attr::Vector4AttributeID& id, const vector4& val);
00081     Attribute(const Attr::Matrix44AttributeID& id, const matrix44& val);
00082 
00084     void SetAttributeID(const Attr::AttributeID& id);
00086     const Attr::AttributeID& GetAttributeID() const;
00088     const nString& GetName() const;
00090     Type GetType() const;
00092     bool IsWritable() const;
00094     bool IsStorable() const;
00096     void Clear();
00098     bool IsValid() const;
00100     bool IsEmpty() const;
00101 
00103     void operator=(const Attribute& rhs);
00105     void operator=(const nString& rhs);
00107     void operator=(int rhs);
00109     void operator=(float rhs);
00111     void operator=(bool rhs);
00113     void operator=(const vector3& rhs);
00115     void operator=(const vector4& rhs);
00117     void operator=(const matrix44& rhs);
00118 
00120     bool operator==(const Attribute& rhs) const;
00122     bool operator==(const nString& rhs) const;
00124     bool operator==(int rhs) const;
00126     bool operator==(float rhs) const;
00128     bool operator==(bool rhs) const;
00130     bool operator==(const vector3& rhs) const;
00132     bool operator==(const vector4& rhs) const;
00133 
00135     bool operator!=(const Attribute& rhs) const;
00137     bool operator!=(const nString& rhs) const;
00139     bool operator!=(int rhs) const;
00141     bool operator!=(float rhs) const;
00143     bool operator!=(bool rhs) const;
00145     bool operator!=(const vector3& rhs) const;
00147     bool operator!=(const vector4& rhs) const;
00148 
00150     void SetString(const nString& s);
00152     void SetInt(int i);
00154     void SetFloat(float f);
00156     void SetBool(bool b);
00158     void SetVector3(const vector3& v);
00160     void SetVector4(const vector4& v);
00162     void SetMatrix44(const matrix44& m);
00164     void SetValue(const nVariant& val);
00165 
00167     nString GetString() const;
00169     int GetInt() const;
00171     float GetFloat() const;
00173     bool GetBool() const;
00175     const vector3& GetVector3() const;
00177     const vector4& GetVector4() const;
00179     const matrix44& GetMatrix44() const;
00181     const nVariant& GetValue() const;
00182 
00184     nString AsString() const;
00186     static Type StringToType(const nString& s);
00188     static nString TypeToString(Type t);
00189 
00190 private:
00192     void PutWriteError() const;
00193         
00194     Attr::AttributeID attrId;
00195     nVariant value;
00196     bool empty;
00197 };
00198 
00199 //------------------------------------------------------------------------------
00202 inline
00203 void
00204 Attribute::PutWriteError() const
00205 {
00206     n_error("Trying to write read-only attribute '%s'!", this->GetName().Get());
00207 }
00208 
00209 //------------------------------------------------------------------------------
00212 inline
00213 Attribute::Attribute() :
00214     attrId(),
00215     empty(true)
00216 {
00217     // empty
00218 }
00219 
00220 
00221 //------------------------------------------------------------------------------
00224 inline
00225 Attribute::Attribute(const Attribute& rhs) :
00226     attrId(rhs.attrId),
00227     empty(false),
00228     value(rhs.value)
00229 {
00230     // empty
00231 }
00232 
00233 //------------------------------------------------------------------------------
00236 inline
00237 Attribute::Attribute(const Attr::AttributeID& id) :
00238     attrId( id ),
00239     empty( true )
00240 {
00241     // empty
00242 }
00243 
00244 //------------------------------------------------------------------------------
00247 inline
00248 Attribute::Attribute(const Attr::VoidAttributeID& i) :
00249     attrId( i ),
00250     empty(true)
00251 {
00252     // empty
00253 }
00254 
00255 //------------------------------------------------------------------------------
00258 inline
00259 Attribute::Attribute( const Attr::StringAttributeID& i, const nString& v) :
00260     attrId( i ),
00261     empty(false),
00262     value(v.Get())
00263 {
00264     n_assert(this->GetType() == String);
00265 }
00266 
00267 //------------------------------------------------------------------------------
00270 inline
00271 Attribute::Attribute( const Attr::IntAttributeID& i, int v) :
00272     attrId( i ),
00273     empty(false),
00274     value(v)
00275 {
00276     n_assert(this->GetType() == Int);
00277 }
00278 
00279 //------------------------------------------------------------------------------
00282 inline
00283 Attribute::Attribute( const Attr::FloatAttributeID& i, float v) :
00284     attrId( i ),
00285     empty(false),
00286     value(v)
00287 {
00288     n_assert(this->GetType() == Float);
00289 }
00290 
00291 //------------------------------------------------------------------------------
00294 inline
00295 Attribute::Attribute( const Attr::BoolAttributeID& i, bool v) :
00296     attrId( i ),
00297     empty(false),
00298     value(v)
00299 {
00300     n_assert(this->GetType() == Bool);
00301 }
00302 
00303 //------------------------------------------------------------------------------
00306 inline
00307 Attribute::Attribute( const Attr::Vector3AttributeID& i, const vector3& v) :
00308     attrId( i ),
00309     empty(false),
00310     value(v)
00311 {
00312     n_assert(this->GetType() == Vector3);
00313 }
00314 
00315 //------------------------------------------------------------------------------
00318 inline
00319 Attribute::Attribute( const Attr::Vector4AttributeID& i, const vector4& v) :
00320     attrId( i ),
00321     empty(false),
00322     value(v)
00323 {
00324     n_assert(this->GetType() == Vector4);
00325 }
00326 
00327 //------------------------------------------------------------------------------
00330 inline
00331 Attribute::Attribute( const Attr::Matrix44AttributeID& i, const matrix44& m) :
00332     attrId( i ),
00333     empty(false),
00334     value(m)
00335 {
00336     n_assert(this->GetType() == Matrix44);
00337 }
00338 
00339 //------------------------------------------------------------------------------
00342 inline
00343 void
00344 Attribute::operator=(const Attribute& rhs)
00345 {
00346     this->attrId = rhs.attrId;
00347     this->empty = rhs.empty;
00348     this->value = rhs.value;
00349 }
00350 
00351 //------------------------------------------------------------------------------
00354 inline
00355 void
00356 Attribute::operator=(const nString& rhs)
00357 {
00358     n_assert(this->GetType() == String);
00359     this->value = rhs.Get();
00360     this->empty = false;
00361 }
00362 
00363 //------------------------------------------------------------------------------
00366 inline
00367 void
00368 Attribute::operator=(int rhs)
00369 {
00370     n_assert(this->GetType() == Int);
00371     this->value = rhs;
00372     this->empty = false;
00373 }
00374 
00375 //------------------------------------------------------------------------------
00378 inline
00379 void
00380 Attribute::operator=(float rhs)
00381 {
00382     n_assert(this->GetType() == Float);
00383     this->value = rhs;
00384     this->empty = false;
00385 }
00386 
00387 //------------------------------------------------------------------------------
00390 inline
00391 void
00392 Attribute::operator=(bool rhs)
00393 {
00394     n_assert(this->GetType() == Bool);
00395     this->value = rhs;
00396     this->empty = false;
00397 }
00398 
00399 //------------------------------------------------------------------------------
00402 inline
00403 void
00404 Attribute::operator=(const vector3& rhs)
00405 {
00406     n_assert(this->GetType() == Vector3);
00407     this->value = rhs;
00408     this->empty = false;
00409 }
00410 
00411 //------------------------------------------------------------------------------
00414 inline
00415 void
00416 Attribute::operator=(const vector4& rhs)
00417 {
00418     n_assert(this->GetType() == Vector4);
00419     this->value = rhs;
00420     this->empty = false;
00421 }
00422 
00423 //------------------------------------------------------------------------------
00426 inline
00427 void
00428 Attribute::operator=(const matrix44& rhs)
00429 {
00430     n_assert(this->GetType() == Matrix44);
00431     this->value = rhs;
00432     this->empty = false;
00433 }
00434 
00435 //------------------------------------------------------------------------------
00438 inline
00439 bool
00440 Attribute::operator==(const Attribute& rhs) const
00441 {
00442     return (this->attrId == rhs.attrId) && (this->value == rhs.value);
00443 }
00444 
00445 //------------------------------------------------------------------------------
00448 inline
00449 bool
00450 Attribute::operator==(const nString& rhs) const
00451 {
00452     n_assert(this->GetType() == String);
00453     return (this->value == rhs.Get());
00454 }
00455 
00456 //------------------------------------------------------------------------------
00459 inline
00460 bool
00461 Attribute::operator==(int rhs) const
00462 {
00463     n_assert(this->GetType() == Int);
00464     return (this->value == rhs);
00465 }
00466 
00467 //------------------------------------------------------------------------------
00470 inline
00471 bool
00472 Attribute::operator==(float rhs) const
00473 {
00474     n_assert(this->GetType() == Float);
00475     return (this->value == rhs);
00476 }
00477 
00478 //------------------------------------------------------------------------------
00481 inline
00482 bool
00483 Attribute::operator==(bool rhs) const
00484 {
00485     n_assert(this->GetType() == Bool);
00486     return (this->value == rhs);
00487 }
00488 
00489 //------------------------------------------------------------------------------
00492 inline
00493 bool
00494 Attribute::operator==(const vector3& rhs) const
00495 {
00496     n_assert(this->GetType() == Vector3);
00497     return (this->value == rhs);
00498 }
00499 
00500 //------------------------------------------------------------------------------
00503 inline
00504 bool
00505 Attribute::operator==(const vector4& rhs) const
00506 {
00507     n_assert(this->GetType() == Vector4);
00508     return (this->value == rhs);
00509 }
00510 
00511 //------------------------------------------------------------------------------
00514 inline
00515 bool
00516 Attribute::operator!=(const Attribute& rhs) const
00517 {
00518     return (this->attrId != rhs.attrId) || (this->value != rhs.value);
00519 }
00520 
00521 //------------------------------------------------------------------------------
00524 inline
00525 bool
00526 Attribute::operator!=(const nString& rhs) const
00527 {
00528     n_assert(this->GetType() == String);
00529     return (this->value != rhs.Get());
00530 }
00531 
00532 //------------------------------------------------------------------------------
00535 inline
00536 bool
00537 Attribute::operator!=(int rhs) const
00538 {
00539     n_assert(this->GetType() == Int);
00540     return (this->value != rhs);
00541 }
00542 
00543 //------------------------------------------------------------------------------
00546 inline
00547 bool
00548 Attribute::operator!=(float rhs) const
00549 {
00550     n_assert(this->GetType() == Float);
00551     return (this->value != rhs);
00552 }
00553 
00554 //------------------------------------------------------------------------------
00557 inline
00558 bool
00559 Attribute::operator!=(bool rhs) const
00560 {
00561     n_assert(this->GetType() == Bool);
00562     return (this->value != rhs);
00563 }
00564 
00565 //------------------------------------------------------------------------------
00568 inline
00569 bool
00570 Attribute::operator!=(const vector3& rhs) const
00571 {
00572     n_assert(this->GetType() == Vector3);
00573     return (this->value != rhs);
00574 }
00575 
00576 //------------------------------------------------------------------------------
00579 inline
00580 bool
00581 Attribute::operator!=(const vector4& rhs) const
00582 {
00583     n_assert(this->GetType() == Vector4);
00584     return (this->value != rhs);
00585 }
00586 
00587 //------------------------------------------------------------------------------
00590 inline
00591 void
00592 Attribute::SetAttributeID(const Attr::AttributeID& i)
00593 {
00594     this->attrId = i;
00595 }
00596 
00597 //------------------------------------------------------------------------------
00600 inline
00601 const Attr::AttributeID&
00602 Attribute::GetAttributeID() const
00603 {
00604     return this->attrId;
00605 }
00606 
00607 //------------------------------------------------------------------------------
00610 inline
00611 const nString&
00612 Attribute::GetName() const
00613 {
00614     n_assert2(this->attrId.IsValid(), "Valid attribute id");
00615     return this->attrId.GetName();
00616 }
00617 
00618 //------------------------------------------------------------------------------
00621 inline
00622 Attribute::Type
00623 Attribute::GetType() const
00624 {
00625     n_assert2(this->attrId.IsValid(), "Valid attribute id");
00626     return (Attribute::Type) this->attrId.GetType();
00627 }
00628 
00629 //------------------------------------------------------------------------------
00632 inline
00633 bool
00634 Attribute::IsWritable() const
00635 {
00636     n_assert2(this->attrId.IsValid(), "Valid attribute id");
00637     return this->attrId.IsWritable();
00638 }
00639 
00640 //------------------------------------------------------------------------------
00643 inline
00644 bool
00645 Attribute::IsStorable() const
00646 {
00647     n_assert2(this->attrId.IsValid(), "Valid attribute id");
00648     return this->attrId.IsStorable();
00649 }
00650 
00651 //------------------------------------------------------------------------------
00654 inline
00655 void
00656 Attribute::Clear()
00657 {
00658     this->empty = true;
00659 }
00660 
00661 //------------------------------------------------------------------------------
00664 inline
00665 bool
00666 Attribute::IsEmpty() const
00667 {
00668     return this->empty;
00669 }
00670 
00671 //------------------------------------------------------------------------------
00674 inline
00675 bool
00676 Attribute::IsValid() const
00677 {
00678     return this->attrId.IsValid();
00679 }
00680 
00681 //------------------------------------------------------------------------------
00684 inline
00685 void
00686 Attribute::SetString(const nString& s)
00687 {
00688     n_assert(this->GetType() == String);
00689     this->value = s.Get();
00690     this->empty = false;
00691 }
00692 
00693 //------------------------------------------------------------------------------
00696 inline
00697 void
00698 Attribute::SetInt(int i)
00699 {
00700     n_assert(this->GetType() == Int);
00701     this->value = i;
00702     this->empty = false;
00703 }
00704 
00705 //------------------------------------------------------------------------------
00708 inline
00709 void
00710 Attribute::SetFloat(float f)
00711 {
00712     n_assert(this->GetType() == Float);
00713     this->value = f;
00714     this->empty = false;
00715 }
00716 
00717 //------------------------------------------------------------------------------
00720 inline
00721 void
00722 Attribute::SetBool(bool b)
00723 {
00724     n_assert(this->GetType() == Bool);
00725     this->value = b;
00726     this->empty = false;
00727 }
00728 
00729 //------------------------------------------------------------------------------
00732 inline
00733 void
00734 Attribute::SetVector3(const vector3& v)
00735 {
00736     n_assert(this->GetType() == Vector3);
00737     this->value = v;
00738     this->empty = false;
00739 }
00740 
00741 //------------------------------------------------------------------------------
00744 inline
00745 void
00746 Attribute::SetVector4(const vector4& v)
00747 {
00748     n_assert(this->GetType() == Vector4);
00749     this->value = v;
00750     this->empty = false;
00751 }
00752 
00753 //------------------------------------------------------------------------------
00756 inline
00757 void
00758 Attribute::SetMatrix44(const matrix44& m)
00759 {
00760     n_assert(this->GetType() == Matrix44);
00761     this->value = m;
00762     this->empty = false;
00763 }
00764 
00765 //------------------------------------------------------------------------------
00768 inline
00769 void
00770 Attribute::SetValue(const nVariant& val)
00771 {
00772     this->value = val;
00773     this->empty = false;
00774 }
00775 
00776 //------------------------------------------------------------------------------
00779 inline
00780 nString
00781 Attribute::GetString() const
00782 {
00783     n_assert(!this->IsEmpty() && (this->GetType() == String));
00784     return nString(this->value.GetString());
00785 }
00786 
00787 //------------------------------------------------------------------------------
00790 inline
00791 int
00792 Attribute::GetInt() const
00793 {
00794     n_assert(!this->IsEmpty() && (this->GetType() == Int));
00795     return this->value.GetInt();
00796 }
00797 
00798 //------------------------------------------------------------------------------
00801 inline
00802 float
00803 Attribute::GetFloat() const
00804 {
00805     n_assert(!this->IsEmpty() && (this->GetType() == Float));
00806     return this->value.GetFloat();
00807 }
00808 
00809 //------------------------------------------------------------------------------
00812 inline
00813 bool
00814 Attribute::GetBool() const
00815 {
00816     n_assert(!this->IsEmpty() && (this->GetType() == Bool));
00817     return this->value.GetBool();
00818 }
00819 
00820 //------------------------------------------------------------------------------
00823 inline
00824 const vector3&
00825 Attribute::GetVector3() const
00826 {
00827     n_assert(!this->IsEmpty() && (this->GetType() == Vector3));
00828     return this->value.GetVector3();
00829 }
00830 
00831 //------------------------------------------------------------------------------
00834 inline
00835 const vector4&
00836 Attribute::GetVector4() const
00837 {
00838     n_assert(!this->IsEmpty() && (this->GetType() == Vector4));
00839     return this->value.GetVector4();
00840 }
00841 
00842 //------------------------------------------------------------------------------
00845 inline
00846 const matrix44&
00847 Attribute::GetMatrix44() const
00848 {
00849     n_assert(!this->IsEmpty() && (this->GetType() == Matrix44));
00850     return this->value.GetMatrix44();
00851 }
00852 
00853 //------------------------------------------------------------------------------
00856 inline
00857 const nVariant&
00858 Attribute::GetValue() const
00859 {
00860     n_assert(!this->IsEmpty());
00861     return this->value;
00862 }
00863 
00864 //------------------------------------------------------------------------------
00867 inline
00868 Attribute::Type
00869 Attribute::StringToType(const nString& s)
00870 {
00871     return (Attribute::Type) nVariant::StringToType(s.Get());
00872 }
00873 
00874 //------------------------------------------------------------------------------
00877 inline
00878 nString
00879 Attribute::TypeToString(Type t)
00880 {
00881     return nString(nVariant::TypeToString((nVariant::Type) t));
00882 }
00883 
00884 //------------------------------------------------------------------------------
00889 inline
00890 nString
00891 Attribute::AsString() const
00892 {
00893     n_assert(this->GetType() != Void);
00894     nString str;
00895     switch (this->GetType())
00896     {
00897         case Int:
00898             str.SetInt(this->GetInt());
00899             break;
00900 
00901         case Float:
00902             str.SetFloat(this->GetFloat());
00903             break;
00904 
00905         case Bool:
00906             str.SetBool(this->GetBool());
00907             break;
00908 
00909         case Vector3:
00910             str.SetVector3(this->GetVector3());
00911             break;
00912 
00913         case Vector4:
00914             str.SetVector4(this->GetVector4());
00915             break;
00916 
00917         case String:
00918             str = this->GetString();
00919             break;
00920 
00921         case Matrix44:
00922             str.SetMatrix44(this->GetMatrix44());
00923             break;
00924         case Void:
00925             break;
00926     }
00927     return str;
00928 }
00929 
00930 }; // namespace Db
00931 //------------------------------------------------------------------------------
00932 #endif

Copyright © 1999-2005 by the contributing authors. Ideas, requests, problems: Send feedback.