TDME2  1.9.200
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseProperties.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <memory>
5 #include <string>
6 
7 #include <tdme/tdme.h>
9 
10 using std::make_unique;
11 using std::move;
12 using std::remove;
13 using std::string;
14 using std::unique_ptr;
15 
18 
19 BaseProperties::BaseProperties(const string& name, const string& description)
20 {
21  this->name = name;
22  this->description = description;
23 }
24 
26 }
27 
29 {
30  properties.clear();
31  propertiesByName.clear();
32 }
33 
34 const BaseProperty* BaseProperties::getProperty(const string& name) const
35 {
36  auto propertyByNameIt = propertiesByName.find(name);
37  if (propertyByNameIt != propertiesByName.end()) {
38  return propertyByNameIt->second;
39  }
40  return nullptr;
41 }
42 
44 {
45  auto propertyByNameIt = propertiesByName.find(name);
46  if (propertyByNameIt != propertiesByName.end()) {
47  return propertyByNameIt->second;
48  }
49  return nullptr;
50 }
51 
52 const int BaseProperties::getPropertyIndex(const string& name) const
53 {
54  for (auto i = 0; i < properties.size(); i++) {
55  if (properties[i]->getName() == name) {
56  return i;
57  }
58  }
59  return -1;
60 }
61 
62 int BaseProperties::getPropertyIndex(const string& name)
63 {
64  for (auto i = 0; i < properties.size(); i++) {
65  if (properties[i]->getName() == name) {
66  return i;
67  }
68  }
69  return -1;
70 }
71 
72 bool BaseProperties::addProperty(const string& name, const string& value)
73 {
74  if (getProperty(name) != nullptr)
75  return false;
76 
77  auto property = make_unique<BaseProperty>(name, value);
78  propertiesByName[name] = property.get();
79  properties.push_back(move(property));
80  return true;
81 }
82 
83 bool BaseProperties::renameProperty(const string& oldName, const string& name) {
84  auto propertyByNameIt = propertiesByName.find(oldName);
85  if (propertyByNameIt == propertiesByName.end())
86  return false;
87 
88  if (oldName != name && getProperty(name) != nullptr) {
89  return false;
90  }
91 
92  BaseProperty* property = propertyByNameIt->second;
93  property->setName(name);
94 
95  propertiesByName.erase(propertyByNameIt);
96  propertiesByName[property->getName()] = property;
97 
98  return true;
99 }
100 
101 bool BaseProperties::updateProperty(const string& oldName, const string& name, const string& value)
102 {
103  auto propertyByNameIt = propertiesByName.find(oldName);
104  if (propertyByNameIt == propertiesByName.end())
105  return false;
106 
107  if (oldName != name && getProperty(name) != nullptr) {
108  return false;
109  }
110 
111  BaseProperty* property = propertyByNameIt->second;
112  property->setName(name);
113  property->setValue(value);
114 
115  propertiesByName.erase(propertyByNameIt);
116  propertiesByName[property->getName()] = property;
117 
118  return true;
119 }
120 
121 bool BaseProperties::removeProperty(const string& name)
122 {
123  auto propertyByNameIt = propertiesByName.find(name);
124  if (propertyByNameIt != propertiesByName.end()) {
125  auto property = propertyByNameIt->second;
126  for (auto i = 0; i < properties.size(); i++) {
127  if (properties[i].get() == property) {
128  properties.erase(properties.begin() + i);
129  break;
130  }
131  }
132  propertiesByName.erase(propertyByNameIt);
133  return true;
134  }
135 
136  return false;
137 }
void clearProperties()
Clears properties.
bool updateProperty(const string &oldName, const string &name, const string &value)
Update a property.
vector< unique_ptr< BaseProperty > > properties
const int getPropertyIndex(const string &name) const
Get property index.
const BaseProperty * getProperty(const string &name) const
Retrieve property by name.
map< string, BaseProperty * > propertiesByName
bool addProperty(const string &name, const string &value)
Add a property.
bool removeProperty(const string &name)
Removes a property.
bool renameProperty(const string &oldName, const string &name)
Rename a property.
Base property model class.
Definition: BaseProperty.h:15
void setName(const string &name)
Set up name.
Definition: BaseProperty.h:44