Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / src / common / attribute_value_generator.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 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 #include "attribute_value_generator.h"
22
23 std::unique_ptr<AttributeValueGen> AttributeValueGenFactory::create(
24     const std::shared_ptr<AttributeProperty> &property)
25 {
26     if (!property)
27         return nullptr;
28
29     if (property->isInteger())
30     {
31         std::shared_ptr<IntegerProperty> intProperty = property->asInteger();
32         if (intProperty->hasRange())
33         {
34             int min = 0;
35             int max = 0;
36             intProperty->getRange(min, max);
37             return std::unique_ptr<AttributeValueGen>(
38                        new RangeValueGen<int>(min, max));
39         }
40         else if (intProperty->hasValues())
41         {
42             std::vector<int> values;
43             intProperty->getValues(values);
44             return std::unique_ptr<AttributeValueGen>(
45                        new ValuesSetGen<int>(values));
46         }
47     }
48     else if (property->isDouble())
49     {
50         std::shared_ptr<DoubleProperty> doubleProperty = property->asDouble();
51         if (doubleProperty->hasRange())
52         {
53             double min = 0.0;
54             double max = 0.0;
55             doubleProperty->getRange(min, max);
56             return std::unique_ptr<AttributeValueGen>(
57                        new RangeValueGen<double>(min, max));
58         }
59         else if (doubleProperty->hasValues())
60         {
61             std::vector<double> values;
62             doubleProperty->getValues(values);
63             return std::unique_ptr<AttributeValueGen>(
64                        new ValuesSetGen<double>(values));
65         }
66     }
67     else if (property->isBoolean())
68     {
69         return std::unique_ptr<AttributeValueGen>(new ValuesSetGen<bool>({true, false}));
70     }
71     else if (property->isString())
72     {
73         std::shared_ptr<StringProperty> stringProperty = property->asString();
74         if (stringProperty->hasValues())
75         {
76             std::vector<std::string> values;
77             stringProperty->getValues(values);
78             return std::unique_ptr<AttributeValueGen>(
79                        new ValuesSetGen<std::string>(values));
80         }
81     }
82
83     return nullptr;
84 }