1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
22 * @file simulator_resource_model.h
24 * @brief This file contains a class which represents the resource model for simulator
25 * resources and provides a set of functions for updating the model.
28 #ifndef SIMULATOR_RESOURCE_MODEL_H_
29 #define SIMULATOR_RESOURCE_MODEL_H_
33 #include "OCPlatform.h"
37 * @class SimulatorResourceModel
38 * @brief This class provides a set of functions for accessing and manipulating the resource model.
40 class SimulatorResourceModel
43 SimulatorResourceModel() = default;
44 SimulatorResourceModel(const SimulatorResourceModel &) = default;
45 SimulatorResourceModel &operator=(const SimulatorResourceModel &) = default;
46 SimulatorResourceModel(SimulatorResourceModel &&) = default;
47 SimulatorResourceModel &operator=(SimulatorResourceModel && ) = default;
51 * @brief This class represents a resource attribute whose values can be generic.
56 typedef boost::variant <
76 m_updateInterval = -1;
79 Attribute(const std::string &attrName)
84 m_updateInterval = -1;
88 * API to get attribute's name.
90 * @return Attribute name.
92 std::string getName(void) const;
95 * API to set the name of attribute.
97 * @param name - Attribute name.
99 void setName(const std::string &name);
102 * API to get attribute's value.
104 * @return value of attribute.
106 template <typename T>
110 return boost::get<T>(m_value);
114 * API to get attribute's value.
116 * @return value of attribute as ValueVariant.
118 ValueVariant &getValue()
124 * API to get attribute's value type.
126 * @return ValueType enum.
128 ValueType getValueType() const;
131 * API to set the attribute's value.
133 * @param value - value to be set.
135 template <typename T>
136 void setValue(const T &value)
142 * API to set the attribute's value from allowed values container.
144 * @param allowedValueIndex - Index of value to be set from allowed vaules container.
146 void setFromAllowedValue(unsigned int index);
149 * API to get range of attribute's value.
151 void getRange(int &min, int &max) const;
154 * API to set range of attribute's value.
156 * @param min - minimum value could be set as attribute value.
157 * @param max - maximum value could be set as attribute value.
159 void setRange(const int &min, const int &max);
162 * API to set the values to allowed values set.
164 * @param values - vector of values which will be set as allowed values.
166 template <typename T>
167 bool setAllowedValues(const std::vector<T> &values)
169 ValueVariant temp = values.at(0);
170 if (temp.which() != m_value.which())
175 m_allowedValues.addValues(values);
180 * API to get the number of values present in allowed values set.
182 * @return Size of the allowed values.
184 int getAllowedValuesSize() const;
187 * API to get the string representation of the value.
189 * @return Attribute's value as a string.
191 std::string valueToString() const;
194 * API to get the string representation of all the allowed values.
196 * @return All allowed values as a string.
198 std::vector<std::string> allowedValuesToString() const;
200 void addValuetoRepresentation(OC::OCRepresentation &rep,
201 const std::string &key) const;
203 bool compare(Attribute &attribute);
205 std::vector<ValueVariant> getAllowedValues();
207 int getUpdateFrequencyTime() {return m_updateInterval;}
208 void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;}
214 template <typename T>
215 void addValue(const T &value)
217 ValueVariant temp = value;
218 m_values.push_back(temp);
221 template <typename T>
222 void addValues(const std::vector<T> &values)
224 for (auto value : values)
226 ValueVariant vValue = value;
227 m_values.push_back(vValue);
231 ValueVariant &at(unsigned int index);
233 std::vector<std::string> toString() const;
234 std::vector<ValueVariant> getValues();
236 std::vector<ValueVariant> m_values;
240 ValueVariant m_value;
243 AllowedValues m_allowedValues;
244 int m_updateInterval;
248 * API to get the number of attributes in the resource model.
250 * @return Number of attributes.
252 int size() const { return m_attributes.size(); }
255 * API to get the value of an attribute.
257 * @param attrName - Attribute name
258 * @param value - Attribute value
260 * @return true if attribute exists, otherwise false.
262 bool getAttribute(const std::string &attrName, Attribute &value);
265 * API to get the entire list of attributes in the form of key-value pair.
266 * Attribute name is the key and an instance of Attribute is the value.
268 * @return A map of all the attributes
270 std::map<std::string, Attribute> getAttributes() const;
273 * API to add new attribute to resource model.
275 * @param attrName - Attribute name
276 * @param attrValue - Attribute value
278 template <typename T>
279 void addAttribute(const std::string &attrName, const T &attrValue)
281 if (m_attributes.end() == m_attributes.find(attrName))
283 m_attributes[attrName] = Attribute(attrName);
284 m_attributes[attrName].setValue(attrValue);
289 * API to add new attribute to resource model.
291 * @param attr - Attribute pointer
294 void addAttribute(const Attribute &attribute);
297 * API to set range of attribute value.
299 * @param attrName - Attribute name.
300 * @param min - Minimum value could be set as attribute value.
301 * @param max - Maximum value could be set as attribute value.
303 void setRange(const std::string &attrName, const int min, const int max);
305 OC::OCRepresentation getOCRepresentation() const;
306 static std::shared_ptr<SimulatorResourceModel> create(const OC::OCRepresentation &ocRep);
308 template <typename T>
309 void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
311 if (m_attributes.end() != m_attributes.find(attrName))
312 m_attributes[attrName].setAllowedValues(values);
315 bool update(OC::OCRepresentation &ocRep);
317 bool update(std::shared_ptr<SimulatorResourceModel> &repModel);
319 template <typename T>
320 void updateAttribute(const std::string &attrName, const T &value)
322 if (m_attributes.end() != m_attributes.find(attrName))
323 m_attributes[attrName].setValue(value);
326 void updateAttributeFromAllowedValues(const std::string &attrName, unsigned int index);
328 void removeAttribute(const std::string &attrName);
330 void setUpdateInterval(const std::string &attrName, int interval);
333 std::map<std::string, Attribute> m_attributes;
336 typedef std::shared_ptr<SimulatorResourceModel> SimulatorResourceModelSP;
337 typedef std::shared_ptr<SimulatorResourceModel::Attribute> AttributeSP;