pianod2
multisource multiuser scriptable networked music player
lookup.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <config.h>
13 
14 #include <cassert>
15 
16 #include <string>
17 #include <initializer_list>
18 
19 #include "fundamentals.h"
20 
22 template <class EnumClass> class StandardLookupValues {
23 public:
24  const char *name;
25  EnumClass value;
26 };
27 
29 template <class EnumClass,
30 class LookupTableType = StandardLookupValues <EnumClass> > class LookupTable {
31  const LookupTableType *table = NULL;
32  const int table_size = 0;
33  bool release = false; // True when table was allocated from free store.
34 public:
35  LookupTable () = delete;
39  LookupTable (const LookupTableType *tab) {
40  table = tab;
41  int i = 0;
42  for (i = 0; tab [i].name; i++) /* just looking */;
43  *const_cast <int *> (&this->table_size) = i;
44  }
46  LookupTable (const std::initializer_list<LookupTableType> tab) :
47  table_size (tab.size()) {
48  LookupTableType *iniz = new LookupTableType [tab.size() + 1];
49  int i = 0;
50  for (auto item : tab) {
51  assert (item.name);
52  iniz [i].name = item.name;
53  iniz [i++].value = item.value;
54  }
55  iniz [i].name = NULL;
56  table = iniz;
57  release = true;
58  }
60  if (release) {
61  delete[] table;
62  }
63  };
64 
65 
69  virtual const LookupTableType *get (const char *s) const noexcept {
70  for (const LookupTableType *l = table; l->name != NULL; l++) {
71  if (strcasecmp (s, l->name) == 0) {
72  return l;
73  }
74  }
75  return NULL;
76  }
77 
82  const EnumClass operator[] (const char *s) const {
83  const LookupTableType *item = get (s);
84  if (!item) {
85  throw CommandError (E_INVALID, s);
86  }
87  return item->value;
88  }
89 
94  inline const EnumClass operator[] (const std::string &s) const {
95  return (*this) [s.c_str()];
96  }
102  const bool tryGetValue (const char *s, EnumClass *value) const {
103  const LookupTableType *item = get (s);
104  if (!item) return false;
105  *value = item->value;
106  return true;
107  }
108  inline const bool tryGetValue (const std::string &s, EnumClass *value) const {
109  return tryGetValue (s.c_str(), value);
110  }
111 
115  const LookupTableType *get (const EnumClass id) const {
116  for (const LookupTableType *l = table; l->name != NULL; l++) {
117  if (id == l->value) {
118  return l;
119  }
120  }
121  assert (0);
122  return NULL;
123  }
127  const char *operator[] (const EnumClass id) const {
128  const LookupTableType *item = get (id);
129  if (!item) return nullptr;
130  return item->name;
131  }
132 
133  const LookupTableType *begin () const {
134  return &table [0];
135  }
136 
137  const LookupTableType *end () const {
138  return &table [table_size];
139  }
140 };
141 
Exception for command execution problems.
Definition: fundamentals.h:293
Class to map between enumerations and their text values.
Definition: lookup.h:30
const LookupTableType * table
Definition: lookup.h:31
LookupTable(const LookupTableType *tab)
Initialize the lookup table from an array.
Definition: lookup.h:39
const bool tryGetValue(const char *s, EnumClass *value) const
Retrieve the enumerator corresponding to a text value.
Definition: lookup.h:102
bool release
Definition: lookup.h:33
LookupTable(const LookupTable< EnumClass, LookupTableType > &)=delete
const LookupTableType * end() const
Definition: lookup.h:137
const LookupTableType * get(const EnumClass id) const
Retrieve a table entry by the enumerator of an entry.
Definition: lookup.h:115
const int table_size
Definition: lookup.h:32
~LookupTable()
Definition: lookup.h:59
const EnumClass operator[](const char *s) const
Retrieve the enumerator corresponding to text name.
Definition: lookup.h:82
virtual const LookupTableType * get(const char *s) const noexcept
Retrieve a table entry by the text name of an entry.
Definition: lookup.h:69
LookupTable(const LookupTable< EnumClass, LookupTableType > &&)=delete
LookupTable()=delete
const LookupTableType * begin() const
Definition: lookup.h:133
const bool tryGetValue(const std::string &s, EnumClass *value) const
Definition: lookup.h:108
LookupTable(const std::initializer_list< LookupTableType > tab)
Initialize the lookup table from an initializer list.
Definition: lookup.h:46
A standard form of enumeration to text value lookup tables.
Definition: lookup.h:22
const char * name
Definition: lookup.h:24
EnumClass value
Definition: lookup.h:25
Essential data structures and support.
@ E_INVALID
Definition: fundamentals.h:192
uint32_t value
Definition: audiooutput.cpp:68
int strcasecmp(const std::string &a, const char *b)
Definition: utility.h:48