CLAM-Development  1.4.0
DescriptionScope.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef _DescriptionScope_hxx_
23 #define _DescriptionScope_hxx_
24 
26 #include <map>
27 #include <vector>
28 
29 namespace CLAM
30 {
45  class SchemaError : public Err
46  {
47  public:
48  SchemaError(const std::string & msg) : Err(msg.c_str()) {}
49  };
50 
52  {
53  public:
54  typedef std::map<std::string, unsigned> NamesMap;
55  typedef std::vector<AbstractAttribute *> Attributes;
56  private:
57  NamesMap _nameMap;
58  Attributes _attributes;
59  std::string _scopeName;
60  public:
61  DescriptionScope(const std::string & name) : _scopeName(name) {}
63 
65  const std::string & GetName() const
66  {
67  return _scopeName;
68  }
69 
76  template <typename AttributeType>
77  void Add(const std::string & name)
78  {
79  unsigned attributeIndex = _nameMap.size();
80  bool inserted =
81  _nameMap.insert(std::make_pair(name,attributeIndex)).second;
82  CLAM_ASSERT(inserted,("Couldn't add attribute '"+_scopeName+":"+name+"', already present in the scope").c_str());
83  _attributes.push_back(new Attribute<AttributeType>(_scopeName, name));
84  }
85 
92  unsigned GetIndex(const std::string & name) const
93  {
94  NamesMap::const_iterator it = _nameMap.find(name);
95  CLAM_ASSERT(it!=_nameMap.end(),
96  (std::string()+"Accessing an unexisting attribute '"+_scopeName+"':'"+name+"'").c_str());
97  return it->second;
98  }
103  unsigned GetIndexSafe(const std::string & name) const
104  {
105  NamesMap::const_iterator it = _nameMap.find(name);
106  if (it==_nameMap.end())
107  throw SchemaError("Accessing an unexisting attribute '"+_scopeName+"':'"+name+"'");
108  return it->second;
109  }
110 
111  unsigned GetNAttributes() const
112  {
113  return _nameMap.size();
114  }
115 
116  void * Allocate(unsigned attribute, unsigned size) const
117  {
118  return _attributes[attribute]->Allocate(size);
119  }
120  void Deallocate(unsigned attribute, void * buffer) const
121  {
122  _attributes[attribute]->Deallocate(buffer);
123  }
124 
125  template <typename AttributeType>
126  void CheckType(unsigned attributeIndex, AttributeType *) const
127  {
128  _attributes[attributeIndex]->CheckType<AttributeType>();
129  }
130 
131  const std::string & GetAttributeName(unsigned attributeIndex) const
132  {
133  CLAM_ASSERT(attributeIndex<_attributes.size(),
134  "GetAttributeName: Using a wrong index to look up an attribute name");
135  AbstractAttribute * attribute = _attributes[attributeIndex];
136  return attribute->GetName();
137  }
138  const AbstractAttribute & GetAttribute(unsigned int attribute) const
139  {
140  return * _attributes[attribute];
141  }
142  };
143 
144 }
145 
146 
147 #endif// _DescriptionScope_hxx_
148