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_ATTRIBUTE_H_
29 #define SIMULATOR_RESOURCE_ATTRIBUTE_H_
33 #include "OCPlatform.h"
36 class SimulatorResourceServer;
38 * @class SimulatorResourceModel
39 * @brief This class provides a set of functions for accessing and manipulating the resource model.
41 class SimulatorResourceModel
43 friend class SimulatorResourceServer;
47 SimulatorResourceModel() = default;
48 SimulatorResourceModel(SimulatorResourceModel &&) = default;
49 SimulatorResourceModel(const SimulatorResourceModel &) = default;
50 SimulatorResourceModel &operator=(const SimulatorResourceModel &) = default;
51 SimulatorResourceModel &operator=(SimulatorResourceModel &&) = default;
53 virtual ~SimulatorResourceModel() {}
57 * @brief This class represents a resource attribute whose values can be generic.
62 typedef boost::variant <
68 Attribute() = default;
69 Attribute(const std::string &attrName) : m_name(attrName) {}
71 std::string getName(void) const;
72 void setName(const std::string &name);
78 return boost::get<T>(m_value);
81 ValueVariant &getValue()
86 int getValueType() const
88 return m_value.which();
92 void setValue(const T &value)
97 void setFromAllowedValue(const int allowedValueIndex);
99 void getRange(int &min, int &max) const;
101 void setRange(const int &min, const int &max);
103 template <typename T>
104 bool setAllowedValues(const std::vector<T> &values)
106 ValueVariant temp = values.at(0);
107 if (temp.which() != m_value.which())
112 m_allowedValues.addValues(values);
117 * This method is used to get the size of the allowed values.
119 * @return Size of the allowed values
121 int getAllowedValuesSize() const;
124 * This method is used to get the string representation of the value.
126 * @return Attribute's value as a string
128 std::string valueToString() const;
131 * This method is used to get the string representation of all the allowed values.
133 * @return All allowed values as a string
135 std::string allowedValuesToString() const;
136 std::vector<std::string> allowedValuesToVectorString() const;
138 void addValuetoRepresentation(OC::OCRepresentation &rep,
139 const std::string &key) const;
141 bool compare(Attribute &attribute);
143 std::vector<ValueVariant> getAllowedValues();
145 int getUpdateFrequencyTime() {return m_updateInterval;}
146 void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;}
152 template <typename T>
153 void addValue(const T &value)
155 ValueVariant temp = value;
156 m_values.push_back(temp);
159 template <typename T>
160 void addValues(const std::vector<T> &values)
162 for (auto value : values)
164 ValueVariant vValue = value;
165 m_values.push_back(vValue);
169 ValueVariant &at(int index);
171 std::string toString() const;
172 std::vector<std::string> toVectorString() const;
173 std::vector<ValueVariant> getValues();
175 std::vector<ValueVariant> m_values;
179 ValueVariant m_value;
182 AllowedValues m_allowedValues;
183 int m_updateInterval;
187 * This method is used to get the number of attributes in the resource.
189 * @return Count of attributes
191 int size() const { return m_attributes.size(); }
194 * This method is used to get the value of an attribute.
196 * @param attrName - Attribute name
197 * @param value - Attribute value
199 * @return Boolean, true if attribute exists, otherwise false.
201 bool getAttribute(const std::string &attrName, Attribute &value);
204 * This method is used to get the entire list of attributes in the form of key-value pair.
205 * Attribute name is the key and an instance of Attribute is the value.
207 * @return A map of all the attributes
209 std::map<std::string, Attribute> getAttributes() const;
211 static SimulatorResourceModel create(const OC::OCRepresentation &ocRep);
213 template <typename T>
214 void addAttribute(const std::string &attrName, const T &attrValue)
216 if (m_attributes.end() == m_attributes.find(attrName))
218 m_attributes[attrName] = Attribute(attrName);
219 m_attributes[attrName].setValue(attrValue);
224 void setRange(const std::string &attrName, const int min, const int max);
226 template <typename T>
227 void setAllowedValues(const std::string &attrName, const std::vector<T> &values)
229 if (m_attributes.end() != m_attributes.find(attrName))
230 m_attributes[attrName].setAllowedValues(values);
233 void setUpdateInterval(const std::string &attrName, int interval);
235 template <typename T>
236 void updateAttribute(const std::string &attrName, const T &value)
238 if (m_attributes.end() != m_attributes.find(attrName))
239 m_attributes[attrName].setValue(value);
242 void updateAttributeFromAllowedValues(const std::string &attrName, const int allowedValueIndex);
244 void removeAttribute(const std::string &attrName);
246 OC::OCRepresentation getOCRepresentation() const;
248 bool update(OC::OCRepresentation &ocRep);
250 bool update(SimulatorResourceModel &repModel);
252 std::map<std::string, Attribute> m_attributes;