2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Mateusz Malicki (m.malicki2@samsung.com)
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Mateusz Malicki (m.malicki2@samsung.com)
22 * @brief GVariant visitor
25 #ifndef CONFIG_TO_GVARIANT_VISITOR_HPP
26 #define CONFIG_TO_GVARIANT_VISITOR_HPP
28 #include "config/is-visitable.hpp"
29 #include "config/is-union.hpp"
37 class ToGVariantVisitor {
41 : mBuilder(g_variant_builder_new(G_VARIANT_TYPE_TUPLE))
45 ToGVariantVisitor(const ToGVariantVisitor& visitor)
46 : mBuilder(visitor.mBuilder ? g_variant_builder_ref(visitor.mBuilder) : nullptr)
53 g_variant_builder_unref(mBuilder);
57 ToGVariantVisitor& operator=(const ToGVariantVisitor&) = delete;
62 GVariant* ret = g_variant_builder_end(mBuilder);
63 g_variant_builder_unref(mBuilder);
71 void visit(const std::string& /* name */, const T& value)
76 GVariantBuilder* mBuilder;
78 void writeInternal(std::int32_t value) { add("i", value); };
79 void writeInternal(std::int64_t value) { add("x", value); };
80 void writeInternal(bool value) { add("b", value); };
81 void writeInternal(double value) { add("d", value); };
82 void writeInternal(const std::string& value) { add("s", value.c_str()); };
85 void writeInternal(const std::vector<T>& value)
88 g_variant_builder_open(mBuilder, G_VARIANT_TYPE_ARRAY);
89 for (const T& v : value) {
92 g_variant_builder_close(mBuilder);
94 g_variant_builder_add(mBuilder, "as", NULL);
99 typename std::enable_if<isVisitable<T>::value && !isUnion<T>::value>::type
100 writeInternal(const T& value)
102 ToGVariantVisitor visitor;
103 value.accept(visitor);
104 g_variant_builder_add_value(mBuilder, visitor.toVariant());
108 typename std::enable_if<isVisitable<T>::value && isUnion<T>::value>::type
109 writeInternal(const T& value)
111 ToGVariantVisitor visitor;
112 value.accept(visitor);
113 add("v", visitor.toVariant());
116 template<typename Value>
117 void add(const char* type, Value value) {
118 g_variant_builder_add(mBuilder, type, value);
122 } // namespace config
124 #endif // CONFIG_TO_GVARIANT_VISITOR_HPP