Allolib  1.0
C++ Components For Interactive Multimedia
al_DiscreteParameterValues.hpp
1 #ifndef AL_DISCRETEPARAMETERVALUES_HPP
2 #define AL_DISCRETEPARAMETERVALUES_HPP
3 
4 #include <vector>
5 #include <string>
6 #include <mutex>
7 
8 namespace al {
9 
11 public:
12  // TODO add support for in16 and uint16
13  typedef enum {
14  BOOL,
15  FLOAT,
16  DOUBLE,
17  INT8,
18  UINT8,
19  INT32,
20  UINT32,
21  INT64,
22  UINT64,
23  STRING
24  } Datatype;
25 
26  DiscreteParameterValues(Datatype datatype = FLOAT);
27 
29 
30  Datatype getDataType() { return mDatatype; }
31 
32  size_t size();
33  // void reserve(size_t totalSize);
34 
35  void sort();
36  void clear();
37 
38  template <typename SpaceDataType>
39  void append(std::vector<SpaceDataType> &values, std::string idprefix = "") {
40  // TODO validate data type
41  append(values.data(), values.size(), idprefix);
42  }
43 
44  void append(void *values, size_t count, std::string idprefix = "");
45 
46  // Set limits from internal data
47  // void conform();
48 
49  // Query ----
50 
59  size_t getFirstIndexForId(std::string id, bool reverse = false);
60 
61  float at(size_t x);
62 
63  std::string idAt(size_t x);
64 
73  size_t getIndexForValue(float value);
74 
75  // Access to complete sets
76  template <typename VecDataType> std::vector<VecDataType> getValues() {
77  // TODO validate sizes
78  return std::vector<VecDataType>((VecDataType *)mValues,
79  (VecDataType *)mValues + size());
80  }
81  void *getValuesPtr() { return mValues; }
82 
83  void setIds(std::vector<std::string> newIds) { mIds = newIds; }
84  std::vector<std::string> getIds();
85 
86  // Protect parameter space (to avoid access during modification)
87  // TODO all readers above need to use this lock
88  void lock() { mLock.lock(); }
89  void unlock() { mLock.unlock(); }
90 
91 protected:
92  template <typename VecDataType> std::vector<VecDataType> getAsVector() {
93  std::vector<VecDataType> vec;
94  VecDataType *castValues = static_cast<VecDataType>(mValues);
95  vec.assign(castValues, castValues + mSize);
96  return vec;
97  }
98  std::string valueToString(void *value);
99  double valueToFloat(void *value);
100  int64_t valueToInt64(void *value);
101 
102 private:
103  Datatype mDatatype{FLOAT};
104  size_t mSize{0};
105  uint8_t mDataSize;
106  unsigned char *mValues{nullptr};
107  std::vector<std::string> mIds;
108 
109  std::mutex mLock;
110 };
111 
112 } // namespace al
113 
114 #endif // AL_DISCRETEPARAMETERVALUES_HPP
size_t getFirstIndexForId(std::string id, bool reverse=false)
getFirstIndexForId
size_t getIndexForValue(float value)
getIndexForValue
Definition: al_App.hpp:23