1 /******************************************************************
3 * Copyright 2016 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 #ifndef SIMULATOR_RESOURCE_MODEL_SCHEMA_H_
22 #define SIMULATOR_RESOURCE_MODEL_SCHEMA_H_
24 #include <unordered_map>
27 #include "simulator_resource_model.h"
29 class IntegerProperty;
31 class BooleanProperty;
35 class AttributeProperty
48 explicit AttributeProperty(Type type);
49 virtual ~AttributeProperty() {};
52 virtual bool isInteger() const { return false; }
53 virtual bool isDouble() const { return false; }
54 virtual bool isBoolean() const { return false; }
55 virtual bool isString() const { return false; }
56 virtual bool isArray() const { return false; }
57 virtual bool isModel() const { return false; }
58 virtual std::shared_ptr<IntegerProperty> asInteger() { return nullptr; }
59 virtual std::shared_ptr<DoubleProperty> asDouble() { return nullptr; }
60 virtual std::shared_ptr<BooleanProperty> asBoolean() { return nullptr; }
61 virtual std::shared_ptr<StringProperty> asString() { return nullptr; }
62 virtual std::shared_ptr<ArrayProperty> asArray() { return nullptr; }
63 virtual std::shared_ptr<ModelProperty> asModel() { return nullptr; }
65 virtual bool validate(const AttributeValueVariant &value) = 0;
66 virtual AttributeValueVariant buildValue() = 0;
72 class IntegerProperty : public AttributeProperty,
73 public std::enable_shared_from_this<IntegerProperty>
76 static std::shared_ptr<IntegerProperty> build(int defaultValue = 0);
78 bool isInteger() const;
79 std::shared_ptr<IntegerProperty> asInteger();
80 void setDefaultValue(int value);
81 void setRange(int min, int max);
82 void setValues(const std::vector<int> &values);
83 bool hasRange() const;
84 bool hasValues() const;
85 int getDefaultValue() const;
86 bool getRange(int &min, int &max) const;
87 bool getValues(std::vector<int> &values) const;
88 bool validate(const AttributeValueVariant &value);
89 bool validate(const int &value);
90 AttributeValueVariant buildValue();
93 explicit IntegerProperty(int defaultValue);
98 std::vector<int> m_values;
102 class DoubleProperty : public AttributeProperty,
103 public std::enable_shared_from_this<DoubleProperty>
106 static std::shared_ptr<DoubleProperty> build(double defaultValue = 0.0);
108 bool isDouble() const;
109 std::shared_ptr<DoubleProperty> asDouble();
110 void setDefaultValue(double value);
111 void setRange(double min, double max);
112 void setValues(const std::vector<double> &values);
113 bool hasRange() const;
114 bool hasValues() const;
115 double getDefaultValue() const;
116 bool getRange(double &min, double &max) const;
117 bool getValues(std::vector<double> &values) const;
118 bool validate(const AttributeValueVariant &value);
119 bool validate(const double &value);
120 AttributeValueVariant buildValue();
123 explicit DoubleProperty(double defaultValue);
125 double m_defaultValue;
128 std::vector<double> m_values;
132 class BooleanProperty : public AttributeProperty,
133 public std::enable_shared_from_this<BooleanProperty>
136 static std::shared_ptr<BooleanProperty> build(bool defaultValue = true);
138 bool isBoolean() const;
139 std::shared_ptr<BooleanProperty> asBoolean();
140 void setDefaultValue(bool value);
141 bool getDefaultValue() const;
142 bool validate(const AttributeValueVariant &value);
143 AttributeValueVariant buildValue();
146 explicit BooleanProperty(bool defaultValue);
151 class StringProperty : public AttributeProperty,
152 public std::enable_shared_from_this<StringProperty>
155 static std::shared_ptr<StringProperty> build(const std::string &defaultValue = "");
157 bool isString() const;
158 std::shared_ptr<StringProperty> asString();
159 void setDefaultValue(const std::string &value);
160 void setRange(size_t min, size_t max);
161 void setValues(const std::vector<std::string> &values);
162 bool hasRange() const;
163 bool hasValues() const;
164 std::string getDefaultValue() const;
165 bool getRange(size_t &min, size_t &max) const;
166 bool getValues(std::vector<std::string> &values) const;
167 bool validate(const AttributeValueVariant &value);
168 bool validate(const std::string &value);
169 AttributeValueVariant buildValue();
172 StringProperty(const std::string &defaultValue);
174 std::string m_defaultValue;
177 std::vector<std::string> m_values;
181 class ArrayProperty : public AttributeProperty,
182 public std::enable_shared_from_this<ArrayProperty>
185 static std::shared_ptr<ArrayProperty> build();
187 bool isArray() const;
188 std::shared_ptr<ArrayProperty> asArray();
189 void setRange(size_t minItems, size_t maxItems);
190 void setVariable(bool state);
191 void setUnique(bool state);
192 bool setElementProperty(const std::shared_ptr<AttributeProperty> &property);
193 bool hasRange() const;
194 bool isVariable() const;
195 bool isUnique() const;
196 size_t getMinItems() const;
197 size_t getMaxItems() const;
198 std::shared_ptr<AttributeProperty> getElementProperty();
199 bool validate(const AttributeValueVariant &value);
200 AttributeValueVariant buildValue();
204 int findDepth(std::shared_ptr<AttributeProperty> &elementProperty);
208 bool m_isVariableSize;
210 std::shared_ptr<AttributeProperty> m_elementProperty;
214 class ModelProperty : public AttributeProperty,
215 public std::enable_shared_from_this<ModelProperty>
218 static std::shared_ptr<ModelProperty> build();
220 bool isModel() const;
221 std::shared_ptr<ModelProperty> asModel();
222 bool add(const std::string &name, const std::shared_ptr<AttributeProperty> &property,
223 bool required = false);
224 std::shared_ptr<AttributeProperty> get(const std::string &name);
225 std::unordered_map<std::string, std::shared_ptr<AttributeProperty>> getChildProperties();
226 bool isRequired(const std::string &name);
227 void remove(const std::string &name);
228 void setRequired(const std::string &name);
229 void unsetRequired(const std::string &name);
230 SimulatorResourceModel buildResourceModel();
231 bool validate(const AttributeValueVariant &value);
232 bool validate(const SimulatorResourceModel &model);
233 AttributeValueVariant buildValue();
238 std::unordered_map<std::string, bool> m_requiredAttributes;
239 std::unordered_map<std::string, std::shared_ptr<AttributeProperty>> m_childProperties;
242 typedef ModelProperty SimulatorResourceModelSchema;