xref: /openbmc/boost-dbus/include/dbus/element.hpp (revision 82a51ce2)
1 // Copyright (c) Benjamin Kietzman (github.com/bkietz)
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef DBUS_ELEMENT_HPP
7 #define DBUS_ELEMENT_HPP
8 
9 #include <dbus/dbus.h>
10 #include <string>
11 #include <vector>
12 #include <boost/cstdint.hpp>
13 #include <boost/variant.hpp>
14 
15 namespace dbus {
16 
17 /// Message elements
18 /**
19  * D-Bus Messages are composed of simple elements of one of these types
20  */
21 // bool // is this simply valid? It might pack wrong...
22 // http://maemo.org/api_refs/5.0/5.0-final/dbus/api/group__DBusTypes.html
23 typedef boost::uint8_t byte;
24 
25 typedef boost::int16_t int16;
26 typedef boost::uint16_t uint16;
27 typedef boost::int32_t int32;
28 typedef boost::uint32_t uint32;
29 
30 typedef boost::int64_t int64;
31 typedef boost::uint64_t uint64;
32 // double
33 // unix_fd
34 
35 typedef std::string string;
36 
37 typedef boost::variant<std::string, bool, byte, int16, uint16, int32, uint32, int64,
38                        uint64, double>
39     dbus_variant;
40 
41 struct object_path {
42   string value;
43 };
44 struct signature {
45   string value;
46 };
47 
48 /// Traits template for message elements
49 /**
50  * D-Bus Message elements are identified by unique integer type codes.
51  */
52 template <typename InvalidType>
53 struct element {
54   static const int code = DBUS_TYPE_INVALID;
55 };
56 
57 template <>
58 struct element<bool> {
59   static const int code = DBUS_TYPE_BOOLEAN;
60 };
61 
62 template <>
63 struct element<byte> {
64   static const int code = DBUS_TYPE_BYTE;
65 };
66 
67 template <>
68 struct element<int16> {
69   static const int code = DBUS_TYPE_INT16;
70 };
71 
72 template <>
73 struct element<uint16> {
74   static const int code = DBUS_TYPE_UINT16;
75 };
76 
77 template <>
78 struct element<int32> {
79   static const int code = DBUS_TYPE_INT32;
80 };
81 
82 template <>
83 struct element<uint32> {
84   static const int code = DBUS_TYPE_UINT32;
85 };
86 
87 template <>
88 struct element<int64> {
89   static const int code = DBUS_TYPE_INT64;
90 };
91 
92 template <>
93 struct element<uint64> {
94   static const int code = DBUS_TYPE_UINT64;
95 };
96 
97 template <>
98 struct element<double> {
99   static const int code = DBUS_TYPE_DOUBLE;
100 };
101 
102 template <>
103 struct element<string> {
104   static const int code = DBUS_TYPE_STRING;
105 };
106 
107 template <typename Element>
108 struct element<std::vector<Element>> {
109   static const int code = DBUS_TYPE_ARRAY;
110 };
111 
112 template <>
113 struct element<dbus_variant> {
114   static const int code = DBUS_TYPE_VARIANT;
115 };
116 
117 template <>
118 struct element<object_path> {
119   static const int code = DBUS_TYPE_OBJECT_PATH;
120 };
121 
122 template <>
123 struct element<signature> {
124   static const int code = DBUS_TYPE_SIGNATURE;
125 };
126 
127 template <typename InvalidType>
128 struct is_fixed_type {
129   static const int value = false;
130 };
131 
132 template <>
133 struct is_fixed_type<bool> {
134   static const int value = true;
135 };
136 
137 template <>
138 struct is_fixed_type<byte> {
139   static const int value = true;
140 };
141 
142 template <>
143 struct is_fixed_type<int16> {
144   static const int value = true;
145 };
146 
147 template <>
148 struct is_fixed_type<uint16> {
149   static const int value = true;
150 };
151 
152 template <>
153 struct is_fixed_type<int32> {
154   static const int value = true;
155 };
156 
157 template <>
158 struct is_fixed_type<uint32> {
159   static const int value = true;
160 };
161 
162 template <>
163 struct is_fixed_type<int64> {
164   static const int value = true;
165 };
166 
167 template <>
168 struct is_fixed_type<uint64> {
169   static const int value = true;
170 };
171 
172 template <>
173 struct is_fixed_type<double> {
174   static const int value = true;
175 };
176 
177 template <typename InvalidType>
178 struct is_string_type {
179   static const bool value = false;
180 };
181 
182 template <>
183 struct is_string_type<string> {
184   static const bool value = true;
185 };
186 
187 template <>
188 struct is_string_type<object_path> {
189   static const bool value = true;
190 };
191 
192 template <>
193 struct is_string_type<signature> {
194   static const bool value = true;
195 };
196 
197 }  // namespace dbus
198 
199 #endif  // DBUS_ELEMENT_HPP
200