1 /*
2  * Copyright 2007-2020 CM4all GmbH
3  * All rights reserved.
4  *
5  * author: Max Kellermann <mk@cm4all.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * - Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ODBUS_TYPES_HXX
34 #define ODBUS_TYPES_HXX
35 
36 #include "util/TemplateString.hxx"
37 
38 #include <dbus/dbus.h>
39 
40 namespace ODBus {
41 
42 template<int type>
43 struct BasicTypeTraits {
44 	static constexpr int TYPE = type;
45 	static constexpr auto as_string = TemplateString::FromChar(TYPE);
46 };
47 
48 template<typename T>
49 struct TypeTraits {
50 };
51 
52 template<>
53 struct TypeTraits<const char *> : BasicTypeTraits<DBUS_TYPE_STRING> {
54 };
55 
56 using StringTypeTraits = TypeTraits<const char *>;
57 
58 struct ObjectPathTypeTraits : BasicTypeTraits<DBUS_TYPE_OBJECT_PATH> {
59 };
60 
61 template<>
62 struct TypeTraits<dbus_uint32_t> : BasicTypeTraits<DBUS_TYPE_UINT32> {
63 };
64 
65 template<>
66 struct TypeTraits<dbus_uint64_t> : BasicTypeTraits<DBUS_TYPE_UINT64> {
67 };
68 
69 using BooleanTypeTraits = BasicTypeTraits<DBUS_TYPE_BOOLEAN>;
70 
71 template<typename T>
72 struct ArrayTypeTraits {
73 	using ContainedTraits = T;
74 
75 	static constexpr int TYPE = DBUS_TYPE_ARRAY;
76 	static constexpr auto as_string =
77 		TemplateString::Concat(TemplateString::FromChar(TYPE),
78 				       ContainedTraits::as_string);
79 };
80 
81 template<typename KeyT, typename ValueT>
82 struct DictEntryTypeTraits {
83 	static constexpr int TYPE = DBUS_TYPE_DICT_ENTRY;
84 
85 	static constexpr auto as_string =
86 		TemplateString::Concat(TemplateString::FromChar(DBUS_DICT_ENTRY_BEGIN_CHAR),
87 				       KeyT::as_string,
88 				       ValueT::as_string,
89 				       TemplateString::FromChar(DBUS_DICT_ENTRY_END_CHAR));
90 };
91 
92 using VariantTypeTraits = BasicTypeTraits<DBUS_TYPE_VARIANT>;
93 
94 /**
95  * Concatenate all TypeAsString members to one string.
96  */
97 template<typename T, typename... ContainedTraits>
98 struct ConcatTypeAsString {
99 	static constexpr auto as_string =
100 		TemplateString::Concat(T::as_string,
101 				       ConcatTypeAsString<ContainedTraits...>::as_string);
102 };
103 
104 template<typename T>
105 struct ConcatTypeAsString<T> {
106 	static constexpr auto as_string = T::as_string;
107 };
108 
109 template<typename... ContainedTraits>
110 struct StructTypeTraits {
111 	static constexpr int TYPE = DBUS_TYPE_STRUCT;
112 
113 	static constexpr auto as_string =
114 		TemplateString::Concat(TemplateString::FromChar(DBUS_STRUCT_BEGIN_CHAR),
115 				       ConcatTypeAsString<ContainedTraits...>::as_string,
116 				       TemplateString::FromChar(DBUS_STRUCT_END_CHAR));
117 };
118 
119 } /* namespace ODBus */
120 
121 #endif
122