client-cpp  0.6.3
CommonValueTools.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef COMMONVALUETOOLS_HPP_
18 #define COMMONVALUETOOLS_HPP_
19 
20 #include "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_CONFIGURATION
23 
24 #include <memory>
25 #include <cstdint>
30 
32 
33 namespace kaa {
34 
35 class CommonValueTools {
36 public:
37  static bool isRecord(std::shared_ptr<ICommonValue> v) {
38  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_RECORD : false);
39  }
40 
41  static bool isArray(std::shared_ptr<ICommonValue> v) {
42  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_ARRAY : false);
43  }
44 
45  static bool isFixed(std::shared_ptr<ICommonValue> v) {
46  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_FIXED : false);
47  }
48 
49  static bool isEnum(std::shared_ptr<ICommonValue> v) {
50  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_ENUM : false);
51  }
52 
53  static bool isString(std::shared_ptr<ICommonValue> v) {
54  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_STRING : false);
55  }
56 
57  static bool isNumeric(std::shared_ptr<ICommonValue> v) {
58  return (v.get() ? (v->getCommonType() >= CommonValueType::COMMON_INT32 && v->getCommonType() <= CommonValueType::COMMON_DOUBLE) : false);
59  }
60 
61  static bool isBool(std::shared_ptr<ICommonValue> v) {
62  return (v.get() ? (v->getCommonType() == CommonValueType::COMMON_BOOL) : false);
63  }
64 
65  static bool isByteArray(std::shared_ptr<ICommonValue> v) {
66  return (v.get() ? (v->getCommonType() == CommonValueType::COMMON_BYTES || v->getCommonType() == CommonValueType::COMMON_FIXED) : false);
67  }
68 
69  static bool isNull(std::shared_ptr<ICommonValue> v) {
70  return (v.get() ? (v->getCommonType() == CommonValueType::COMMON_NULL) : false);
71  }
72 
73  static const CommonRecord getRecord(std::shared_ptr<ICommonValue> v) {
74  if (isRecord(v)) {
75  return boost::any_cast<const CommonRecord &>(v->getValue());
76  }
77  throw KaaException("Can not cast to record");
78  }
79 
80  static ICommonArray::container_type getList(std::shared_ptr<ICommonValue> v) {
81  if (isArray(v)) {
82  return boost::any_cast<const ICommonArray::container_type &>(v->getValue());
83  }
84  throw KaaException("Can not cast to array");
85  }
86 
87  static const std::string &getEnumValue(std::shared_ptr<ICommonValue> v) {
88  if (isEnum(v)) {
89  return boost::any_cast<const std::string &>(v->getValue());
90  }
91  throw KaaException("Can not cast to enum");
92  }
93 
94  static std::vector<std::uint8_t> getByteArray(std::shared_ptr<ICommonValue> v) {
95  if (isByteArray(v)) {
96  return boost::any_cast<const std::vector<std::uint8_t> &>(v->getValue());
97  }
98  throw KaaException("Can not cast to byte array");
99  }
100 };
101 
102 } // namespace kaa
103 
104 #endif
105 
106 #endif /* COMMONVALUETOOLS_HPP_ */