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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
22 #ifndef ASSET_DB_H
23 #define ASSET_DB_H
24 
25 
26 #include "lib/sync.hpp"
27 #include "lib/error.hpp"
28 #include "steam/asset.hpp"
29 #include "lib/depend-inject.hpp"
30 
31 #include <memory>
32 #include <unordered_map>
33 // #include <boost/functional/hash.hpp> /////////TODO which boost include to use here??
34 #include <boost/utility.hpp>
35 
36 
37 namespace steam {
38 namespace asset {
39 
40  using std::static_pointer_cast;
41  using std::dynamic_pointer_cast;
42 
43  using lib::Sync;
44  using lib::RecursiveLock_NoWait;
45 
46 
47  /* ===== hash implementations ===== */
48 
49  size_t
50  hash_value (Asset::Ident const& idi)
51  {
52  size_t hash = 0;
53  boost::hash_combine(hash, idi.org);
54  boost::hash_combine(hash, idi.name);
55  boost::hash_combine(hash, idi.category);
56  return hash;
57  }
58 
59  size_t
60  hash_value (Asset const& asset)
61  {
62  return asset.getID();
63  }
64 
65 
72  struct IdentityHash
73  : public std::unary_function<size_t, size_t>
74  {
75  size_t
76  operator() (size_t val) const { return val; }
77  };
78 
79  typedef std::unordered_map<size_t, PAsset, IdentityHash> IdHashtable;
80 
81 
82 
83 
89  class DB
91  , public Sync<RecursiveLock_NoWait>
92  {
93  IdHashtable table;
94 
95  DB()
96  : table()
97  { }
98 
99  ~DB()
100  {
101  clear();
102  }
103 
104  friend class lib::DependencyFactory<DB>;
105 
106 
107  public:
108  template<class KIND>
109  lib::P<KIND>
110  get (ID<KIND> hash) const
111  {
112  return dynamic_pointer_cast<KIND,Asset> (find (hash));
113  }
114 
115  template<class KIND>
116  void
117  put (ID<KIND> hash, lib::P<KIND>& ptr)
118  {
119  table[hash] = static_pointer_cast (ptr);
120  }
121 
122  void
123  put (ID<Asset> hash, PAsset& ptr)
124  {
125  table[hash] = ptr;
126  }
127 
128  bool
129  del (ID<Asset> hash)
130  {
131  return table.erase (hash);
132  }
133 
145  void
146  clear ()
147  try
148  {
149  IdHashtable::iterator i = table.begin();
150  IdHashtable::iterator e = table.end();
151  for ( ; i!=e ; ++i )
152  i->second->dependants.clear();
153 
154  table.clear();
155  }
156  ERROR_LOG_AND_IGNORE (progress, "cleaning the Asset registry")
157 
158 
159 
160  void
161  asList (list<PcAsset>& output) const
162  {
163  IdHashtable::const_iterator i = table.begin();
164  IdHashtable::const_iterator e = table.end();
165  for ( ; i!=e ; ++i )
166  output.push_back (i->second);
167  }
168 
169 
170  private:
171  const PAsset &
172  find (size_t hash) const
173  {
174  static const PAsset NULLP;
175  IdHashtable::const_iterator i = table.find (hash);
176  if (i == table.end())
177  return NULLP; // empty ptr signalling "not found"
178  else
179  return i->second;
180  }
181  };
182 
183 
184 }} // namespace steam::asset
185 #endif
Facility for monitor object based locking.
Definition: sync.hpp:209
#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:266
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Per type specific configuration of instances created as service dependencies.
trivial hash functor returns any hash value unmodified.
Definition: db.hpp:72
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:89
void clear()
removes all registered assets and does something similar to Asset::unlink() on each to break cyclic d...
Definition: db.hpp:146
Superinterface describing especially bookkeeping properties.
Definition: asset.hpp:139
Steam-Layer Interface: Assets.
Lumiera error handling (C++ interface).
Helper to abstract creation and lifecycle of a dependency.
Definition: depend.hpp:125
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:71
The asset subsystem of the Steam-Layer.
Definition: wrapperptr.hpp:35
void asList(list< PcAsset > &output) const
intended for diagnostics
Definition: db.hpp:161
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:99