12 #include <unordered_map>
27 const char type_code[2] = {
static_cast<char> (item_type),
'\0'};
34 template <
class TThing,
class TParent>
40 template <
class TThing,
class TParent>
43 for (
auto &item : *
this) {
44 assert (item.second->getUseCount() == 1);
52 template <
class TThing,
class TParent>
54 auto it = this->begin();
55 while (it != this->end()) {
57 if (pred (element->second)) {
58 element->second->release();
59 this->erase (element);
65 template <
class TThing,
class TParent>
67 while (this->begin() != this->end()) {
68 this->begin()->second->release();
69 this->erase (this->begin());
74 template <
class TThing,
class TParent>
76 const auto iterator = this->find (key);
77 return (iterator == this->end() ?
nullptr : iterator->second);
81 template <
class TThing,
class TParent>
83 return getById (data[field].asString());
87 template <
class TThing,
class TParent>
89 for (
auto item : *
this) {
90 if (item.second->parent() == parent && *(item.second) == name) {
100 template <
class TThing,
class TParent>
102 const char type[2] = {
static_cast<char> (item_type),
'\0'};
104 long candidate = random();
105 std::string newid = type + std::to_string (candidate);
106 if (!getById (newid))
115 template <
class TThing,
class TParent>
117 std::string newid (
id.empty() ? getNewId (TThing::typetype()) :
id);
118 TThing *thing = allocate (parent, newid, name);
119 assert (this->find (newid) == this->end());
120 (*this)[newid] = thing;
134 template <
class TThing,
class TParent>
141 thing = getById (
id);
142 if (thing && thing->parent() == parent && *thing == name) {
148 thing = getByName (name, parent);
150 thing = getById (
id);
154 return addItem (name,
id, parent);
163 template <
class TThing,
class TParent>
166 const std::string &namefield,
167 const std::string &idfield) {
168 const std::string &name = data[namefield].
asString();
169 const std::string &
id = data[idfield].
asString();
170 TThing *item = addOrGetItem (name,
id, parent);
172 item->restore (data);
TThing * getById(const std::string &key) const
Get a thing by its id.
Definition: musiclibraryhash.h:75
std::string getNewId(MusicThingie::Type item_type) const
Construct unique, random ID for a new item.
Definition: musiclibraryhash.h:101
~ThingieContainer()
Destructor: Release all contents prior to destruction.
Definition: musiclibraryhash.h:41
TThing * addItem(const std::string &name, std::string id, TParent *parent)
Construct a new item instance and add it to the hash by its ID.
Definition: musiclibraryhash.h:116
TThing * getByName(const std::string &name, TParent *parent) const
Search the things looking for a name and parent match.
Definition: musiclibraryhash.h:88
TThing * addOrGetItem(const std::string &name, std::string id, TParent *parent)
Retrieve an item by ID or by name.
Definition: musiclibraryhash.h:135
ThingieContainer(const Allocator &alloc)
Constructor: Construct a new ThingieContainer and assign it an allocator.
Definition: musiclibraryhash.h:35
void purge(bool pred(const TThing *))
Remove items according to the predicate.
Definition: musiclibraryhash.h:53
void clear()
Remove all items from the hash table.
Definition: musiclibraryhash.h:66
std::function< TThing *(TParent *const, const std::string &, const std::string &)> Allocator
Definition: musiclibrary.h:291
Type
Definition: musictypes.h:86
Generic data type.
Definition: parsnip.h:81
const StringType & asString() const
Retrieve string value.
Definition: parsnip_types.cpp:133
PlayList / Artist / Album / Song types that are interbred to form a library.
Memory-based index/database of music library contents.
Definition: musiclibrary.cpp:37
std::string persistentId(MusicThingie::Type item_type, const std::string &key)
Use an item's type and a string key generate a id string.
Definition: musiclibraryhash.h:26
lamerkey_t create_key_from_string(const char *source)
Generate a key from a string.
Definition: utility.cpp:316
Various helpful templates, classes and functions.