1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle 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-dazzle 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-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.Trie;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import glib.ConstructionException;
24 private import glib.Str;
25 private import gobject.ObjectG;
26 private import gtkd.Loader;
27 
28 
29 /** */
30 public class Trie
31 {
32 	/** the main Gtk struct */
33 	protected DzlTrie* dzlTrie;
34 	protected bool ownedRef;
35 
36 	/** Get the main Gtk struct */
37 	public DzlTrie* getTrieStruct(bool transferOwnership = false)
38 	{
39 		if (transferOwnership)
40 			ownedRef = false;
41 		return dzlTrie;
42 	}
43 
44 	/** the main Gtk struct as a void* */
45 	protected void* getStruct()
46 	{
47 		return cast(void*)dzlTrie;
48 	}
49 
50 	/**
51 	 * Sets our main struct and passes it to the parent class.
52 	 */
53 	public this (DzlTrie* dzlTrie, bool ownedRef = false)
54 	{
55 		this.dzlTrie = dzlTrie;
56 		this.ownedRef = ownedRef;
57 	}
58 
59 	~this ()
60 	{
61 		if ( Linker.isLoaded(LIBRARY_DAZZLE) && ownedRef )
62 			dzl_trie_unref(dzlTrie);
63 	}
64 
65 
66 	/** */
67 	public static GType getType()
68 	{
69 		return dzl_trie_get_type();
70 	}
71 
72 	/**
73 	 * Creates a new #DzlTrie. When a value is removed from the trie, @value_destroy
74 	 * will be called to allow you to release any resources.
75 	 *
76 	 * Params:
77 	 *     valueDestroy = A #GDestroyNotify, or %NULL.
78 	 *
79 	 * Returns: A newly allocated #DzlTrie that should be freed
80 	 *     with dzl_trie_unref().
81 	 *
82 	 * Throws: ConstructionException GTK+ fails to create the object.
83 	 */
84 	public this(GDestroyNotify valueDestroy)
85 	{
86 		auto p = dzl_trie_new(valueDestroy);
87 
88 		if(p is null)
89 		{
90 			throw new ConstructionException("null returned by new");
91 		}
92 
93 		this(cast(DzlTrie*) p);
94 	}
95 
96 	/**
97 	 * This is an alias for dzl_trie_unref().
98 	 */
99 	public void destroy()
100 	{
101 		dzl_trie_destroy(dzlTrie);
102 	}
103 
104 	/**
105 	 * Inserts @value into @trie located with @key.
106 	 *
107 	 * Params:
108 	 *     key = The key to insert.
109 	 *     value = The value to insert.
110 	 */
111 	public void insert(string key, void* value)
112 	{
113 		dzl_trie_insert(dzlTrie, Str.toStringz(key), value);
114 	}
115 
116 	/**
117 	 * Looks up @key in @trie and returns the value associated.
118 	 *
119 	 * Params:
120 	 *     key = The key to lookup.
121 	 *
122 	 * Returns: The value inserted or %NULL.
123 	 */
124 	public void* lookup(string key)
125 	{
126 		return dzl_trie_lookup(dzlTrie, Str.toStringz(key));
127 	}
128 
129 	alias doref = ref_;
130 	/** */
131 	public Trie ref_()
132 	{
133 		auto p = dzl_trie_ref(dzlTrie);
134 
135 		if(p is null)
136 		{
137 			return null;
138 		}
139 
140 		return ObjectG.getDObject!(Trie)(cast(DzlTrie*) p, true);
141 	}
142 
143 	/**
144 	 * Removes @key from @trie, possibly destroying the value associated with
145 	 * the key.
146 	 *
147 	 * Params:
148 	 *     key = The key to remove.
149 	 *
150 	 * Returns: %TRUE if @key was found, otherwise %FALSE.
151 	 */
152 	public bool remove(string key)
153 	{
154 		return dzl_trie_remove(dzlTrie, Str.toStringz(key)) != 0;
155 	}
156 
157 	/**
158 	 * Traverses all nodes of @trie according to the parameters. For each node
159 	 * matching the traversal parameters, @func will be executed.
160 	 *
161 	 * Only %G_PRE_ORDER and %G_POST_ORDER are supported for @order.
162 	 *
163 	 * If @max_depth is less than zero, the entire tree will be traversed.
164 	 * If max_depth is 1, then only the root will be traversed.
165 	 *
166 	 * Params:
167 	 *     key = The key to start traversal from.
168 	 *     order = The order to traverse.
169 	 *     flags = The flags for which nodes to callback.
170 	 *     maxDepth = the maximum depth to process.
171 	 *     func = The func to execute for each matching node.
172 	 *     userData = User data for @func.
173 	 */
174 	public void traverse(string key, GTraverseType order, GTraverseFlags flags, int maxDepth, DzlTrieTraverseFunc func, void* userData)
175 	{
176 		dzl_trie_traverse(dzlTrie, Str.toStringz(key), order, flags, maxDepth, func, userData);
177 	}
178 
179 	/**
180 	 * Drops the reference count by one on @trie. When it reaches zero, the
181 	 * structure is freed.
182 	 */
183 	public void unref()
184 	{
185 		dzl_trie_unref(dzlTrie);
186 	}
187 }