CLAM-Development  1.4.0
Extractor.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 _Extractor_hxx_
23 #define _Extractor_hxx_
24 
25 #include <string>
26 #include "Pool.hxx"
27 
28 namespace CLAM
29 {
30 
32 template <typename AttributeType>
33 class Hook
34 {
35 public:
36  virtual ~Hook(){}
37  void Bind(
38  const std::string & scope,
39  const std::string & attribute)
40  {
41  _scope = scope;
42  _attribute = attribute;
43  }
44 
45  virtual void Next() = 0;
46  virtual bool IsInsideScope() const = 0;
47 
48 protected:
49  const std::string GetScope() const
50  {
51  return _scope;
52  }
53  std::string _attribute;
54  std::string _scope;
55 };
56 
58 template <typename AttributeType>
59 class ReadHook : public Hook<AttributeType>
60 {
61 public:
63  {
64  _chained=0;
65  }
67  {
68  if (_chained) delete _chained;
69  }
71  const std::string & scope,
72  const std::string & attribute)
73  {
74  Hook<AttributeType>::Bind(scope,attribute);
75  return *this;
76  }
78  const std::string & scope,
79  const std::string & attribute)
80  {
81  if (_chained)
82  {
83  _chained->Indirect(scope,attribute);
84  }
85  else
86  {
88  _chained->Bind(scope,attribute);
89  }
90  return *this;
91  }
92 
93  const AttributeType & GetForReading() const
94  {
95  return _data [GetCurrent()];
96  }
97 
98  void Init(const DescriptionDataPool & pool)
99  {
100  _current = 0;
101  _pool = &pool;
102  const std::string & scope = Hook<AttributeType>::_scope;
103  const std::string & attribute = Hook<AttributeType>::_attribute;
104  _data = _pool->template GetReadPool<AttributeType>(scope,attribute);
105  if (_chained) _chained->Init(pool);
106  }
107 
108  virtual void Next()
109  {
110  if (_chained) _chained->Next();
111  else _current++;
112  }
113 
114  virtual bool IsInsideScope() const
115  {
116  if (_chained) return _chained->IsInsideScope();
117  const std::string & scope = Hook<AttributeType>::_scope;
118  return _current < _pool->GetNumberOfContexts(scope);
119  }
120 
121 protected:
122  virtual unsigned GetCurrent() const
123  {
124  if (!_chained) return _current;
125 
126  unsigned indirection = _chained->GetForReading();
127  const std::string & scope = Hook<AttributeType>::_scope;
128  CLAM_ASSERT(indirection<_pool->GetNumberOfContexts(scope),
129  "Invalid cross-scope reference");
130  return indirection;
131  }
132 
133 protected:
135  const AttributeType * _data;
137 private:
138  unsigned _current;
139 };
140 
142 template <typename AttributeType>
143 class ReadRangedHook : public ReadHook<AttributeType>
144 {
145 public:
147  const AttributeType*& begin,
148  const AttributeType*& end) const
149  {
151  end = begin + _range;
152  }
153 
154  ReadHook<AttributeType> & Range(unsigned range)
155  {
156  _range = range;
157  return *this;
158  }
159 private:
160  unsigned _range;
161 };
162 
164 template <typename AttributeType>
165 class WriteHook : public Hook<AttributeType>
166 {
167 public:
168  void Init(DescriptionDataPool & pool)
169  {
170  _pool = &pool;
171  _current = 0;
172  const std::string & scope = Hook<AttributeType>::_scope;
173  const std::string & attribute = Hook<AttributeType>::_attribute;
174  _data = _pool->template GetWritePool<AttributeType>(scope,attribute);
175  }
176 
177  AttributeType & GetForWriting() const
178  {
179  return _data [GetCurrent()];
180  }
181 
182  virtual void Next()
183  {
184  _current++;
185  }
186 
187  virtual bool IsInsideScope() const
188  {
189  const std::string & scope = Hook<AttributeType>::_scope;
190  return _current < _pool->GetNumberOfContexts(scope);
191  }
192 
193 protected:
194  unsigned GetCurrent() const
195  {
196  return _current;
197  }
198 private:
199  DescriptionDataPool * _pool;
200  AttributeType * _data;
201  unsigned _current;
202 };
203 
204 }
205 
206 
207 
208 
209 
210 #endif // _Extractor_hxx_
211