Lumiera  0.pre.03
»edityourfreedom«
db.hpp
Go to the documentation of this file.
1 /*
2  DB.hpp - registry holding known Asset instances.
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
31 #ifndef ASSET_DB_H
32 #define ASSET_DB_H
33 
34 
35 #include "lib/sync.hpp"
36 #include "lib/error.hpp"
37 #include "proc/asset.hpp"
38 #include "lib/depend-inject.hpp"
39 
40 #include <memory>
41 #include <unordered_map>
42 // #include <boost/functional/hash.hpp> /////////TODO which boost include to use here??
43 #include <boost/utility.hpp>
44 
45 
46 namespace proc {
47 namespace asset {
48 
49  using std::static_pointer_cast;
50  using std::dynamic_pointer_cast;
51 
52  using lib::Sync;
54 
55 
56  /* ===== hash implementations ===== */
57 
58  size_t
59  hash_value (Asset::Ident const& idi)
60  {
61  size_t hash = 0;
62  boost::hash_combine(hash, idi.org);
63  boost::hash_combine(hash, idi.name);
64  boost::hash_combine(hash, idi.category);
65  return hash;
66  }
67 
68  size_t
70  {
71  return asset.getID();
72  }
73 
74 
81  struct IdentityHash
82  : public std::unary_function<size_t, size_t>
83  {
84  size_t
85  operator() (size_t val) const { return val; }
86  };
87 
88  typedef std::unordered_map<size_t, PAsset, IdentityHash> IdHashtable;
89 
90 
91 
92 
98  class DB
100  , public Sync<RecursiveLock_NoWait>
101  {
102  IdHashtable table;
103 
104  DB()
105  : table()
106  { }
107 
108  ~DB()
109  {
110  clear();
111  }
112 
113  friend class lib::DependencyFactory<DB>;
114 
115 
116  public:
117  template<class KIND>
118  lib::P<KIND>
119  get (ID<KIND> hash) const
120  {
121  return dynamic_pointer_cast<KIND,Asset> (find (hash));
122  }
123 
124  template<class KIND>
125  void
127  {
128  table[hash] = static_pointer_cast (ptr);
129  }
130 
131  void
133  {
134  table[hash] = ptr;
135  }
136 
137  bool
139  {
140  return table.erase (hash);
141  }
142 
154  void
155  clear ()
156  try
157  {
158  IdHashtable::iterator i = table.begin();
159  IdHashtable::iterator e = table.end();
160  for ( ; i!=e ; ++i )
161  i->second->dependants.clear();
162 
163  table.clear();
164  }
165  ERROR_LOG_AND_IGNORE (progress, "cleaning the Asset registry")
166 
167 
168 
169  void
170  asList (list<PcAsset>& output) const
171  {
172  IdHashtable::const_iterator i = table.begin();
173  IdHashtable::const_iterator e = table.end();
174  for ( ; i!=e ; ++i )
175  output.push_back (i->second);
176  }
177 
178 
179  private:
180  const PAsset &
181  find (size_t hash) const
182  {
183  static const PAsset NULLP;
184  IdHashtable::const_iterator i = table.find (hash);
185  if (i == table.end())
186  return NULLP; // empty ptr signalling "not found"
187  else
188  return i->second;
189  }
190  };
191 
192 
193 }} // namespace proc::asset
194 #endif
bool del(ID< Asset > hash)
Definition: db.hpp:138
Facility for monitor object based locking.
Definition: sync.hpp:425
virtual const ID< Asset > & getID() const
Definition: asset.hpp:210
#define hash
string name
element ID, comprehensible but sanitised.
Definition: asset.hpp:164
const PAsset & find(size_t hash) const
Definition: db.hpp:181
void put(ID< KIND > hash, lib::P< KIND > &ptr)
Definition: db.hpp:126
#define ERROR_LOG_AND_IGNORE(_FLAG_, _OP_DESCR_)
convenience shortcut for a sequence of catch blocks just logging and consuming an error...
Definition: error.hpp:272
The asset subsystem of the Proc-Layer.
Definition: id-scheme.hpp:64
void clear()
removes all registered assets and does something similar to Asset::unlink() on each to break cyclic d...
Definition: db.hpp:155
IdHashtable table
Definition: db.hpp:102
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Per type specific configuration of instances created as service dependencies.
void asList(list< PcAsset > &output) const
intended for diagnostics
Definition: db.hpp:170
std::unordered_map< size_t, PAsset, IdentityHash > IdHashtable
Definition: db.hpp:88
typename meta::Strip< CON >::TypeReferred::iterator iterator
asset::Category category
primary tree like classification of the asset.
Definition: asset.hpp:169
trivial hash functor returns any hash value unmodified.
Definition: db.hpp:81
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:148
bool find(Query< Pipe > &q)
shortcut: run just a query without creating new instances
void put(ID< Asset > hash, PAsset &ptr)
Definition: db.hpp:132
Object Monitor based synchronisation.
a POD comprised of all the information sufficiently identifying any given Asset.
Definition: asset.hpp:158
typename meta::Strip< CON >::TypeReferred::const_iterator const_iterator
Proc-Layer Interface: Assets.
size_t operator()(size_t val) const
Definition: db.hpp:85
Lumiera error handling (C++ interface).
size_t hash_value(Category const &cat)
Definition: category.hpp:112
thin wrapper around a size_t hash ID used as primary key for all Asset objects.
Definition: asset.hpp:108
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:134
Mutex< Wrapped_RecursiveMutex > RecursiveLock_NoWait
Definition: sync.hpp:375
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:76
Proc-Layer implementation namespace root.
Definition: id-scheme.hpp:63
const string org
origin or authorship id.
Definition: asset.hpp:176
Implementation of the registry holding all Asset instances known to the Asset Manager subsystem...
Definition: db.hpp:98