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 ******************************************************************/
21 #include "simulator_resource_model.h"
23 #include <boost/lexical_cast.hpp>
25 class to_string_visitor : public boost::static_visitor<std::string>
29 result_type operator ()(const T &value)
31 return boost::lexical_cast<std::string>(value);
35 class add_to_representation : public boost::static_visitor<>
38 add_to_representation(OC::OCRepresentation &rep, const std::string &key)
39 : m_rep(rep), m_key(key) {}
42 void operator ()(const T &value)
44 m_rep.setValue(m_key, value);
47 OC::OCRepresentation &&getRep()
49 return std::move(m_rep);
53 OC::OCRepresentation m_rep;
57 class range_validation : public boost::static_visitor<bool>
60 range_validation (SimulatorResourceModel::Attribute &attrItem)
61 : m_attrItem(attrItem) {}
63 bool operator ()(int &value)
66 m_attrItem.getRange(min, max);
67 if (value >= min && value <= max)
72 bool operator ()(double &value)
74 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
75 = m_attrItem.getAllowedValues();
76 for (SimulatorResourceModel::Attribute::ValueVariant & val : values)
78 SimulatorResourceModel::Attribute::ValueVariant vVal = value;
85 bool operator ()(std::string &value)
87 std::vector<SimulatorResourceModel::Attribute::ValueVariant> values
88 = m_attrItem.getAllowedValues();
89 for (SimulatorResourceModel::Attribute::ValueVariant & vVal : values)
91 std::string val = boost::get<std::string>(vVal);
100 SimulatorResourceModel::Attribute &m_attrItem;
103 SimulatorResourceModel::Attribute::ValueVariant
104 &SimulatorResourceModel::Attribute::AllowedValues::at(int index)
106 return m_values.at(index);
109 int SimulatorResourceModel::Attribute::AllowedValues::size() const
111 return m_values.size();
114 std::string SimulatorResourceModel::Attribute::AllowedValues::toString() const
116 std::ostringstream stream;
118 for (auto & value : m_values)
120 to_string_visitor visitor;
121 stream << boost::apply_visitor(visitor, value) << " ";
127 std::vector<std::string> SimulatorResourceModel::Attribute::AllowedValues::toVectorString() const
129 std::vector<std::string> values;
131 for (auto & value : m_values)
133 to_string_visitor visitor;
134 values.push_back(boost::apply_visitor(visitor, value));
139 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
140 SimulatorResourceModel::Attribute::AllowedValues::getValues()
145 std::string SimulatorResourceModel::Attribute::getName(void) const
150 void SimulatorResourceModel::Attribute::setName(const std::string &name)
155 void SimulatorResourceModel::Attribute::getRange(int &min, int &max) const
161 void SimulatorResourceModel::Attribute::setRange(const int &min, const int &max)
167 int SimulatorResourceModel::Attribute::getAllowedValuesSize() const
169 return m_allowedValues.size();
172 void SimulatorResourceModel::Attribute::setFromAllowedValue(const int allowedValueIndex)
174 m_value = m_allowedValues.at(allowedValueIndex);
177 std::string SimulatorResourceModel::Attribute::valueToString() const
179 to_string_visitor visitor;
180 return boost::apply_visitor(visitor, m_value);
183 std::string SimulatorResourceModel::Attribute::allowedValuesToString() const
185 return m_allowedValues.toString();
188 std::vector<std::string> SimulatorResourceModel::Attribute::allowedValuesToVectorString() const
190 return m_allowedValues.toVectorString();
193 void SimulatorResourceModel::Attribute::addValuetoRepresentation(OC::OCRepresentation &rep,
194 const std::string &key) const
196 add_to_representation visitor(rep, key);
197 boost::apply_visitor(visitor, m_value);
198 rep = visitor.getRep();
201 bool SimulatorResourceModel::Attribute::compare(SimulatorResourceModel::Attribute &attribute)
203 // Check the value types
204 if (m_value.which() != attribute.getValue().which())
209 // Check the value in allowed range
210 range_validation visitor(*this);
211 return boost::apply_visitor(visitor, attribute.getValue());
214 std::vector<SimulatorResourceModel::Attribute::ValueVariant>
215 SimulatorResourceModel::Attribute::getAllowedValues()
217 return m_allowedValues.getValues();
220 bool SimulatorResourceModel::getAttribute(const std::string &attrName, Attribute &value)
222 if (m_attributes.end() != m_attributes.find(attrName))
224 value = m_attributes[attrName];
231 std::map<std::string, SimulatorResourceModel::Attribute> SimulatorResourceModel::getAttributes()
237 void SimulatorResourceModel::setRange(const std::string &attrName, const int min, const int max)
239 if (m_attributes.end() != m_attributes.find(attrName))
240 m_attributes[attrName].setRange(min, max);
243 void SimulatorResourceModel::setUpdateInterval(const std::string &attrName, int interval)
245 if (m_attributes.end() != m_attributes.find(attrName))
246 m_attributes[attrName].setUpdateFrequencyTime(interval);
249 void SimulatorResourceModel::updateAttributeFromAllowedValues(const std::string &attrName,
250 const int allowedValueIndex)
252 if (m_attributes.end() != m_attributes.find(attrName))
253 m_attributes[attrName].setFromAllowedValue(allowedValueIndex);
256 void SimulatorResourceModel::removeAttribute(const std::string &attrName)
258 m_attributes.erase(attrName);
262 OC::OCRepresentation SimulatorResourceModel::getOCRepresentation() const
264 OC::OCRepresentation rep;
265 for (auto & attribute : m_attributes)
267 (attribute.second).addValuetoRepresentation(rep, attribute.first);
273 bool SimulatorResourceModel::update(OC::OCRepresentation &ocRep)
275 if (0 == ocRep.size())
278 // Convert OCRepresentation to SimulatorResourceModel
279 SimulatorResourceModel resModel = create(ocRep);
281 return update(resModel);
284 bool SimulatorResourceModel::update(SimulatorResourceModel &repModel)
286 std::map<std::string, SimulatorResourceModel::Attribute> attributes = repModel.getAttributes();
287 for (auto & attributeItem : attributes)
289 // Check the attribute presence
290 SimulatorResourceModel::Attribute attribute;
291 if (false == getAttribute((attributeItem.second).getName(), attribute))
296 // Check the validity of the value to be set
297 if (false == attribute.compare(attributeItem.second))
301 m_attributes[(attributeItem.second).getName()].setValue((attributeItem.second).getValue());
307 SimulatorResourceModel SimulatorResourceModel::create(const OC::OCRepresentation &ocRep)
309 SimulatorResourceModel resModel;
310 for (auto & attributeItem : ocRep)
312 SimulatorResourceModel::Attribute attribute;
313 if (attributeItem.type() == OC::AttributeType::Integer)
314 attribute.setValue(attributeItem.getValue<int>());
315 if (attributeItem.type() == OC::AttributeType::Double)
316 attribute.setValue(attributeItem.getValue<double>());
317 if (attributeItem.type() == OC::AttributeType::String)
318 attribute.setValue(attributeItem.getValue<std::string>());
320 attribute.setName(attributeItem.attrname());
321 resModel.m_attributes[attributeItem.attrname()] = attribute;