client-cpp  0.0.1-SNAPSHOT
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 <boost/smart_ptr/shared_ptr.hpp>
25 
27 
28 namespace kaa {
29 
31 public:
32  static bool isRecord(boost::shared_ptr<ICommonValue> v) {
33  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_RECORD : false);
34  }
35 
36  static bool isArray(boost::shared_ptr<ICommonValue> v) {
37  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_ARRAY : false);
38  }
39 
40  static bool isFixed(boost::shared_ptr<ICommonValue> v) {
41  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_FIXED : false);
42  }
43 
44  static bool isEnum(boost::shared_ptr<ICommonValue> v) {
45  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_ENUM : false);
46  }
47 
48  static bool isString(boost::shared_ptr<ICommonValue> v) {
49  return (v.get() ? v->getCommonType() == CommonValueType::COMMON_STRING : false);
50  }
51 
52  static bool isNumeric(boost::shared_ptr<ICommonValue> v) {
53  return (v.get() ? (v->getCommonType() >= CommonValueType::COMMON_INT32 && v->getCommonType() <= CommonValueType::COMMON_DOUBLE) : false);
54  }
55 
56  static bool isBool(boost::shared_ptr<ICommonValue> v) {
57  return (v.get() ? (v->getCommonType() == CommonValueType::COMMON_BOOL) : false);
58  }
59 
60  static bool isByteArray(boost::shared_ptr<ICommonValue> v) {
61  return (v.get() ? (v->getCommonType() == CommonValueType::COMMON_BYTES || v->getCommonType() == CommonValueType::COMMON_FIXED) : false);
62  }
63 
64  static bool isNull(boost::shared_ptr<ICommonValue> v) {
65  return (v.get() ? (v->getCommonType() == CommonValueType::COMMON_NULL) : false);
66  }
67 
68  static const CommonRecord getRecord(boost::shared_ptr<ICommonValue> v) {
69  if (isRecord(v)) {
70  return boost::any_cast<const CommonRecord &>(v->getValue());
71  }
72  throw KaaException("Can not cast to record");
73  }
74 
75  static ICommonArray::container_type getList(boost::shared_ptr<ICommonValue> v) {
76  if (isArray(v)) {
77  return boost::any_cast<const ICommonArray::container_type &>(v->getValue());
78  }
79  throw KaaException("Can not cast to array");
80  }
81 
82  static const std::string &getEnumValue(boost::shared_ptr<ICommonValue> v) {
83  if (isEnum(v)) {
84  return boost::any_cast<const std::string &>(v->getValue());
85  }
86  throw KaaException("Can not cast to enum");
87  }
88 
89  static std::vector<boost::uint8_t> getByteArray(boost::shared_ptr<ICommonValue> v) {
90  if (isByteArray(v)) {
91  return boost::any_cast<const std::vector<boost::uint8_t> &>(v->getValue());
92  }
93  throw KaaException("Can not cast to byte array");
94  }
95 };
96 
97 } // namespace kaa
98 
99 
100 #endif /* COMMONVALUETOOLS_HPP_ */
static bool isByteArray(boost::shared_ptr< ICommonValue > v)
static bool isBool(boost::shared_ptr< ICommonValue > v)
static bool isArray(boost::shared_ptr< ICommonValue > v)
static std::vector< boost::uint8_t > getByteArray(boost::shared_ptr< ICommonValue > v)
static bool isRecord(boost::shared_ptr< ICommonValue > v)
static const std::string & getEnumValue(boost::shared_ptr< ICommonValue > v)
static bool isString(boost::shared_ptr< ICommonValue > v)
static const CommonRecord getRecord(boost::shared_ptr< ICommonValue > v)
static bool isEnum(boost::shared_ptr< ICommonValue > v)
static bool isNull(boost::shared_ptr< ICommonValue > v)
static bool isFixed(boost::shared_ptr< ICommonValue > v)
static ICommonArray::container_type getList(boost::shared_ptr< ICommonValue > v)
static bool isNumeric(boost::shared_ptr< ICommonValue > v)