Debian Codegraph @

cpp/include/proton/scalar.hpp

raw
1 #ifndef PROTON_SCALAR_HPP
2 #define PROTON_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 "./scalar_base.hpp"
24
25 #include "proton/null.hpp"
26
27 #include <proton/type_compat.h>
28
29 /// @file
30 /// @copybrief proton::scalar
31
32 namespace proton {
33
34 /// A holder for an instance of any scalar AMQP type.
35 ///
36 /// @see @ref types_page
37 class scalar : public scalar_base {
38 public:
39 /// Create an empty scalar.
40 PN_CPP_EXTERN scalar() = default;
41
42 /// Construct from any scalar type.
43 template <class T> scalar(const T& x) { *this = x; }
44
45 /// Assign from any scalar type.
46 template <class T> scalar& operator=(const T& x) { put(x); return *this; }
47
48 /// Clear the scalar, making it empty().
49 void clear() { *this = null(); }
50 };
51
52 /// Get a contained value of type T. For example:
53 ///
54 /// uint64_t i = get<uint64_t>(x)
55 ///
56 /// This will succeed if and only if x contains a uint64_t value.
57 ///
58 /// @throw conversion_error if contained value is not of type T.
59 /// @relatedalso scalar
60 template<class T> T get(const scalar& s) { return internal::get<T>(s); }
61
62 /// Coerce the contained value to type T. For example:
63 ///
64 /// uint64_t i = coerce<uint64_t>(x)
65 ///
66 /// This will succeed if x contains any numeric value, but may lose
67 /// precision if it contains a float or double value.
68 ///
69 /// @throw conversion_error if the value cannot be converted to T
70 /// according to `std::is_convertible`
71 /// @relatedalso scalar
72 template<class T> T coerce(const scalar& x) { return internal::coerce<T>(x); }
73
74
75 /// Coerce the contained value to type T. For example:
76 ///
77 /// uint64_t i = coerce<uint64_t>(x)
78 ///
79 /// This will succeed if x contains any numeric value, but may lose
80 /// precision if it contains a float or double value.
81 ///
82 /// @throw conversion_error if the value cannot be converted to T
83 /// according to `std::is_convertible`
84 /// @relatedalso scalar
85 template<class T> T coerce(scalar& x) { return internal::coerce<T>(x); }
86
87 } // proton
88
89 #endif // PROTON_SCALAR_HPP