Qpid Proton C++  0.12.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
scalar.hpp
1 #ifndef SCALAR_HPP
2 #define SCALAR_HPP
3 
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22 
23 #include "proton/comparable.hpp"
24 #include "proton/types.hpp"
25 
26 #include <iosfwd>
27 #include <string>
28 
29 namespace proton {
30 
31 class encoder;
32 class decoder;
33 
35 class scalar : public comparable<scalar> {
36  public:
38  PN_CPP_EXTERN scalar();
39 
41  PN_CPP_EXTERN scalar(const scalar&);
42 
44  PN_CPP_EXTERN scalar& operator=(const scalar&);
45 
47  PN_CPP_EXTERN type_id type() const;
48 
50  PN_CPP_EXTERN bool empty() const;
51 
57  PN_CPP_EXTERN scalar& operator=(bool);
58  PN_CPP_EXTERN scalar& operator=(uint8_t);
59  PN_CPP_EXTERN scalar& operator=(int8_t);
60  PN_CPP_EXTERN scalar& operator=(uint16_t);
61  PN_CPP_EXTERN scalar& operator=(int16_t);
62  PN_CPP_EXTERN scalar& operator=(uint32_t);
63  PN_CPP_EXTERN scalar& operator=(int32_t);
64  PN_CPP_EXTERN scalar& operator=(uint64_t);
65  PN_CPP_EXTERN scalar& operator=(int64_t);
66  PN_CPP_EXTERN scalar& operator=(wchar_t);
67  PN_CPP_EXTERN scalar& operator=(float);
68  PN_CPP_EXTERN scalar& operator=(double);
69  PN_CPP_EXTERN scalar& operator=(amqp_timestamp);
70  PN_CPP_EXTERN scalar& operator=(const amqp_decimal32&);
71  PN_CPP_EXTERN scalar& operator=(const amqp_decimal64&);
72  PN_CPP_EXTERN scalar& operator=(const amqp_decimal128&);
73  PN_CPP_EXTERN scalar& operator=(const amqp_uuid&);
74  PN_CPP_EXTERN scalar& operator=(const amqp_string&);
75  PN_CPP_EXTERN scalar& operator=(const amqp_symbol&);
76  PN_CPP_EXTERN scalar& operator=(const amqp_binary&);
77  PN_CPP_EXTERN scalar& operator=(const std::string& s);
78  PN_CPP_EXTERN scalar& operator=(const char* s);
79 
82  template <class T> explicit scalar(T x) { *this = x; }
83 
90  PN_CPP_EXTERN void get(bool&) const;
91  PN_CPP_EXTERN void get(uint8_t&) const;
92  PN_CPP_EXTERN void get(int8_t&) const;
93  PN_CPP_EXTERN void get(uint16_t&) const;
94  PN_CPP_EXTERN void get(int16_t&) const;
95  PN_CPP_EXTERN void get(uint32_t&) const;
96  PN_CPP_EXTERN void get(int32_t&) const;
97  PN_CPP_EXTERN void get(uint64_t&) const;
98  PN_CPP_EXTERN void get(int64_t&) const;
99  PN_CPP_EXTERN void get(wchar_t&) const;
100  PN_CPP_EXTERN void get(float&) const;
101  PN_CPP_EXTERN void get(double&) const;
102  PN_CPP_EXTERN void get(amqp_timestamp&) const;
103  PN_CPP_EXTERN void get(amqp_decimal32&) const;
104  PN_CPP_EXTERN void get(amqp_decimal64&) const;
105  PN_CPP_EXTERN void get(amqp_decimal128&) const;
106  PN_CPP_EXTERN void get(amqp_uuid&) const;
107  PN_CPP_EXTERN void get(amqp_string&) const;
108  PN_CPP_EXTERN void get(amqp_symbol&) const;
109  PN_CPP_EXTERN void get(amqp_binary&) const;
110  PN_CPP_EXTERN void get(std::string&) const;
111 
114  template<class T> T get() const { T x; get(x); return x; }
115 
123  PN_CPP_EXTERN int64_t as_int() const;
124  PN_CPP_EXTERN uint64_t as_uint() const;
125  PN_CPP_EXTERN double as_double() const;
126  PN_CPP_EXTERN std::string as_string() const;
127 
130 
131  friend PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const scalar&);
132  friend PN_CPP_EXTERN encoder operator<<(encoder, const scalar&);
133  friend PN_CPP_EXTERN decoder operator>>(decoder, scalar&);
134 
137  friend PN_CPP_EXTERN bool operator==(const scalar& x, const scalar& y);
138 
140  friend PN_CPP_EXTERN bool operator<(const scalar& x, const scalar& y);
141 
143 
144  private:
145  void ok(pn_type_t) const;
146  void set(const std::string&, pn_type_t);
147  void set(const pn_atom_t&);
148  pn_atom_t atom_;
149  std::string str_; // Owner of string-like data.
150 
151  friend class message;
152 };
153 
156 
158 class restricted_scalar : public comparable<restricted_scalar> {
159  public:
160  operator const scalar&() const { return scalar_; }
161  type_id type() const { return scalar_.type(); }
162 
170  int64_t as_int() const { return scalar_.as_int(); }
171  uint64_t as_uint() const { return scalar_.as_uint(); }
172  double as_double() const { return scalar_.as_double(); }
173  std::string as_string() const { return scalar_.as_string(); }
175 
176  protected:
177  restricted_scalar() {}
178  scalar scalar_;
179 
180  friend std::ostream& operator<<(std::ostream& o, const restricted_scalar& x) { return o << x.scalar_; }
181  friend bool operator<(const restricted_scalar& x, const restricted_scalar& y) { return x.scalar_ < y.scalar_; }
182  friend bool operator==(const restricted_scalar& x, const restricted_scalar& y) { return x.scalar_ == y.scalar_; }
183 };
184 
186 
187 }
188 
189 #endif // SCALAR_HPP
A holder for an instance of any scalar AMQP type.
Definition: scalar.hpp:35
An AMQP message.
Definition: message.hpp:48
bool empty() const
True if the scalar is empty.
Defines C++ types representing AMQP types.
scalar & operator=(const scalar &)
Copy a scalar.
double as_double() const
Allowed if type_id_is_floating_point(type())
int64_t as_int() const
Allowed if type_id_is_integral(type())
uint64_t as_uint() const
Allowed if type_id_is_integral(type())
std::string as_string() const
Allowed if type_id_is_string_like(type())
scalar(T x)
Create a scalar from any type that we can assign from.
Definition: scalar.hpp:82
type_id type() const
Type for the value in the scalar, NULL_TYPE if empty()
scalar()
Create an empty scalar.