List.hpp
Go to the documentation of this file.
1 
8 #pragma once
9 
11 #include <map>
12 #include <typeinfo>
13 
14 namespace eproperty {
18  template<class TYPE> class List : public PropertyType<TYPE> {
19  private:
20  std::map<std::string, TYPE> m_list;
21  public:
30  template<class CLASS_TYPE>
31  List(CLASS_TYPE* _owner,
32  const std::string& _name,
33  const TYPE& _defaultValue,
34  const std::string& _description="",
35  void (CLASS_TYPE::*_setObs)()=nullptr) :
36  eproperty::PropertyType<TYPE>(_owner, _name, _defaultValue, _description, _setObs) {
37 
38  };
43  List(const TYPE& _defaultValue) :
44  eproperty::PropertyType<TYPE>(_defaultValue) {
45 
46  };
50  virtual ~List() = default;
57  void add(const TYPE& _value, const std::string& _name, const std::string& _description = "") {
58  auto it = m_list.find(_name);
59  if (it != m_list.end()) {
60  it->second = _value;
61  return;
62  }
63  m_list.insert(std::make_pair(_name, _value));
64  }
69  void remove(const std::string& _name) {
70  auto it = m_list.find(_name);
71  bool firstValue = false;
72  bool firstDefault = false;
73  if (it != m_list.end()) {
74  if (it->second == eproperty::PropertyType<TYPE>::m_value) {
75  EPROPERTY_ERROR("property value='" << it->first << "' has been removed and this value was set ... change it before removing value to remove this error");
76  firstValue = true;
77  }
78  if (it->second == eproperty::PropertyType<TYPE>::m_default) {
79  EPROPERTY_ERROR("property default='" << it->first << "' has been removed and this value was set ... change it before removing value to remove this error");
80  firstDefault = true;
81  }
82  m_list.erase(it);
83  return;
84  }
85  if (m_list.size() == 0) {
86  EPROPERTY_INFO("property All value removed ==> can not change default and value");
87  return;
88  }
89  if (firstDefault == true) {
90  eproperty::PropertyType<TYPE>::m_default = m_list.begin()->second;
91  }
92  if (firstValue == true) {
93  eproperty::PropertyType<TYPE>::m_value = m_list.begin()->second;
94  }
95  }
101  void rename(const std::string& _nameOld, const std::string& _nameNew) {
102  //get old value
103  TYPE value;
104  auto it = m_list.find(_nameOld);
105  if (it != m_list.end()) {
106  value = it->second;
107  } else {
108  EPROPERTY_ERROR("paramList rename can not be done '" << _nameOld << "' in '" << _nameNew << "' parameter name does not exist");
109  return;
110  }
111  remove(_nameOld);
112  add(value, _nameNew);
113  }
114  std::string getPropertyType() const override {
115  return "eproperty::List";
116  }
117  void setString(const std::string& _newVal) override {
118  auto it = m_list.find(_newVal);
119  if (it != m_list.end()) {
120  if (it->second != eproperty::PropertyType<TYPE>::m_value) {
123  }
124  return;
125  }
126  EPROPERTY_WARNING("paramList value='" << _newVal << "' is not un the list ... ==> no change");
127  #ifdef DEBUG
128  for (auto &it : m_list) {
129  EPROPERTY_VERBOSE(" element : " << it.first);
130  }
131  #endif
132  }
133  std::string getInfo() const override {
134  std::string list = "List default=" + getValueSpecific(eproperty::PropertyType<TYPE>::m_default) + " in : [";
135  for (auto &it : m_list) {
136  list += it.first + "/";
137  }
138  return list + "]";
139  }
140  std::vector<std::string> getListValue() const override {
141  std::vector<std::string> out;
142  for (auto &it : m_list) {
143  out.push_back(it.first);
144  }
145  return out;
146  }
151  void set(const TYPE& _newVal) override {
153  return;
154  }
155  for (auto &it : m_list) {
156  if (it.second == _newVal) {
159  return;
160  }
161  }
162  EPROPERTY_WARNING("paramList value=??? is not un the list ... ==> no change");
163  }
164  void setDirectCheck(const TYPE& _newVal) override {
166  return;
167  }
168  for (auto &it : m_list) {
169  if (it.second == _newVal) {
171  return;
172  }
173  }
174  EPROPERTY_WARNING("paramList value=??? is not un the list ... ==> no change");
175  }
176  private:
182  std::string getValueSpecific(const TYPE& _valueRequested) const override {
183  for (auto &it : m_list) {
184  if (it.second == _valueRequested) {
185  return it.first;
186  }
187  }
188  return "???";
189  }
190  };
192  template<typename TYPE> std::ostream& operator <<(std::ostream& _os, const eproperty::List<TYPE>& _obj) {
193  _os << _obj.get();
194  return _os;
195  }
196 }
197 
List(CLASS_TYPE *_owner, const std::string &_name, const TYPE &_defaultValue, const std::string &_description="", void(CLASS_TYPE::*_setObs)()=nullptr)
Create a parameter with List of element parameter (nullptr if none).
Definition: List.hpp:31
std::string getInfo() const override
Description of the Propertys.
Definition: List.hpp:133
void add(const TYPE &_value, const std::string &_name, const std::string &_description="")
Add a value in the list of parameter.
Definition: List.hpp:57
void setDirectCheck(const TYPE &_newVal) override
Set the value of the current parameter (check range and ... if needed).
Definition: List.hpp:164
void setString(const std::string &_newVal) override
Set a new value of the Property (with string interface).
Definition: List.hpp:117
void notifyChange() const
call main class that PropertyChange
Template base of the property (have a generic set and get for string)
Definition: PropertyType.hpp:18
std::vector< std::string > getListValue() const override
Specific for eproperty::List to get all the possible values.
Definition: List.hpp:140
List(const TYPE &_defaultValue)
Create a parameter with List of element parameter (nullptr if none).
Definition: List.hpp:43
virtual ~List()=default
virtualisation of Destructor.
std::string getPropertyType() const override
Get the Property type of the class in string mode.
Definition: List.hpp:114
void rename(const std::string &_nameOld, const std::string &_nameNew)
Rename a value of the property.
Definition: List.hpp:101
eproperty global interface for all property implementation
Definition: Interface.hpp:14
Set a list of value availlable (for enumeration)
Definition: List.hpp:18
std::vector< std::string > list()