1 // Generated by gmmproc 2.64.2 -- DO NOT MODIFY!
2 #ifndef _GLIBMM_VARIANTDICT_H
3 #define _GLIBMM_VARIANTDICT_H
4 
5 
6 /*
7  * Copyright (C) 2014 The glibmm Development Team
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <glibmm/variant.h>
24 
25 
26 namespace Glib
27 {
28 
29 /** VariantDict is a mutable interface to Variant dictionaries.
30  *
31  * It can be used for doing a sequence of dictionary lookups in an
32  * efficient way on an existing Variant dictionary or it can be used
33  * to construct new dictionaries with a hashtable-like interface.  It
34  * can also be used for taking existing dictionaries and modifying them
35  * in order to create new ones.
36  *
37  * newin{2,40}
38  */
39 class GLIBMM_API VariantDict final
40 {
41   //GVariantDict is registered as a boxed type, but it has ref/unref functions instead of copy/free,
42   //so we use it via RefPtr.
43   public:
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45   using CppObjectType = VariantDict;
46   using BaseObjectType = GVariantDict;
47 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
48 
49 
50   /** Increment the reference count for this object.
51    * You should never need to do this manually - use the object via a RefPtr instead.
52    */
53   void reference()   const;
54 
55   /** Decrement the reference count for this object.
56    * You should never need to do this manually - use the object via a RefPtr instead.
57    */
58   void unreference() const;
59 
60   ///Provides access to the underlying C instance.
61   GVariantDict*       gobj();
62 
63   ///Provides access to the underlying C instance.
64   const GVariantDict* gobj() const;
65 
66   ///Provides access to the underlying C instance. The caller is responsible for unrefing it. Use when directly setting fields in structs.
67   GVariantDict* gobj_copy() const;
68 
69   VariantDict() = delete;
70 
71   // noncopyable
72   VariantDict(const VariantDict&) = delete;
73   VariantDict& operator=(const VariantDict&) = delete;
74 
75 protected:
76   // Do not derive this.  Glib::VariantDict can neither be constructed nor deleted.
77 
78   void operator delete(void*, std::size_t);
79 
80 private:
81 
82 
83 public:
84 
85   /** Allocates and initialises a new VariantDict.
86    *
87    * You should call g_variant_dict_unref() on the return value when it
88    * is no longer needed.  The memory will not be automatically freed by
89    * any other call.
90    *
91    * In some cases it may be easier to place a VariantDict directly on
92    * the stack of the calling function and initialise it with
93    * g_variant_dict_init().  This is particularly useful when you are
94    * using VariantDict to construct a Variant.
95    *
96    * @newin{2,40}
97    *
98    * @param from_asv The Variant with which to initialise the
99    * dictionary.
100    * @return A VariantDict.
101    */
102   static Glib::RefPtr<VariantDict> create(const VariantBase& from_asv);
103 
104   /// A create() convenience overload.
105   static Glib::RefPtr<VariantDict> create();
106 
107 
108 //TODO: Add a method overload that does not take expected_type (which can be null),
109 //just returning a VariantBase that should be cast_dynamic()ed?
110 
111   /** Looks up a value in the VariantDict. See also lookup_value().
112    *
113    * If the @a key is not found the false is returned.
114    *
115    * The @a expected_type string specifies what type of value is expected.
116    * If the value associated with @a key has a different type then false is
117    * returned.
118    *
119    * If the key is found and the value has the correct type, it is
120    * returned in the @a value output variable.
121    */
122   bool lookup_value_variant(const Glib::ustring& key, const VariantType& expected_type, VariantBase& value) const;
123 
124 
125   /** Looks up a value in the VariantDict.
126    *
127    * If the @a key is not found the false is returned.
128    *
129    * If the value associated with @a key has a different type than expected then false is
130    * returned.
131    *
132    * If the key is found and the value has the correct type, it is
133    * returned in the @a value output variable.
134    */
135   template <typename T_Value>
136   bool lookup_value(const Glib::ustring& key, T_Value& value) const;
137 
138 
139   /** Checks if @a key exists in @a dict.
140    *
141    * @newin{2,40}
142    *
143    * @param key The key to look up in the dictionary.
144    * @return <tt>true</tt> if @a key is in @a dict.
145    */
146   bool contains(const Glib::ustring& key) const;
147 
148 
149   /** Inserts (or replaces) a key in a VariantDict.
150    *
151    *  @a value is consumed if it is floating.
152    *
153    * @newin{2,40}
154    *
155    * @param key The key to insert a value for.
156    * @param value The value to insert.
157    */
158   void insert_value_variant(const Glib::ustring& key, const VariantBase& value);
159 
160   /** Inserts (or replaces) a key in a VariantDict.
161    *
162    * @param key The key to insert a value for.
163    * @param value The value to insert.
164    */
165   template <typename T_Value>
166   void insert_value(const Glib::ustring& key, const T_Value& value);
167 
168 
169   /** Removes a key and its associated value from a VariantDict.
170    *
171    * @newin{2,40}
172    *
173    * @param key The key to remove.
174    * @return <tt>true</tt> if the key was found and removed.
175    */
176   bool remove(const Glib::ustring& key);
177 
178 
179   /** Releases all memory associated with a VariantDict without freeing
180    * the VariantDict structure itself.
181    *
182    * It typically only makes sense to do this on a stack-allocated
183    * VariantDict if you want to abort building the value part-way
184    * through.  This function need not be called if you call
185    * g_variant_dict_end() and it also doesn't need to be called on dicts
186    * allocated with g_variant_dict_new (see g_variant_dict_unref() for
187    * that).
188    *
189    * It is valid to call this function on either an initialised
190    * VariantDict or one that was previously cleared by an earlier call
191    * to g_variant_dict_clear() but it is not valid to call this function
192    * on uninitialised memory.
193    *
194    * @newin{2,40}
195    */
196   void clear();
197 
198 
199 };
200 
201 template <typename T_Value>
insert_value(const Glib::ustring & key,const T_Value & value)202 void VariantDict::insert_value(const Glib::ustring& key, const T_Value& value)
203 {
204   using type_glib_variant = Glib::Variant<T_Value>;
205 
206   //TODO: Can we do any check like this here, before glib does?
207   //g_return_val_if_fail(
208   //  g_variant_type_equal(g_action_get_parameter_type(const_cast<GAction*>(gobj())), type_glib_variant::variant_type().gobj()),
209   //  Glib::ustring());
210   return insert_value_variant(key, type_glib_variant::create(value));
211 }
212 
213 template <typename T_Value>
lookup_value(const Glib::ustring & key,T_Value & value)214 bool VariantDict::lookup_value(const Glib::ustring& key, T_Value& value) const
215 {
216   value = T_Value(); //Make sure that it is initialized.
217 
218   using type_glib_variant = Glib::Variant<T_Value>;
219 
220   //TODO: Can we do any check like this here, before glib does?
221   //g_variant_type_equal(g_action_group_get_action_state_type(const_cast<GActionGroup*>(gobj()), action_name.c_str()), type_glib_variant::variant_type().gobj()));
222 
223   Glib::VariantBase variantBase;
224   const bool result = lookup_value_variant(key, type_glib_variant::variant_type(), variantBase);
225   if(!result)
226     return result;
227 
228   try
229   {
230     const type_glib_variant variantDerived = variantBase.cast_dynamic<type_glib_variant>(variantBase);
231     value = variantDerived.get();
232   }
233   catch(const std::bad_cast& ex)
234   {
235     return false;
236   }
237 
238   return result;
239 }
240 
241 } //namespace Glib
242 
243 
244 namespace Glib
245 {
246 
247 /** A Glib::wrap() method for this object.
248  *
249  * @param object The C instance.
250  * @param take_copy False if the result should take ownership of the C instance. True if it should take a new copy or ref.
251  * @result A C++ instance that wraps this C instance.
252  *
253  * @relates Glib::VariantDict
254  */
255 GLIBMM_API
256 Glib::RefPtr<Glib::VariantDict> wrap(GVariantDict* object, bool take_copy = false);
257 
258 } // namespace Glib
259 
260 
261 #endif /* _GLIBMM_VARIANTDICT_H */
262 
263