types.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-or-later OR BSD-3-Clause
2 /***************************************************************************
3  * Copyright (C) 2007,2008,2009,2010 by Rick L. Vinyard, Jr. *
4  * rvinyard@cs.nmsu.edu *
5  * *
6  * This file is part of the dbus-cxx library. *
7  ***************************************************************************/
8 #include <dbus-cxx/enums.h>
9 #include <dbus-cxx/path.h>
10 #include <dbus-cxx/signature.h>
12 #include <stdint.h>
13 #include <string>
14 #include <tuple>
15 #include <vector>
16 
17 #ifndef DBUSCXX_TYPES_H
18 #define DBUSCXX_TYPES_H
19 
20 namespace DBus {
21 
22 class Variant;
23 
24 inline int typeToDBusType( DataType t ) {
25  return static_cast<int>( t );
26 }
27 
29  return static_cast<int>( t );
30 }
31 
32 inline DataType char_to_dbus_type( char c ) {
33  switch( c ) {
34  case 'y':
35  return DataType::BYTE;
36 
37  case 'b':
38  return DataType::BOOLEAN;
39 
40  case 'n':
41  return DataType::INT16;
42 
43  case 'q':
44  return DataType::UINT16;
45 
46  case 'i':
47  return DataType::INT32;
48 
49  case 'u':
50  return DataType::UINT32;
51 
52  case 'x':
53  return DataType::INT64;
54 
55  case 't':
56  return DataType::UINT64;
57 
58  case 'd':
59  return DataType::DOUBLE;
60 
61  case 's':
62  return DataType::STRING;
63 
64  case 'o':
65  return DataType::OBJECT_PATH;
66 
67  case 'g':
68  return DataType::SIGNATURE;
69 
70  case 'a':
71  return DataType::ARRAY;
72 
73  case 'r':
74  case '(':
75  case ')':
76  return DataType::STRUCT;
77 
78  case 'v':
79  return DataType::VARIANT;
80 
81  case 'e':
82  case '{':
83  case '}':
84  return DataType::DICT_ENTRY;
85 
86  case 'h':
87  return DataType::UNIX_FD;
88 
89  }
90 
91  return DataType::INVALID;
92 }
93 
95  switch( c ) {
96  case 'a':
97  return ContainerType::ARRAY;
98 
99  case 'e':
100  case '{':
102 
103  case '(':
104  case 'r':
105  return ContainerType::STRUCT;
106 
107  case 'v':
108  return ContainerType::VARIANT;
109  }
110 
111  return ContainerType::None;
112 }
113 
114 inline bool is_ending_container( char c ) {
115  switch( c ) {
116  case '}':
117  case ')':
118  return true;
119 
120  default:
121  return false;
122  }
123 }
124 
126  switch( c ) {
127  case '}':
129 
130  case ')':
131  return ContainerType::STRUCT;
132  }
133 
134  return ContainerType::None;
135 }
136 
137 inline DataType type( const uint8_t& ) { return DataType::BYTE; }
138 inline DataType type( const bool& ) { return DataType::BOOLEAN; }
139 inline DataType type( const int16_t& ) { return DataType::INT16; }
140 inline DataType type( const uint16_t& ) { return DataType::UINT16; }
141 inline DataType type( const int32_t& ) { return DataType::INT32; }
142 inline DataType type( const uint32_t& ) { return DataType::UINT32; }
143 inline DataType type( const int64_t& ) { return DataType::INT64; }
144 inline DataType type( const uint64_t& ) { return DataType::UINT64; }
145 inline DataType type( const double& ) { return DataType::DOUBLE; }
146 inline DataType type( const std::string& ) { return DataType::STRING; }
147 inline DataType type( const char* ) { return DataType::STRING; }
148 inline DataType type( const Path& ) { return DataType::OBJECT_PATH; }
149 inline DataType type( const Signature& ) { return DataType::SIGNATURE; }
150 template <typename... args>
151 inline DataType type( const DBus::Variant& ) { return DataType::VARIANT; }
152 inline DataType type( const FileDescriptor& ) { return DataType::UNIX_FD; }
153 
154 inline DataType type( const char& ) { return DataType::BYTE; }
155 inline DataType type( const int8_t& ) { return DataType::BYTE; }
156 
157 inline DataType type( const float& ) { return DataType::DOUBLE; }
158 
159 template <typename T>
160 inline DataType type( const std::vector<T>& ) { return DataType::ARRAY; }
161 
162 template <typename ...T>
163 inline DataType type( const std::tuple<T...>& ) { return DataType::STRUCT; }
164 
165 inline
167  return ( DataType )( n );
168 }
169 
174 class TypeInfo {
175 public:
179  TypeInfo( DataType d );
180 
184  bool isTemplated() const;
185 
189  std::vector<std::string> includeFilesForType() const;
190 
194  std::string cppType() const;
195 
197  bool is_basic() const;
198 
200  bool is_fixed() const;
201 
203  bool is_container() const;
204 
209  int32_t alignment() const;
210 
215  char to_dbus_char() const;
216 
217 private:
219 };
220 
221 std::ostream& operator<<( std::ostream& os, DataType d );
222 
223 }
224 
225 #endif
A FileDescriptor holds a UNIX file descriptor that can be passed between processes.
Definition: filedescriptor.h:21
Represents a DBus Path.
Definition: path.h:21
Represents a DBus signature.
Definition: signature.h:74
Contains useful data about the type in order for code generation to happen in an easy manner.
Definition: types.h:174
DataType m_type
Definition: types.h:218
int32_t alignment() const
Return the alignment of the type.
Definition: types.cpp:181
TypeInfo(DataType d)
The type to get info for.
Definition: types.cpp:15
bool is_container() const
True if the element type is a container.
Definition: types.cpp:168
std::string cppType() const
Returns the C++ type of this DataType.
Definition: types.cpp:72
bool isTemplated() const
Returns true if this type is templated.
Definition: types.cpp:17
std::vector< std::string > includeFilesForType() const
Returns the needed includes in order to use this type.
Definition: types.cpp:31
char to_dbus_char() const
Turn this type into a DBus char.
Definition: types.cpp:215
bool is_basic() const
True if the element type is a basic type.
Definition: types.cpp:132
bool is_fixed() const
True if the element type is a fixed type.
Definition: types.cpp:152
A Variant is a type-safe union for DBus operations.
Definition: variant.h:42
Global DBus namespace, where everything happens.
Definition: callmessage.cpp:18
int typeToDBusContainerType(ContainerType t)
Definition: types.h:28
ContainerType
Definition: enums.h:73
DataType checked_type_cast(int n)
Definition: types.h:166
std::shared_ptr< DBus::Connection > operator<<(std::shared_ptr< DBus::Connection > ptr, std::shared_ptr< DBus::Message > msg)
Definition: connection.h:431
bool is_ending_container(char c)
Definition: types.h:114
DataType type(const uint8_t &)
Definition: types.h:137
ContainerType char_to_ending_container(char c)
Definition: types.h:125
DataType char_to_dbus_type(char c)
Definition: types.h:32
DataType
Definition: enums.h:52
int typeToDBusType(DataType t)
Definition: types.h:24
ContainerType char_to_container_type(char c)
Definition: types.h:94