libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Variable.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef VARIABLE_H_
32 #define VARIABLE_H_
33 
37 
38 #include <vector>
39 #include <string>
40 #include <memory>
41 #include <map>
42 #include <list>
43 #include <cmath>
44 
45 using namespace rapidxml;
46 
47 namespace BaseLib {
48 
49 enum class VariableType {
50  tVoid = 0x00,
51  tInteger = 0x01,
52  tBoolean = 0x02,
53  tString = 0x03,
54  tFloat = 0x04,
55  tArray = 0x100,
56  tStruct = 0x101,
57  //rpcDate = 0x10,
58  tBase64 = 0x11,
59  tBinary = 0xD0,
60  tInteger64 = 0xD1,
61  tVariant = 0x1111,
62 };
63 
64 class Variable;
65 
66 typedef std::shared_ptr<Variable> PVariable;
67 typedef std::shared_ptr<PVariable> PPVariable;
68 typedef std::pair<std::string, PVariable> StructElement;
69 typedef std::map<std::string, PVariable> Struct;
70 typedef std::shared_ptr<std::map<std::string, PVariable>> PStruct;
71 typedef std::vector<PVariable> Array;
72 typedef std::shared_ptr<Array> PArray;
73 typedef std::list<PVariable> List;
74 typedef std::shared_ptr<List> PList;
75 
76 class Variable {
77  private:
78  typedef void (Variable::*bool_type)() const;
79 
80  void this_type_does_not_support_comparisons() const {}
81  std::string print(PVariable variable, std::string indent, bool ignoreIndentOnFirstLine, bool oneLine);
82  std::string printStruct(PStruct rpcStruct, std::string indent, bool ignoreIndentOnFirstLine, bool oneLine);
83  std::string printArray(PArray rpcArray, std::string indent, bool ignoreIndentOnFirstLine, bool oneLine);
84 
88  void parseXmlNode(const xml_node *node, PStruct &xmlStruct);
89  public:
90  bool errorStruct = false;
92  std::string stringValue;
93  int32_t integerValue = 0;
94  int64_t integerValue64 = 0;
95  double floatValue = 0;
96  bool booleanValue = false;
97  PArray arrayValue;
98  PStruct structValue;
99  std::vector<uint8_t> binaryValue;
100 
101  Variable();
102  Variable(Variable const &rhs);
103  explicit Variable(VariableType variableType);
104  explicit Variable(uint8_t integer);
105  explicit Variable(int32_t integer);
106  explicit Variable(uint32_t integer);
107  explicit Variable(int64_t integer);
108  explicit Variable(uint64_t integer);
109  explicit Variable(const std::string &string);
110  explicit Variable(const char *string);
111  explicit Variable(bool boolean);
112  explicit Variable(double floatVal);
113  explicit Variable(const PArray &arrayVal);
114  explicit Variable(const std::vector<std::string> &arrayVal);
115  explicit Variable(const PStruct &structVal);
116  explicit Variable(const std::vector<uint8_t> &binaryVal);
117  explicit Variable(const uint8_t *binaryVal, size_t binaryValSize);
118  explicit Variable(const std::vector<char> &binaryVal);
119  explicit Variable(const char *binaryVal, size_t binaryValSize);
120 
121  explicit Variable(DeviceDescription::ILogical::Type::Enum variableType);
122  explicit Variable(const xml_node *node);
123  virtual ~Variable();
124  static PVariable createError(int32_t faultCode, const std::string& faultString, bool retry = false);
125  std::string print(bool stdout = false, bool stderr = false, bool oneLine = false);
126  void trimStrings();
127  static std::string getTypeString(VariableType type);
128  void setType(VariableType value) { type = value; };
130  static PVariable fromString(std::string &value, DeviceDescription::ILogical::Type::Enum type);
131  static PVariable fromString(std::string &value, DeviceDescription::IPhysical::Type::Enum type);
132  static PVariable fromString(std::string &value, VariableType type);
133  std::string toString();
134  Variable &operator=(const Variable &rhs);
135  bool operator==(const Variable &rhs);
136  bool operator<(const Variable &rhs);
137  bool operator<=(const Variable &rhs);
138  bool operator>(const Variable &rhs);
139  bool operator>=(const Variable &rhs);
140  bool operator!=(const Variable &rhs);
141  operator bool_type() const;
142 };
143 
144 }
145 
146 #endif
std::vector< uint8_t > binaryValue
Definition: Variable.h:99
std::shared_ptr< std::map< std::string, PVariable > > PStruct
Definition: Variable.h:70
PArray arrayValue
Definition: Variable.h:97
Definition: BaseLib.cpp:34
std::vector< PVariable > Array
Definition: Variable.h:71
std::shared_ptr< Array > PArray
Definition: Variable.h:72
VariableType type
Definition: Variable.h:91
VariableType
Definition: Variable.h:49
std::list< PVariable > List
Definition: Variable.h:73
PVariable value
Definition: UiElements.h:217
std::shared_ptr< Variable > PVariable
Definition: PhysicalInterfaceSettings.h:41
void setType(VariableType value)
Definition: Variable.h:128
std::shared_ptr< List > PList
Definition: Variable.h:74
std::map< std::string, PVariable > Struct
Definition: Variable.h:69
Definition: Variable.h:76
std::shared_ptr< PVariable > PPVariable
Definition: Variable.h:67
std::pair< std::string, PVariable > StructElement
Definition: Variable.h:68
Class representing a node of XML document.
Definition: rapidxml.h:539
std::string stringValue
Definition: Variable.h:92
Definition: BinaryPayload.h:38
PStruct structValue
Definition: Variable.h:98