Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
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/utility.hpp>
34
35
36namespace steam {
37namespace asset {
38
39 using std::static_pointer_cast;
40 using std::dynamic_pointer_cast;
41
42 using lib::Sync;
43 using lib::RecursiveLock_NoWait;
44
45
46 /* ===== hash implementations ===== */
47
48 size_t
50 {
51 size_t hash = 0;
52 boost::hash_combine(hash, idi.org);
53 boost::hash_combine(hash, idi.name);
54 boost::hash_combine(hash, idi.category);
55 return hash;
56 }
57
58 size_t
60 {
61 return asset.getID();
62 }
63
64
72 {
73 size_t
74 operator() (size_t val) const { return val; }
75 };
76
77 using IdHashtable = std::unordered_map<size_t, PAsset, IdentityHash>;
78
79
80
81
87 class DB
89 , public Sync<RecursiveLock_NoWait>
90 {
92
94 : table()
95 { }
96
98 {
99 clear();
100 }
101
102 friend class lib::DependencyFactory<DB>;
103
104
105 public:
106 template<class KIND>
109 {
110 return dynamic_pointer_cast<KIND,Asset> (find (hash));
111 }
112
113 template<class KIND>
114 void
116 {
117 table[hash] = static_pointer_cast (ptr);
118 }
119
120 void
122 {
123 table[hash] = ptr;
124 }
125
126 bool
128 {
129 return table.erase (hash);
130 }
131
143 void
145 try
146 {
147 IdHashtable::iterator i = table.begin();
148 IdHashtable::iterator e = table.end();
149 for ( ; i!=e ; ++i )
150 i->second->dependants.clear();
151
152 table.clear();
153 }
154 ERROR_LOG_AND_IGNORE (progress, "cleaning the Asset registry")
155
156
157
158 void
159 asList (list<PcAsset>& output) const
160 {
161 IdHashtable::const_iterator i = table.begin();
162 IdHashtable::const_iterator e = table.end();
163 for ( ; i!=e ; ++i )
164 output.push_back (i->second);
165 }
166
167
168 private:
169 const PAsset &
170 find (size_t hash) const
171 {
172 static const PAsset NULLP;
173 IdHashtable::const_iterator i = table.find (hash);
174 if (i == table.end())
175 return NULLP; // empty ptr signalling "not found"
176 else
177 return i->second;
178 }
179 };
180
181
182}} // namespace steam::asset
183#endif
Steam-Layer Interface: Assets.
Helper to abstract creation and lifecycle of a dependency.
Definition depend.hpp:127
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition p.hpp:77
Facility for monitor object based locking.
Definition sync.hpp:210
Superinterface describing especially bookkeeping properties.
Definition asset.hpp:139
Implementation of the registry holding all Asset instances known to the Asset Manager subsystem.
Definition db.hpp:90
void put(ID< Asset > hash, PAsset &ptr)
Definition db.hpp:121
lib::P< KIND > get(ID< KIND > hash) const
Definition db.hpp:108
void asList(list< PcAsset > &output) const
intended for diagnostics
Definition db.hpp:159
const PAsset & find(size_t hash) const
Definition db.hpp:170
bool del(ID< Asset > hash)
Definition db.hpp:127
void put(ID< KIND > hash, lib::P< KIND > &ptr)
Definition db.hpp:115
IdHashtable table
Definition db.hpp:91
void clear()
removes all registered assets and does something similar to Asset::unlink() on each to break cyclic d...
Definition db.hpp:144
thin wrapper around a size_t hash ID used as primary key for all Asset objects.
Definition asset.hpp:98
Any copy and copy construction prohibited.
Definition nocopy.hpp:38
Per type specific configuration of instances created as service dependencies.
Lumiera error handling (C++ interface).
#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:267
#define hash
The asset subsystem of the Steam-Layer.
size_t hash_value(Category const &cat)
Definition category.hpp:90
std::unordered_map< size_t, PAsset, IdentityHash > IdHashtable
Definition db.hpp:77
Steam-Layer implementation namespace root.
a POD comprised of all the information sufficiently identifying any given Asset.
Definition asset.hpp:147
asset::Category category
primary tree like classification of the asset.
Definition asset.hpp:156
string name
element ID, comprehensible but sanitised.
Definition asset.hpp:151
const string org
origin or authorship id.
Definition asset.hpp:163
trivial hash functor returns any hash value unmodified.
Definition db.hpp:72
size_t operator()(size_t val) const
Definition db.hpp:74
Object Monitor based synchronisation.