1 /*
2  * This file is part of d-handy.
3  *
4  * d-handy is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-handy is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-handy; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module handy.ValueObject;
20 
21 private import glib.ConstructionException;
22 private import glib.Str;
23 private import gobject.ObjectG;
24 private import gobject.Value;
25 private import handy.c.functions;
26 public  import handy.c.types;
27 
28 
29 /** */
30 public class ValueObject : ObjectG
31 {
32 	/** the main Gtk struct */
33 	protected HdyValueObject* hdyValueObject;
34 
35 	/** Get the main Gtk struct */
36 	public HdyValueObject* getValueObjectStruct(bool transferOwnership = false)
37 	{
38 		if (transferOwnership)
39 			ownedRef = false;
40 		return hdyValueObject;
41 	}
42 
43 	/** the main Gtk struct as a void* */
44 	protected override void* getStruct()
45 	{
46 		return cast(void*)hdyValueObject;
47 	}
48 
49 	/**
50 	 * Sets our main struct and passes it to the parent class.
51 	 */
52 	public this (HdyValueObject* hdyValueObject, bool ownedRef = false)
53 	{
54 		this.hdyValueObject = hdyValueObject;
55 		super(cast(GObject*)hdyValueObject, ownedRef);
56 	}
57 
58 
59 	/** */
60 	public static GType getType()
61 	{
62 		return hdy_value_object_get_type();
63 	}
64 
65 	/**
66 	 * Create a new #HdyValueObject.
67 	 *
68 	 * Params:
69 	 *     value = the #GValue to store
70 	 *
71 	 * Returns: a new #HdyValueObject
72 	 *
73 	 * Since: 0.0.8
74 	 *
75 	 * Throws: ConstructionException GTK+ fails to create the object.
76 	 */
77 	public this(Value value)
78 	{
79 		auto __p = hdy_value_object_new((value is null) ? null : value.getValueStruct());
80 
81 		if(__p is null)
82 		{
83 			throw new ConstructionException("null returned by new");
84 		}
85 
86 		this(cast(HdyValueObject*) __p, true);
87 	}
88 
89 	/**
90 	 * Creates a new #HdyValueObject. This is a convenience method to create a
91 	 * #HdyValueObject that stores a string.
92 	 *
93 	 * Params:
94 	 *     string_ = the string to store
95 	 *
96 	 * Returns: a new #HdyValueObject
97 	 *
98 	 * Since: 0.0.8
99 	 *
100 	 * Throws: ConstructionException GTK+ fails to create the object.
101 	 */
102 	public this(string string_)
103 	{
104 		auto __p = hdy_value_object_new_string(Str.toStringz(string_));
105 
106 		if(__p is null)
107 		{
108 			throw new ConstructionException("null returned by new_string");
109 		}
110 
111 		this(cast(HdyValueObject*) __p, true);
112 	}
113 
114 	/**
115 	 * Copy data from the contained #GValue into @dest.
116 	 *
117 	 * Params:
118 	 *     dest = #GValue with correct type to copy into
119 	 *
120 	 * Since: 0.0.8
121 	 */
122 	public void copyValue(Value dest)
123 	{
124 		hdy_value_object_copy_value(hdyValueObject, (dest is null) ? null : dest.getValueStruct());
125 	}
126 
127 	/**
128 	 * Returns a copy of the contained string if the value is of type
129 	 * #G_TYPE_STRING.
130 	 *
131 	 * Returns: a copy of the contained string
132 	 *
133 	 * Since: 0.0.8
134 	 */
135 	public string dupString()
136 	{
137 		auto retStr = hdy_value_object_dup_string(hdyValueObject);
138 
139 		scope(exit) Str.freeString(retStr);
140 		return Str.toString(retStr);
141 	}
142 
143 	/**
144 	 * Returns the contained string if the value is of type #G_TYPE_STRING.
145 	 *
146 	 * Returns: the contained string
147 	 *
148 	 * Since: 0.0.8
149 	 */
150 	public string getString()
151 	{
152 		return Str.toString(hdy_value_object_get_string(hdyValueObject));
153 	}
154 
155 	/**
156 	 * Return the contained value.
157 	 *
158 	 * Returns: the contained #GValue
159 	 *
160 	 * Since: 0.0.8
161 	 */
162 	public Value getValue()
163 	{
164 		auto __p = hdy_value_object_get_value(hdyValueObject);
165 
166 		if(__p is null)
167 		{
168 			return null;
169 		}
170 
171 		return ObjectG.getDObject!(Value)(cast(GValue*) __p);
172 	}
173 }