Lumiera  0.pre.03
»edit your freedom«
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 "steam/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 steam {
47 namespace asset {
48 
49  using std::static_pointer_cast;
50  using std::dynamic_pointer_cast;
51 
52  using lib::Sync;
53  using lib::RecursiveLock_NoWait;
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
69  hash_value (Asset const& asset)
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
126  put (ID<KIND> hash, lib::P<KIND>& ptr)
127  {
128  table[hash] = static_pointer_cast (ptr);
129  }
130 
131  void
132  put (ID<Asset> hash, PAsset& ptr)
133  {
134  table[hash] = ptr;
135  }
136 
137  bool
138  del (ID<Asset> hash)
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 steam::asset
194 #endif
Facility for monitor object based locking.
Definition: sync.hpp:217
#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:275
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Per type specific configuration of instances created as service dependencies.
trivial hash functor returns any hash value unmodified.
Definition: db.hpp:81
Steam-Layer implementation namespace root.
Object Monitor based synchronisation.
Implementation of the registry holding all Asset instances known to the Asset Manager subsystem...
Definition: db.hpp:98
void clear()
removes all registered assets and does something similar to Asset::unlink() on each to break cyclic d...
Definition: db.hpp:155
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:148
Steam-Layer Interface: Assets.
Lumiera error handling (C++ interface).
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:134
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:44
void asList(list< PcAsset > &output) const
intended for diagnostics
Definition: db.hpp:170
NoUsableHashDefinition hash_value(...)
declared for metaprogramming only, never defined
thin wrapper around a size_t hash ID used as primary key for all Asset objects.
Definition: asset.hpp:108