Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / inc / simulator_resource_model.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 /**
22  * @file   simulator_resource_model.h
23  *
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.
26  */
27
28 #ifndef SIMULATOR_RESOURCE_MODEL_H_
29 #define SIMULATOR_RESOURCE_MODEL_H_
30
31 #include <map>
32 #include <set>
33
34 #include "OCPlatform.h"
35
36 class SimulatorResourceModel;
37 typedef boost::variant <
38 int,
39 double,
40 bool,
41 std::string,
42 SimulatorResourceModel,
43
44 std::vector<int>,
45 std::vector<double>,
46 std::vector<bool>,
47 std::vector<std::string>,
48 std::vector<SimulatorResourceModel>,
49
50 std::vector<std::vector<int>>,
51 std::vector<std::vector<double>>,
52 std::vector<std::vector<bool>>,
53 std::vector<std::vector<std::string>>,
54 std::vector<std::vector<SimulatorResourceModel>>,
55
56 std::vector<std::vector<std::vector<int>>>,
57 std::vector<std::vector<std::vector<double>>>,
58 std::vector<std::vector<std::vector<bool>>>,
59 std::vector<std::vector<std::vector<std::string>>>,
60 std::vector<std::vector<std::vector<SimulatorResourceModel>>>
61 > AttributeValueVariant;
62
63 enum class AttributeValueType
64 {
65     UNKNOWN,
66     INTEGER,
67     DOUBLE,
68     BOOLEAN,
69     STRING,
70     RESOURCE_MODEL,
71     VECTOR
72 };
73
74 class OCRepresentationBuilder;
75 class ToStringConverter;
76 class AttributeProperty;
77
78 /**
79  * @class   SimulatorResourceModel
80  * @brief   This class provides a set of functions for accessing and manipulating the resource model.
81  */
82 class SimulatorResourceModel
83 {
84     public:
85         friend class OCRepresentationBuilder;
86         friend class ToStringConverter;
87
88         class TypeInfo
89         {
90             public:
91                 TypeInfo(AttributeValueType type = AttributeValueType::UNKNOWN,
92                          AttributeValueType baseType = AttributeValueType::UNKNOWN, int depth = 0);
93                 AttributeValueType type() const;
94                 AttributeValueType baseType() const;
95                 int depth() const;
96                 bool operator ==(const TypeInfo &rhs) const;
97
98             private:
99                 AttributeValueType m_type;
100                 AttributeValueType m_baseType;
101                 int m_depth;
102         };
103
104         template <typename T>
105         bool add(const std::string &name, T value)
106         {
107             if (!name.empty() && m_attributes.end() == m_attributes.find(name))
108             {
109                 AttributeValueVariant newValue = value;
110                 m_attributes[name] = value;
111                 return true;
112             }
113             return false;
114         }
115
116         template <typename T>
117         T get(const std::string &name) const
118         {
119             T val = T();
120             auto x = m_attributes.find(name);
121             if (x != m_attributes.end())
122             {
123                 try
124                 {
125                     val = boost::get<T>(x->second);
126                 }
127                 catch (boost::bad_get &e)
128                 {
129                     return val;
130                 }
131             }
132             return val;
133         }
134
135         template <typename T>
136         bool update(const std::string &name, T value)
137         {
138             AttributeValueVariant newValue = value;
139             return updateValue(name, newValue);
140         }
141
142         bool remove(const std::string &name);
143
144         bool contains(const std::string &name) const;
145
146         size_t size() const;
147
148         TypeInfo getType(const std::string &name) const;
149
150         std::map<std::string, AttributeValueVariant> getAttributeValues() const;
151
152         AttributeValueVariant getAttributeValue(const std::string &name) const;
153
154         std::set<std::string> getAttributeNameSet() const;
155
156         OC::OCRepresentation asOCRepresentation() const;
157
158         std::string asString() const;
159
160         static SimulatorResourceModel build(const OC::OCRepresentation &ocRep);
161
162     private:
163         TypeInfo getTypeInfo(const AttributeValueVariant &value) const;
164         bool updateValue(const std::string &name, const AttributeValueVariant &value);
165
166         std::map<std::string, AttributeValueVariant> m_attributes;
167 };
168
169 class SimulatorResourceAttribute
170 {
171     public:
172         SimulatorResourceAttribute() = default;
173         SimulatorResourceAttribute(const std::string &name);
174         SimulatorResourceAttribute(const std::string &name, const AttributeValueVariant &value);
175
176         std::string getName() const;
177         const SimulatorResourceModel::TypeInfo getType() const;
178         std::shared_ptr<AttributeProperty> getProperty() const;
179         AttributeValueVariant getValue() const;
180
181         void setName(const std::string &name);
182         void setProperty(const std::shared_ptr<AttributeProperty> &property);
183
184         template <typename T>
185         void setValue(const T &value)
186         {
187             m_value = std::make_shared<AttributeValueVariant>(value);
188         }
189
190         std::string asString() const;
191
192     private:
193         std::string m_name;
194         std::shared_ptr<AttributeValueVariant> m_value;
195         std::shared_ptr<AttributeProperty> m_property;
196 };
197
198 typedef std::shared_ptr<SimulatorResourceModel> SimulatorResourceModelSP;
199
200 #endif