DescriptionScope.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _DescriptionScope_hxx_
00023 #define _DescriptionScope_hxx_
00024
00025 #include "DescriptionAttributes.hxx"
00026 #include <map>
00027 #include <vector>
00028
00029 namespace CLAM
00030 {
00045 class SchemaError : public Err
00046 {
00047 public:
00048 SchemaError(const std::string & msg) : Err(msg.c_str()) {}
00049 };
00050
00051 class DescriptionScope
00052 {
00053 public:
00054 typedef std::map<std::string, unsigned> NamesMap;
00055 typedef std::vector<AbstractAttribute *> Attributes;
00056 private:
00057 NamesMap _nameMap;
00058 Attributes _attributes;
00059 std::string _scopeName;
00060 public:
00061 DescriptionScope(const std::string & name) : _scopeName(name) {}
00062 ~DescriptionScope();
00063
00065 const std::string & GetName() const
00066 {
00067 return _scopeName;
00068 }
00069
00076 template <typename AttributeType>
00077 void Add(const std::string & name)
00078 {
00079 unsigned attributeIndex = _nameMap.size();
00080 bool inserted =
00081 _nameMap.insert(std::make_pair(name,attributeIndex)).second;
00082 CLAM_ASSERT(inserted,("Couldn't add attribute '"+_scopeName+":"+name+"', already present in the scope").c_str());
00083 _attributes.push_back(new Attribute<AttributeType>(_scopeName, name));
00084 }
00085
00092 unsigned GetIndex(const std::string & name) const
00093 {
00094 NamesMap::const_iterator it = _nameMap.find(name);
00095 CLAM_ASSERT(it!=_nameMap.end(),
00096 (std::string()+"Accessing an unexisting attribute '"+_scopeName+"':'"+name+"'").c_str());
00097 return it->second;
00098 }
00103 unsigned GetIndexSafe(const std::string & name) const
00104 {
00105 NamesMap::const_iterator it = _nameMap.find(name);
00106 if (it==_nameMap.end())
00107 throw SchemaError("Accessing an unexisting attribute '"+_scopeName+"':'"+name+"'");
00108 return it->second;
00109 }
00110
00111 unsigned GetNAttributes() const
00112 {
00113 return _nameMap.size();
00114 }
00115
00116 void * Allocate(unsigned attribute, unsigned size) const
00117 {
00118 return _attributes[attribute]->Allocate(size);
00119 }
00120 void Deallocate(unsigned attribute, void * buffer) const
00121 {
00122 _attributes[attribute]->Deallocate(buffer);
00123 }
00124
00125 template <typename AttributeType>
00126 void CheckType(unsigned attributeIndex, AttributeType *) const
00127 {
00128 _attributes[attributeIndex]->CheckType<AttributeType>();
00129 }
00130
00131 const std::string & GetAttributeName(unsigned attributeIndex) const
00132 {
00133 CLAM_ASSERT(attributeIndex<_attributes.size(),
00134 "GetAttributeName: Using a wrong index to look up an attribute name");
00135 AbstractAttribute * attribute = _attributes[attributeIndex];
00136 return attribute->GetName();
00137 }
00138 const AbstractAttribute & GetAttribute(unsigned int attribute) const
00139 {
00140 return * _attributes[attribute];
00141 }
00142 };
00143
00144 }
00145
00146
00147 #endif// _DescriptionScope_hxx_
00148