Qpid C++ Messaging API  1.37.0
Variant.h
1 #ifndef QPID_TYPES_VARIANT_H
2 #define QPID_TYPES_VARIANT_H
3 
4 /*
5  *
6  * Licensed to the Apache Software Foundation (ASF) under one
7  * or more contributor license agreements. See the NOTICE file
8  * distributed with this work for additional information
9  * regarding copyright ownership. The ASF licenses this file
10  * to you under the Apache License, Version 2.0 (the
11  * "License"); you may not use this file except in compliance
12  * with the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing,
17  * software distributed under the License is distributed on an
18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19  * KIND, either express or implied. See the License for the
20  * specific language governing permissions and limitations
21  * under the License.
22  *
23  */
24 #include <list>
25 #include <map>
26 #include <ostream>
27 #include <string>
28 #include "Uuid.h"
29 #include "qpid/types/Exception.h"
30 #include "qpid/sys/IntegerTypes.h"
31 #include "qpid/types/ImportExport.h"
32 
33 namespace qpid {
34 namespace types {
35 
39 struct QPID_TYPES_CLASS_EXTERN InvalidConversion : public Exception
40 {
41  QPID_TYPES_EXTERN InvalidConversion(const std::string& msg);
42  QPID_TYPES_EXTERN ~InvalidConversion() throw();
43 };
44 
45 enum VariantType {
46  VAR_VOID = 0,
47  VAR_BOOL,
48  VAR_UINT8,
49  VAR_UINT16,
50  VAR_UINT32,
51  VAR_UINT64,
52  VAR_INT8,
53  VAR_INT16,
54  VAR_INT32,
55  VAR_INT64,
56  VAR_FLOAT,
57  VAR_DOUBLE,
58  VAR_STRING,
59  VAR_MAP,
60  VAR_LIST,
61  VAR_UUID
62 };
63 
64 QPID_TYPES_EXTERN std::string getTypeName(VariantType type);
65 
66 QPID_TYPES_EXTERN bool isIntegerType(VariantType type);
67 
68 class VariantImpl;
69 
73 class QPID_TYPES_CLASS_EXTERN Variant
74 {
75  public:
76  typedef std::map<std::string, Variant> Map;
77  typedef std::list<Variant> List;
78 
79  QPID_TYPES_EXTERN Variant();
80  QPID_TYPES_EXTERN Variant(bool);
81  QPID_TYPES_EXTERN Variant(uint8_t);
82  QPID_TYPES_EXTERN Variant(uint16_t);
83  QPID_TYPES_EXTERN Variant(uint32_t);
84  QPID_TYPES_EXTERN Variant(uint64_t);
85  QPID_TYPES_EXTERN Variant(int8_t);
86  QPID_TYPES_EXTERN Variant(int16_t);
87  QPID_TYPES_EXTERN Variant(int32_t);
88  QPID_TYPES_EXTERN Variant(int64_t);
89  QPID_TYPES_EXTERN Variant(float);
90  QPID_TYPES_EXTERN Variant(double);
91  QPID_TYPES_EXTERN Variant(const std::string&);
92  QPID_TYPES_EXTERN Variant(const std::string& value, const std::string& encoding);
93  QPID_TYPES_EXTERN Variant(const char*);
94  QPID_TYPES_EXTERN Variant(const char* value, const char* encoding);
95  QPID_TYPES_EXTERN Variant(const Map&);
96  QPID_TYPES_EXTERN Variant(const List&);
97  QPID_TYPES_EXTERN Variant(const Variant&);
98  QPID_TYPES_EXTERN Variant(const Uuid&);
99 
100  QPID_TYPES_EXTERN ~Variant();
101 
102  QPID_TYPES_EXTERN VariantType getType() const;
103  QPID_TYPES_EXTERN bool isVoid() const;
104 
105  QPID_TYPES_EXTERN Variant& operator=(bool);
106  QPID_TYPES_EXTERN Variant& operator=(uint8_t);
107  QPID_TYPES_EXTERN Variant& operator=(uint16_t);
108  QPID_TYPES_EXTERN Variant& operator=(uint32_t);
109  QPID_TYPES_EXTERN Variant& operator=(uint64_t);
110  QPID_TYPES_EXTERN Variant& operator=(int8_t);
111  QPID_TYPES_EXTERN Variant& operator=(int16_t);
112  QPID_TYPES_EXTERN Variant& operator=(int32_t);
113  QPID_TYPES_EXTERN Variant& operator=(int64_t);
114  QPID_TYPES_EXTERN Variant& operator=(float);
115  QPID_TYPES_EXTERN Variant& operator=(double);
116  QPID_TYPES_EXTERN Variant& operator=(const std::string&);
117  QPID_TYPES_EXTERN Variant& operator=(const char*);
118  QPID_TYPES_EXTERN Variant& operator=(const Map&);
119  QPID_TYPES_EXTERN Variant& operator=(const List&);
120  QPID_TYPES_EXTERN Variant& operator=(const Variant&);
121  QPID_TYPES_EXTERN Variant& operator=(const Uuid&);
122 
127  QPID_TYPES_EXTERN Variant& parse(const std::string&);
128 
129  QPID_TYPES_EXTERN bool asBool() const;
130  QPID_TYPES_EXTERN uint8_t asUint8() const;
131  QPID_TYPES_EXTERN uint16_t asUint16() const;
132  QPID_TYPES_EXTERN uint32_t asUint32() const;
133  QPID_TYPES_EXTERN uint64_t asUint64() const;
134  QPID_TYPES_EXTERN int8_t asInt8() const;
135  QPID_TYPES_EXTERN int16_t asInt16() const;
136  QPID_TYPES_EXTERN int32_t asInt32() const;
137  QPID_TYPES_EXTERN int64_t asInt64() const;
138  QPID_TYPES_EXTERN float asFloat() const;
139  QPID_TYPES_EXTERN double asDouble() const;
140  QPID_TYPES_EXTERN std::string asString() const;
141  QPID_TYPES_EXTERN Uuid asUuid() const;
142 
143  QPID_TYPES_EXTERN operator bool() const;
144  QPID_TYPES_EXTERN operator uint8_t() const;
145  QPID_TYPES_EXTERN operator uint16_t() const;
146  QPID_TYPES_EXTERN operator uint32_t() const;
147  QPID_TYPES_EXTERN operator uint64_t() const;
148  QPID_TYPES_EXTERN operator int8_t() const;
149  QPID_TYPES_EXTERN operator int16_t() const;
150  QPID_TYPES_EXTERN operator int32_t() const;
151  QPID_TYPES_EXTERN operator int64_t() const;
152  QPID_TYPES_EXTERN operator float() const;
153  QPID_TYPES_EXTERN operator double() const;
154  QPID_TYPES_EXTERN operator std::string() const;
155  QPID_TYPES_EXTERN operator Uuid() const;
156 
157  QPID_TYPES_EXTERN const Map& asMap() const;
158  QPID_TYPES_EXTERN Map& asMap();
159  QPID_TYPES_EXTERN const List& asList() const;
160  QPID_TYPES_EXTERN List& asList();
161 
166  QPID_TYPES_EXTERN const std::string& getString() const;
167  QPID_TYPES_EXTERN std::string& getString();
168 
169  QPID_TYPES_EXTERN void setEncoding(const std::string&);
170  QPID_TYPES_EXTERN const std::string& getEncoding() const;
171 
172  QPID_TYPES_EXTERN bool isEqualTo(const Variant& a) const;
173 
175  QPID_TYPES_EXTERN void reset();
176 
178  QPID_TYPES_EXTERN bool isDescribed() const;
179 
188  QPID_TYPES_EXTERN Variant getDescriptor() const;
189 
191  QPID_TYPES_EXTERN void setDescriptor(const Variant& descriptor);
192 
197  QPID_TYPES_EXTERN List& getDescriptors();
198 
203  QPID_TYPES_EXTERN const List& getDescriptors() const;
204 
206  QPID_TYPES_EXTERN static Variant described(const Variant& descriptor, const Variant& value);
207 
209  QPID_TYPES_EXTERN static Variant described(const Variant& descriptor, const List& value);
210 
211  private:
212  mutable VariantImpl* impl;
213 };
214 
215 #ifndef SWIG
216 QPID_TYPES_EXTERN std::ostream& operator<<(std::ostream& out, const Variant& value);
217 QPID_TYPES_EXTERN std::ostream& operator<<(std::ostream& out, const Variant::Map& map);
218 QPID_TYPES_EXTERN std::ostream& operator<<(std::ostream& out, const Variant::List& list);
219 QPID_TYPES_EXTERN bool operator==(const Variant& a, const Variant& b);
220 QPID_TYPES_EXTERN bool operator!=(const Variant& a, const Variant& b);
221 #endif
222 }} // namespace qpid::types
223 
224 #endif
Definition: Exception.h:31
Definition: Variant.h:73
Definition: Uuid.h:32
Definition: Variant.h:39
Definition: Address.h:32