//========= Copyright Valve Corporation, All rights reserved. ============// // // Purpose: // //============================================================================= #ifndef DMATTRIBUTEVAR_H #define DMATTRIBUTEVAR_H #ifdef _WIN32 #pragma once #endif #include "Color.h" #include "datamodel/dmattribute.h" #include "datamodel/dmelement.h" #include "mathlib/vector.h" #include "mathlib/vector2d.h" #include "mathlib/vector4d.h" #include "mathlib/vmatrix.h" #include "tier1/utlvector.h" template class CDmeHandle; //----------------------------------------------------------------------------- // Specialization for color //----------------------------------------------------------------------------- class CDmaColor : public CDmaVar { public: // Set methods void SetColor(int r, int g, int b, int a = 0); void SetRed(int r); void SetGreen(int g); void SetBlue(int b); void SetAlpha(int a); // Sets the color as a 32-bit integer void SetRawColor(int color); // Get methods unsigned char r() const; unsigned char g() const; unsigned char b() const; unsigned char a() const; const unsigned char &operator[](int index) const; }; //----------------------------------------------------------------------------- // Specialization for object ids //----------------------------------------------------------------------------- class CDmaObjectId : public CDmaVar { public: void CreateObjectId(); void Invalidate(); bool IsValid() const; bool IsEqual(const DmObjectId_t &id) const; const DmObjectId_t &operator=(const DmObjectId_t &src); const CDmaObjectId &operator=(const CDmaObjectId &src); const DmObjectId_t &Set(const DmObjectId_t &src); }; //----------------------------------------------------------------------------- // Specialization for binary block //----------------------------------------------------------------------------- class CDmaBinaryBlock : public CDmaVar { public: void Get(void *pValue, int nMaxLen) const; void Set(const void *pValue, int nLen); const void *Get() const; const unsigned char &operator[](int i) const; // Returns buffer length int Length() const; }; //----------------------------------------------------------------------------- // Specialization for elements //----------------------------------------------------------------------------- template class CDmaElement : public CDmaVar { typedef CDmaVar BaseClass; public: // Used to initialize the attribute in an element's OnConstruction method void InitAndCreate(CDmElement *pOwner, const char *pAttributeName, const char *pElementName = NULL, int flags = 0); void Init(CDmElement *pOwner, const char *pAttributeName, int flags = 0); // Returns the type of elements allowed into this attribute. // UTL_INVAL_SYMBOL allows everything. UtlSymId_t GetElementType() const; // Get/set void Set(T *pElement); T *GetElement() const; // Cast T *operator->() const; operator T *() const; // NULL check bool operator!() const; // Assignment.. wish I knew how to un-inline these methods template CDmaElement &operator=(S *pElement) { Set(static_cast(pElement)); return *this; } template CDmaElement &operator=(const CDmaElement &src) { Set(static_cast(src.Get())); return *this; } template bool operator==(const CDmaElement &src) const { return Value() == src.Value(); } template bool operator!=(const CDmaElement &src) const { return Value() != src.Value(); } }; //----------------------------------------------------------------------------- // Can access any array attribute, regardless of type // See below for type-specific array accessors which have more features //----------------------------------------------------------------------------- class CDmrGenericArrayConst { public: CDmrGenericArrayConst(const CDmAttribute *pAttribute); CDmrGenericArrayConst(const CDmElement *pElement, const char *pAttributeName); // Array count int Count() const; // Gets const void *GetUntyped(int i) const; // String conversion const char *GetAsString(int i, char *pBuffer, size_t nBufLen) const; const CDmAttribute *GetAttribute() const; bool IsValid() const; protected: CDmrGenericArrayConst(); void Init(const CDmAttribute *pAttribute); void Init(const CDmElement *pElement, const char *pAttributeName); CDmAttribute *m_pAttribute; }; class CDmrGenericArray : public CDmrGenericArrayConst { public: CDmrGenericArray(CDmAttribute *pAttribute); CDmrGenericArray(CDmElement *pElement, const char *pAttributeName); void EnsureCount(int num); // Sets multiple elements at the same time int AddToTail(); void Remove(int elem); // preserves order, shifts elements void RemoveAll(); // doesn't deallocate memory void SetMultiple(int i, int nCount, DmAttributeType_t valueType, const void *pValue); void Set(int i, DmAttributeType_t valueType, const void *pValue); // String conversion void SetFromString(int i, const char *pValue); CDmAttribute *GetAttribute(); const CDmAttribute *GetAttribute() const; }; //----------------------------------------------------------------------------- // Helper template for external array attribute vars // NOTE: To use this class, don't use CDmaArrayBase directly. Instead, use // CDmaArray var; <- Instantiate an array attribute var as a //member of a element class CDmrArray var; <- Used to reference an existing //array attribute + read/modify it CDmrArrayConst var; <- Used to //reference an existing array attribute + read it (no modify) // // Also, there is a CDmaStringArray/CDmrStringArray/CDmrStringArrayConst for // strings and a CDmaElementArray/CDmrElementArray/CDmrElementArrayConst for // elements //----------------------------------------------------------------------------- template class CDmaArrayConstBase : public B { public: // Accessors const CUtlVector &Get() const; const T *Base() const; // Iteration int Count() const; const T &operator[](int i) const; const T &Element(int i) const; const T &Get(int i) const; const void *GetUntyped(int i) const; bool IsValidIndex(int i) const; int InvalidIndex(void) const; // Search int Find(const T &value) const; // Attribute-related methods const CDmAttribute *GetAttribute() const; CDmElement *GetOwner(); bool IsDirty() const; protected: CDmaArrayConstBase(); CDmAttribute *m_pAttribute; }; template class CDmaArrayBase : public CDmaArrayConstBase { public: // Insertion int AddToTail(); int InsertBefore(int elem); int AddToTail(const T &src); int InsertBefore(int elem, const T &src); int AddMultipleToTail(int num); int InsertMultipleBefore(int elem, int num); void EnsureCount(int num); // Element Modification void Set(int i, const T &value); void SetMultiple(int i, int nCount, const T *pValue); void Swap(int i, int j); // Copy related methods void CopyArray(const T *pArray, int size); // this is basically just a faster version of CopyArray which uses pointer // swap NOTE: This doesn't work for element arrays void SwapArray(CUtlVector &array); // Removal void FastRemove(int elem); void Remove(int elem); void RemoveMultiple(int elem, int num); void RemoveAll(); // Memory management void EnsureCapacity(int num); void Purge(); // Attribute-related methods CDmAttribute *GetAttribute(); const CDmAttribute *GetAttribute() const; }; //----------------------------------------------------------------------------- // Specialization for string arrays // NOTE: To use this class, don't use CDmaStringArrayBase directly. Instead, use // CDmaStringArray var; <- Instantiate an array attribute var as //a member of a element class CDmrStringArray var; <- Used to reference an //existing array attribute + read/modify it CDmrStringArrayConst var; <- Used to //reference an existing array attribute + read it (no modify) //----------------------------------------------------------------------------- template class CDmaStringArrayConstBase : public BaseClass { public: const char *operator[](int i) const; const char *Element(int i) const; const char *Get(int i) const; const CUtlVector &Get() const; // Returns strlen of element i int Length(int i) const; }; template class CDmaStringArrayBase : public CDmaStringArrayConstBase > { typedef CDmaStringArrayConstBase > BaseClass; public: // Sets an element in the array void Set(int i, const char *pValue); // Adds an element, uses copy constructor int AddToTail(const char *pValue); int InsertBefore(int elem, const char *pValue); }; //----------------------------------------------------------------------------- // Specialization for elements // NOTE: To use this class, don't use CDmaElementArrayBase directly. Instead, // use // CDmaElementArray< element_type > var; <- Instantiate an array //attribute var as a member of a element class CDmrElementArray< element_type > //var; <- Used to reference an existing array attribute + read/modify it // CDmrElementArrayConst< element_type > var; <- Used to reference an //existing array attribute + read it (no modify) //----------------------------------------------------------------------------- template class CDmaElementArrayConstBase : public BaseClass { public: // Returns the element type UtlSymId_t GetElementType() const; // Array access E *operator[](int i) const; E *Element(int i) const; E *Get(int i) const; const DmElementHandle_t &GetHandle(int i) const; const CUtlVector &Get() const; // Search int Find(const E *pValue) const; int Find(DmElementHandle_t h) const; }; template class CDmaElementArrayBase : public CDmaElementArrayConstBase > { typedef CDmaElementArrayConstBase > BaseClass; public: void SetHandle(int i, DmElementHandle_t h); void Set(int i, E *pElement); // Insertion int AddToTail(); int AddToTail(DmElementHandle_t src); int AddToTail(E *pValue); int InsertBefore(int elem); int InsertBefore(int elem, DmElementHandle_t src); int InsertBefore(int elem, E *pValue); template int AddToTail(const CDmeHandle &value) { return BaseClass::AddToTail(value.GetHandle()); } template int InsertBefore(int elem, const CDmeHandle &value) { return BaseClass::InsertBefore(elem, value.GetHandle()); } }; // NOTE: The next couple classes are implementation details used to create // CDmrAray/CDmaArray //----------------------------------------------------------------------------- // Base classes that contain data or refer to it; used for array accessor // classes //----------------------------------------------------------------------------- template class CDmaDataInternal { protected: typedef typename CDmAttributeInfo::StorageType_t D; const T &Value() const { return m_Storage; } T &Value() { return m_Storage; } const D &Data() const { return m_Storage; } D &Data() { return m_Storage; } private: D m_Storage; }; template class CDmaDataExternal { protected: typedef typename CDmAttributeInfo::StorageType_t D; CDmaDataExternal() : m_pStorage(0) {} void Attach(void *pData) { m_pStorage = (D *)pData; } const T &Value() const { return *m_pStorage; } T &Value() { return *m_pStorage; } const D &Data() const { return *m_pStorage; } D &Data() { return *m_pStorage; } private: D *m_pStorage; }; //----------------------------------------------------------------------------- // Versions for access, or for attribute vars //----------------------------------------------------------------------------- template class CDmaDecorator : public B { public: void Init(CDmElement *pOwner, const char *pAttributeName, int flags = 0); }; template class CDmrDecoratorConst : public BaseClass { public: void Init(const CDmAttribute *pAttribute); void Init(const CDmElement *pElement, const char *pAttributeName); bool IsValid() const; }; template class CDmrDecorator : public BaseClass { public: void Init(CDmAttribute *pAttribute); void Init(CDmElement *pElement, const char *pAttributeName, bool bAddAttribute = false); bool IsValid() const; }; #define DECLARE_ATTRIBUTE_ARRAY_VARIABLE(_className, _elementType) \ public: \ _className() {} #define DECLARE_ATTRIBUTE_ARRAY_REFERENCE(_className, _elementType) \ public: \ _className() {} \ _className(CDmAttribute *pAttribute) { BaseClass::Init(pAttribute); } \ _className(CDmElement *pElement, const char *pAttributeName, \ bool bAddAttribute = false) { \ BaseClass::Init(pElement, pAttributeName, bAddAttribute); \ } \ _className(CDmaArray<_className> &var) { \ BaseClass::Init(var.GetAttribute()); \ } \ _className(CDmrArray<_className> &var) { \ BaseClass::Init(var.GetAttribute()); \ } #define DECLARE_ATTRIBUTE_ARRAY_CONST_REFERENCE(_className, _elementType) \ public: \ _className() {} \ _className(const CDmAttribute *pAttribute) { \ BaseClass::Init(pAttribute); \ } \ _className(const CDmElement *pElement, const char *pAttributeName) { \ BaseClass::Init(pElement, pAttributeName); \ } \ _className(const CDmaArray<_className> &var) { \ BaseClass::Init(var.GetAttribute()); \ } \ _className(const CDmrArrayConst<_className> &var) { \ BaseClass::Init(var.GetAttribute()); \ } \ _className(const CDmrArray<_className> &var) { \ BaseClass::Init(var.GetAttribute()); \ } template class CDmrArray; template class CDmrArrayConst; template class CDmaArray; //----------------------------------------------------------------------------- // Versions for access, or for attribute vars //----------------------------------------------------------------------------- template class CDmaArray : public CDmaDecorator< T, CDmaArrayBase > > > { DECLARE_ATTRIBUTE_ARRAY_VARIABLE(CDmaArray, T); public: const CDmaArray &operator=(const CDmaArray &val) { CopyArray(val.Base(), val.Count()); return *this; } template const CDmaArray &operator=(const C &val) { CopyArray(val.Base(), val.Count()); return *this; } private: CDmaArray(const CDmaArray &array) {} }; template class CDmrArrayConst : public CDmrDecoratorConst< T, CDmaArrayConstBase > > > { typedef CDmrDecoratorConst< T, CDmaArrayConstBase > > > BaseClass; DECLARE_ATTRIBUTE_ARRAY_CONST_REFERENCE(CDmrArrayConst, T); }; template class CDmrArray : public CDmrDecorator< T, CDmaArrayBase > > > { typedef CDmrDecorator > > > BaseClass; DECLARE_ATTRIBUTE_ARRAY_REFERENCE(CDmrArray, T); public: const CDmrArray &operator=(const CDmrArray &val) { CopyArray(val.Base(), val.Count()); return *this; } template const CDmrArray &operator=(const C &val) { CopyArray(val.Base(), val.Count()); return *this; } }; class CDmrStringArray; class CDmaStringArray : public CDmaDecorator< CUtlString, CDmaStringArrayBase > > > { DECLARE_ATTRIBUTE_ARRAY_VARIABLE(CDmaStringArray, CUtlString); public: const CDmaStringArray &operator=(const CDmaStringArray &val) { CopyArray(val.Base(), val.Count()); return *this; } template const CDmaStringArray &operator=(const C &val) { CopyArray(val.Base(), val.Count()); return *this; } private: CDmaStringArray(const CDmaStringArray &array) {} }; class CDmrStringArray : public CDmrDecorator< CUtlString, CDmaStringArrayBase > > > { typedef CDmrDecorator< CUtlString, CDmaStringArrayBase > > > BaseClass; DECLARE_ATTRIBUTE_ARRAY_REFERENCE(CDmrStringArray, CUtlString); public: CDmrStringArray(CDmaStringArray &var) { Init(var.GetAttribute()); } CDmrStringArray(CDmrStringArray &var) { Init(var.GetAttribute()); } const CDmrStringArray &operator=(const CDmrStringArray &val) { CopyArray(val.Base(), val.Count()); return *this; } template const CDmrStringArray &operator=(const C &val) { CopyArray(val.Base(), val.Count()); return *this; } }; class CDmrStringArrayConst : public CDmrDecoratorConst< CUtlString, CDmaStringArrayConstBase > > > > { typedef CDmrDecoratorConst< CUtlString, CDmaStringArrayConstBase > > > > BaseClass; DECLARE_ATTRIBUTE_ARRAY_CONST_REFERENCE(CDmrStringArrayConst, CUtlString); public: CDmrStringArrayConst(const CDmaStringArray &var) { Init(var.GetAttribute()); } CDmrStringArrayConst(const CDmrStringArray &var) { Init(var.GetAttribute()); } CDmrStringArrayConst(const CDmrStringArrayConst &var) { Init(var.GetAttribute()); } }; //----------------------------------------------------------------------------- // Prevent CDmaArray for DmElementHandle_t //----------------------------------------------------------------------------- template <> class CDmaArray { private: CDmaArray(); }; template class CDmrElementArray; template class CDmaElementArray : public CDmaElementArrayBase< E, CDmaDataInternal > > { DECLARE_ATTRIBUTE_ARRAY_VARIABLE(CDmaElementArray, DmElementHandle_t); public: void Init(CDmElement *pOwner, const char *pAttributeName, int flags = 0) { Assert(pOwner); this->m_pAttribute = pOwner->AddExternalAttribute( pAttributeName, AT_ELEMENT_ARRAY, &CDmaElementArrayBase< E, CDmaDataInternal > >::Value()); this->m_pAttribute->SetElementTypeSymbol(E::GetStaticTypeSymbol()); if (flags) { this->m_pAttribute->AddFlag(flags); } } template CDmaElementArray &operator=(const C &val) { CopyArray(val.Base(), val.Count()); return *this; } // NOTE: The copy operator= must be defined in addition to the generic one const CDmaElementArray &operator=(const CDmaElementArray &val) { CopyArray(val.Base(), val.Count()); return *this; } private: template CDmaElementArray(const CDmaElementArray &var); }; template class CDmrElementArrayConst : public CDmaElementArrayConstBase< E, CDmaArrayConstBase< DmElementHandle_t, CDmaDataExternal > > > { public: CDmrElementArrayConst() { this->m_pAttribute = NULL; } CDmrElementArrayConst(const CDmAttribute *pAttribute) { Init(pAttribute); } CDmrElementArrayConst(const CDmElement *pElement, const char *pAttributeName) { Init(pElement, pAttributeName); } template CDmrElementArrayConst(const CDmaElementArray &var) { Init(var.GetAttribute()); } template CDmrElementArrayConst(const CDmrElementArray &var) { Init(var.GetAttribute()); } template CDmrElementArrayConst(const CDmrElementArrayConst &var) { Init(var.GetAttribute()); } void Init(const CDmAttribute *pAttribute) { if (pAttribute && pAttribute->GetType() == AT_ELEMENT_ARRAY) { this->m_pAttribute = const_cast(pAttribute); this->Attach(this->m_pAttribute->GetAttributeData()); } else { this->m_pAttribute = NULL; this->Attach(NULL); } } void Init(const CDmElement *pElement, const char *pAttributeName) { const CDmAttribute *pAttribute = NULL; if (pElement && pAttributeName && pAttributeName[0]) { pAttribute = (CDmAttribute *)pElement->GetAttribute(pAttributeName); } Init(pAttribute); } bool IsValid() const { return this->m_pAttribute != NULL; } }; template class CDmrElementArray : public CDmaElementArrayBase< T, CDmaDataExternal > > { public: CDmrElementArray() { this->m_pAttribute = NULL; } CDmrElementArray(CDmAttribute *pAttribute) { Init(pAttribute); } CDmrElementArray(CDmElement *pElement, const char *pAttributeName, bool bAddAttribute = false) { Init(pElement, pAttributeName, bAddAttribute); } template CDmrElementArray(CDmaElementArray &var) { Init(var.GetAttribute()); } template CDmrElementArray(CDmrElementArray &var) { Init(var.GetAttribute()); } void Init(CDmAttribute *pAttribute) { if (pAttribute && pAttribute->GetType() == AT_ELEMENT_ARRAY) { this->m_pAttribute = pAttribute; this->Attach(this->m_pAttribute->GetAttributeData()); } else { this->m_pAttribute = NULL; this->Attach(NULL); } } void Init(CDmElement *pElement, const char *pAttributeName, bool bAddAttribute = false) { CDmAttribute *pAttribute = NULL; if (pElement && pAttributeName && pAttributeName[0]) { pAttribute = pElement->GetAttribute(pAttributeName); if (bAddAttribute && !pAttribute) { pAttribute = pElement->CreateAttribute(pAttributeName, AT_ELEMENT_ARRAY); // FIXME: Should we do this? pAttribute->SetElementTypeSymbol(T::GetStaticTypeSymbol()); } } Init(pAttribute); } bool IsValid() const { return this->m_pAttribute != NULL; } template CDmrElementArray &operator=(const C &val) { CopyArray(val.Base(), val.Count()); return *this; } // NOTE: The copy operator= must be defined in addition to the generic one const CDmrElementArray &operator=(const CDmrElementArray &val) { CopyArray(val.Base(), val.Count()); return *this; } }; //----------------------------------------------------------------------------- // // Inline methods for CDmaVar // //----------------------------------------------------------------------------- template inline CDmaVar::CDmaVar() { m_pAttribute = NULL; CDmAttributeInfo::SetDefaultValue(m_Storage); } template inline void CDmaVar::Init(CDmElement *pOwner, const char *pAttributeName, int flags) { Assert(pOwner); m_pAttribute = pOwner->AddExternalAttribute( pAttributeName, CDmAttributeInfo::AttributeType(), &m_Storage); Assert(m_pAttribute); if (flags) { m_pAttribute->AddFlag(flags); } } template inline void CDmaVar::InitAndSet(CDmElement *pOwner, const char *pAttributeName, const T &value, int flags) { Init(pOwner, pAttributeName); Set(value); // this has to happen AFTER set so the set happens before FATTRIB_READONLY if (flags) { m_pAttribute->AddFlag(flags); } } template inline const T &CDmaVar::Set(const T &val) { Assert(m_pAttribute); m_pAttribute->SetValue(val); return m_Storage; } template inline const T &CDmaVar::operator=(const T &val) { return Set(val); } template inline const CDmaVar &CDmaVar::operator=(const CDmaVar &src) { Set(src.Get()); return *this; } template inline const T &CDmaVar::operator+=(const T &val) { return Set(Value() + val); } template inline const T &CDmaVar::operator-=(const T &val) { return Set(Value() - val); } template inline const T &CDmaVar::operator/=(const T &val) { return Set(Value() / val); } template inline const T &CDmaVar::operator*=(const T &val) { return Set(Value() * val); } template inline const T &CDmaVar::operator^=(const T &val) { return Set(Value() ^ val); } template inline const T &CDmaVar::operator|=(const T &val) { return Set(Value() | val); } template inline const T &CDmaVar::operator&=(const T &val) { return Set(Value() & val); } template inline T CDmaVar::operator++() { return Set(Value() + 1); } template inline T CDmaVar::operator--() { return Set(Value() - 1); } template inline T CDmaVar::operator++(int) // postfix version.. { T oldValue = Value(); Set(Value() + 1); return oldValue; } template inline T CDmaVar::operator--(int) // postfix version.. { T oldValue = Value(); Set(Value() - 1); return oldValue; } template inline CDmaVar::operator const T &() const { return Value(); } template inline const T &CDmaVar::Get() const { return Value(); } template inline const T *CDmaVar::operator->() const { return &Value(); } template inline CDmAttribute *CDmaVar::GetAttribute() { Assert(m_pAttribute); return m_pAttribute; } template inline const CDmAttribute *CDmaVar::GetAttribute() const { Assert(m_pAttribute); return m_pAttribute; } template inline bool CDmaVar::IsDirty() const { Assert(m_pAttribute); return m_pAttribute->IsFlagSet(FATTRIB_DIRTY); } template inline const T &CDmaVar::Value() const { return m_Storage; } template inline T &CDmaVar::Value() { return m_Storage; } template <> inline const DmElementHandle_t &CDmaVar::Value() const { return m_Storage.m_Handle; } template <> inline DmElementHandle_t &CDmaVar::Value() { return m_Storage.m_Handle; } template inline const typename CDmaVar::D &CDmaVar::Storage() const { return m_Storage; } template inline typename CDmaVar::D &CDmaVar::Storage() { return m_Storage; } //----------------------------------------------------------------------------- // // Inline methods for CDmaColor // //----------------------------------------------------------------------------- inline void CDmaColor::SetColor(int r, int g, int b, int a) { Color clr(r, g, b, a); m_pAttribute->SetValue(clr); } inline void CDmaColor::SetRed(int r) { Color org = Value(); org[0] = r; m_pAttribute->SetValue(org); } inline void CDmaColor::SetGreen(int g) { Color org = Value(); org[1] = g; m_pAttribute->SetValue(org); } inline void CDmaColor::SetBlue(int b) { Color org = Value(); org[2] = b; m_pAttribute->SetValue(org); } inline void CDmaColor::SetAlpha(int a) { Color org = Value(); org[3] = a; m_pAttribute->SetValue(org); } inline unsigned char CDmaColor::r() const { return (unsigned char)Value().r(); } inline unsigned char CDmaColor::g() const { return (unsigned char)Value().g(); } inline unsigned char CDmaColor::b() const { return (unsigned char)Value().b(); } inline unsigned char CDmaColor::a() const { return (unsigned char)Value().a(); } inline const unsigned char &CDmaColor::operator[](int index) const { return Value()[index]; } inline void CDmaColor::SetRawColor(int color) { Color clr; clr.SetRawColor(color); m_pAttribute->SetValue(clr); } //----------------------------------------------------------------------------- // // Inline methods for CDmaObjectId // //----------------------------------------------------------------------------- inline void CDmaObjectId::CreateObjectId() { DmObjectId_t id; CreateUniqueId(&id); m_pAttribute->SetValue(id); } inline void CDmaObjectId::Invalidate() { DmObjectId_t id; InvalidateUniqueId(&id); m_pAttribute->SetValue(id); } inline bool CDmaObjectId::IsValid() const { return IsUniqueIdValid(Value()); } inline bool CDmaObjectId::IsEqual(const DmObjectId_t &id) const { return IsUniqueIdEqual(Value(), id); } inline const DmObjectId_t &CDmaObjectId::operator=(const DmObjectId_t &src) { m_pAttribute->SetValue(src); return Value(); } inline const CDmaObjectId &CDmaObjectId::operator=(const CDmaObjectId &src) { m_pAttribute->SetValue(src.Get()); return *this; } inline const DmObjectId_t &CDmaObjectId::Set(const DmObjectId_t &src) { m_pAttribute->SetValue(src); return Value(); } //----------------------------------------------------------------------------- // // Inline methods for CDmaString // //----------------------------------------------------------------------------- inline const char *CDmaString::Get() const { return Value().Get(); } inline CDmaString::operator const char *() const { return Value().Get(); } inline void CDmaString::Set(const char *pValue) { CUtlString str(pValue, pValue ? Q_strlen(pValue) + 1 : 0); m_pAttribute->SetValue(str); } // Returns strlen inline int CDmaString::Length() const { return Value().Length(); } inline CDmaString &CDmaString::operator=(const char *src) { Set(src); return *this; } inline const CDmaString &CDmaString::operator=(const CDmaString &src) { Set(src.Get()); return *this; } //----------------------------------------------------------------------------- // // Inline methods for CDmaBinaryBlock // //----------------------------------------------------------------------------- inline void CDmaBinaryBlock::Get(void *pValue, int nMaxLen) const { Value().Get(pValue, nMaxLen); } inline void CDmaBinaryBlock::Set(const void *pValue, int nLen) { CUtlBinaryBlock block(pValue, nLen); m_pAttribute->SetValue(block); } inline const void *CDmaBinaryBlock::Get() const { return Value().Get(); } inline const unsigned char &CDmaBinaryBlock::operator[](int i) const { return Value()[i]; } inline int CDmaBinaryBlock::Length() const { return Value().Length(); } //----------------------------------------------------------------------------- // // Inline methods for CDmaElement // //----------------------------------------------------------------------------- template inline void CDmaElement::InitAndCreate(CDmElement *pOwner, const char *pAttributeName, const char *pElementName, int flags) { Init(pOwner, pAttributeName); DmElementHandle_t hElement = DMELEMENT_HANDLE_INVALID; if (!g_pDataModel->IsUnserializing()) { hElement = g_pDataModel->CreateElement( T::GetStaticTypeSymbol(), pElementName, pOwner->GetFileId()); } Assert(m_pAttribute); m_pAttribute->SetValue(hElement); // this has to happen AFTER set so the set happens before FATTRIB_READONLY m_pAttribute->AddFlag(flags | FATTRIB_MUSTCOPY); } template inline void CDmaElement::Init(CDmElement *pOwner, const char *pAttributeName, int flags) { BaseClass::Init(pOwner, pAttributeName); Assert(m_pAttribute); m_pAttribute->SetElementTypeSymbol(T::GetStaticTypeSymbol()); if (flags) { m_pAttribute->AddFlag(flags); } } template inline UtlSymId_t CDmaElement::GetElementType() const { return this->Data().m_ElementType; } template inline T *CDmaElement::GetElement() const { CDmElement *pElement = g_pDataModel->GetElement(Value()); Assert(!pElement || pElement->IsA(T::GetStaticTypeSymbol())); return static_cast(pElement); } template inline T *CDmaElement::operator->() const { return GetElement(); } template inline CDmaElement::operator T *() const { return GetElement(); } template inline void CDmaElement::Set(T *pElement) { Assert(m_pAttribute); m_pAttribute->SetValue(pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID); } template inline bool CDmaElement::operator!() const { return (GetElement() == NULL); } //----------------------------------------------------------------------------- // // Inline methods for CDmaArrayBase // //----------------------------------------------------------------------------- template inline const CUtlVector &CDmaArrayConstBase::Get() const { return this->Value(); } template inline const T *CDmaArrayConstBase::Base() const { return this->Value().Base(); } template inline const T &CDmaArrayConstBase::operator[](int i) const { return this->Value()[i]; } template const T &CDmaArrayConstBase::Element(int i) const { return this->Value()[i]; } template inline const T &CDmaArrayConstBase::Get(int i) const { return this->Value()[i]; } template const void *CDmaArrayConstBase::GetUntyped(int i) const { return &(this->Value()[i]); } template inline int CDmaArrayConstBase::Count() const { return this->Value().Count(); } template inline bool CDmaArrayConstBase::IsValidIndex(int i) const { return this->Value().IsValidIndex(i); } template inline int CDmaArrayConstBase::InvalidIndex(void) const { return this->Value().InvalidIndex(); } template inline const CDmAttribute *CDmaArrayConstBase::GetAttribute() const { Assert(m_pAttribute); return m_pAttribute; } template inline CDmElement *CDmaArrayConstBase::GetOwner() { return m_pAttribute->GetOwner(); } template inline bool CDmaArrayConstBase::IsDirty() const { return m_pAttribute->IsFlagSet(FATTRIB_DIRTY); } template inline CDmAttribute *CDmaArrayBase::GetAttribute() { Assert(this->m_pAttribute); return this->m_pAttribute; } template inline const CDmAttribute *CDmaArrayBase::GetAttribute() const { Assert(this->m_pAttribute); return this->m_pAttribute; } //----------------------------------------------------------------------------- // // Inline methods for CDmaStringArrayBase // //----------------------------------------------------------------------------- template inline const char *CDmaStringArrayConstBase::operator[](int i) const { return this->Value()[i].Get(); } template inline const char *CDmaStringArrayConstBase::Element(int i) const { return this->Value()[i].Get(); } template inline const char *CDmaStringArrayConstBase::Get(int i) const { return this->Value()[i].Get(); } template inline const CUtlVector &CDmaStringArrayConstBase::Get() const { return this->Value(); } // Returns strlen of element i template inline int CDmaStringArrayConstBase::Length(int i) const { return this->Value()[i].Length(); } template inline void CDmaStringArrayBase::Set(int i, const char *pValue) { CUtlString str(pValue, Q_strlen(pValue) + 1); BaseClass::Set(i, str); } // Adds an element, uses copy constructor template inline int CDmaStringArrayBase::AddToTail(const char *pValue) { CUtlString str(pValue, Q_strlen(pValue) + 1); return BaseClass::AddToTail(str); } template inline int CDmaStringArrayBase::InsertBefore(int elem, const char *pValue) { CUtlString str(pValue, Q_strlen(pValue) + 1); return BaseClass::InsertBefore(elem, str); } //----------------------------------------------------------------------------- // // Inline methods for CDmaElementArrayBase // //----------------------------------------------------------------------------- template inline UtlSymId_t CDmaElementArrayConstBase::GetElementType() const { return this->Data().m_ElementType; } template inline E *CDmaElementArrayConstBase::operator[](int i) const { return GetElement(this->Value()[i]); } template inline E *CDmaElementArrayConstBase::Element(int i) const { return GetElement(this->Value()[i]); } template inline E *CDmaElementArrayConstBase::Get(int i) const { return GetElement(this->Value()[i]); } template inline const DmElementHandle_t &CDmaElementArrayConstBase::GetHandle( int i) const { return this->Value()[i]; } template inline const CUtlVector &CDmaElementArrayConstBase::Get() const { return this->Value(); } // Search template inline int CDmaElementArrayConstBase::Find(const E *pValue) const { if (!pValue) return -1; return B::Find(pValue->GetHandle()); } template inline int CDmaElementArrayConstBase::Find(DmElementHandle_t h) const { return B::Find(h); } template inline void CDmaElementArrayBase::SetHandle(int i, DmElementHandle_t h) { BaseClass::Set(i, h); } template inline void CDmaElementArrayBase::Set(int i, E *pElement) { BaseClass::Set(i, pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID); } // Adds an element, uses copy constructor template inline int CDmaElementArrayBase::AddToTail() { return BaseClass::AddToTail(); } template inline int CDmaElementArrayBase::AddToTail(E *pValue) { return BaseClass::AddToTail(pValue ? pValue->GetHandle() : DMELEMENT_HANDLE_INVALID); } template inline int CDmaElementArrayBase::AddToTail(DmElementHandle_t src) { return BaseClass::AddToTail(src); } template inline int CDmaElementArrayBase::InsertBefore(int elem) { return BaseClass::InsertBefore(elem); } template inline int CDmaElementArrayBase::InsertBefore(int elem, E *pValue) { return BaseClass::InsertBefore( elem, pValue ? pValue->GetHandle() : DMELEMENT_HANDLE_INVALID); } template inline int CDmaElementArrayBase::InsertBefore(int elem, DmElementHandle_t src) { return BaseClass::InsertBefore(elem, src); } //----------------------------------------------------------------------------- // // Inline methods for CDmrGenericArray // //----------------------------------------------------------------------------- inline const CDmAttribute *CDmrGenericArrayConst::GetAttribute() const { Assert(m_pAttribute); return m_pAttribute; } inline bool CDmrGenericArrayConst::IsValid() const { return m_pAttribute != NULL; } inline CDmAttribute *CDmrGenericArray::GetAttribute() { Assert(m_pAttribute); return m_pAttribute; } inline const CDmAttribute *CDmrGenericArray::GetAttribute() const { Assert(m_pAttribute); return m_pAttribute; } #endif // DMATTRIBUTEVAR_H