CLAM-Development  1.4.0
Pool.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 _Pool_hxx_
23 #define _Pool_hxx_
24 
25 #include "ScopePool.hxx"
26 #include "DescriptionScheme.hxx"
27 
28 namespace CLAM
29 {
41  {
42  public:
49  : _scheme(scheme), _scopePools(_scheme.GetNScopes(),(ScopePool*)0)
50  {
51  }
53 
60  void SetNumberOfContexts(const std::string & scopeName, unsigned size)
61  {
62  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
63  const DescriptionScope & scope = _scheme.GetScope(scopeIndex);
64  _scopePools[scopeIndex] = new ScopePool(scope, size);
65  }
66 
67  unsigned GetNumberOfContexts(const std::string & scopeName) const
68  {
69  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
70  CLAM_ASSERT(_scopePools[scopeIndex],
71  ("Getting the size of scope '"+scopeName+"' but it is not populated").c_str());
72  return _scopePools[scopeIndex]->GetSize();
73  }
74 
75  void Insert(const std::string & scopeName, unsigned pos)
76  {
77  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
78  CLAM_ASSERT(_scopePools[scopeIndex], "booooo");
79  _scopePools[scopeIndex]->Insert(pos);
80  }
81  void Remove(const std::string & scopeName, unsigned pos)
82  {
83  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
84  CLAM_ASSERT(_scopePools[scopeIndex], "booooo");
85  _scopePools[scopeIndex]->Remove(pos);
86  }
87 
89  void InstantiateAttribute(const std::string & scopeName, const std::string & attributeName)
90  {
91  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
92  const DescriptionScope & scope = _scheme.GetScope(scopeIndex);
93  scope.GetIndex(attributeName);
94  CLAM_ASSERT(_scopePools[scopeIndex],
95  ("Instantianting '"+scopeName+":"+attributeName+"' but the scope is not yet populated").c_str());
96  }
104  template <typename AttributeType>
105  AttributeType * GetWritePool(const std::string & scopeName, const std::string & attributeName)
106  {
107  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
108 
109  CLAM_ASSERT(_scopePools[scopeIndex],
110  ("Writting data on '"+scopeName+":"+attributeName+"' but the scope is not yet populated").c_str());
111 
112  return _scopePools[scopeIndex]->template GetWritePool<AttributeType>(attributeName);
113  }
122  template <typename AttributeType>
123  const AttributeType * GetReadPool(const std::string & scopeName, const std::string & attributeName) const
124  {
125  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
126 
127  CLAM_ASSERT(_scopePools[scopeIndex],
128  ("Reading data from '"+scopeName+":"+attributeName+"' but the scope is not yet populated").c_str());
129 
130  return _scopePools[scopeIndex]->template GetReadPool<AttributeType>(attributeName);
131  }
132  bool IsInstantiated(const std::string & scopeName, const std::string & attributeName) const
133  {
134  unsigned scopeIndex = _scheme.GetScopeIndex(scopeName);
135  if (!_scopePools[scopeIndex]) return false;
136  return _scopePools[scopeIndex]->IsInstantiated(attributeName);
137  }
138  // Component Interface
139 
140  const char * GetClassName() const { return "DescriptionDataPool"; }
141  void StoreOn(Storage & storage) const
142  {
143  for (unsigned i = 0; i<_scopePools.size(); i++)
144  {
145  XMLComponentAdapter adapter(*(_scopePools[i]), "ScopePool", true);
146  storage.Store(adapter);
147  }
148  }
149  void LoadFrom(Storage & storage)
150  {
151  for (unsigned i = 0; i<_scopePools.size(); i++)
152  {
153  const DescriptionScope & scope = _scheme.GetScope(i);
154  _scopePools[i] = new ScopePool(scope,0);
155  XMLComponentAdapter adapter(*(_scopePools[i]), "ScopePool", true);
156  storage.Load(adapter);
157  }
158  }
159 
160  private:
161  const DescriptionScheme & _scheme;
162  typedef std::vector<ScopePool*> ScopePools;
163  ScopePools _scopePools;
164  };
165 
166 }
167 
168 
169 #endif// _Pool_hxx_
170