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.TaskCache;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import gio.AsyncResultIF;
24 private import gio.Cancellable;
25 private import glib.ConstructionException;
26 private import glib.ErrorG;
27 private import glib.GException;
28 private import glib.PtrArray;
29 private import glib.Str;
30 private import gobject.ObjectG;
31 
32 
33 /** */
34 public class TaskCache : ObjectG
35 {
36 	/** the main Gtk struct */
37 	protected DzlTaskCache* dzlTaskCache;
38 
39 	/** Get the main Gtk struct */
40 	public DzlTaskCache* getTaskCacheStruct(bool transferOwnership = false)
41 	{
42 		if (transferOwnership)
43 			ownedRef = false;
44 		return dzlTaskCache;
45 	}
46 
47 	/** the main Gtk struct as a void* */
48 	protected override void* getStruct()
49 	{
50 		return cast(void*)dzlTaskCache;
51 	}
52 
53 	/**
54 	 * Sets our main struct and passes it to the parent class.
55 	 */
56 	public this (DzlTaskCache* dzlTaskCache, bool ownedRef = false)
57 	{
58 		this.dzlTaskCache = dzlTaskCache;
59 		super(cast(GObject*)dzlTaskCache, ownedRef);
60 	}
61 
62 
63 	/** */
64 	public static GType getType()
65 	{
66 		return dzl_task_cache_get_type();
67 	}
68 
69 	/** */
70 	public this(GHashFunc keyHashFunc, GEqualFunc keyEqualFunc, GBoxedCopyFunc keyCopyFunc, GBoxedFreeFunc keyDestroyFunc, GBoxedCopyFunc valueCopyFunc, GBoxedFreeFunc valueFreeFunc, long timeToLiveMsec, DzlTaskCacheCallback populateCallback, void* populateCallbackData, GDestroyNotify populateCallbackDataDestroy)
71 	{
72 		auto p = dzl_task_cache_new(keyHashFunc, keyEqualFunc, keyCopyFunc, keyDestroyFunc, valueCopyFunc, valueFreeFunc, timeToLiveMsec, populateCallback, populateCallbackData, populateCallbackDataDestroy);
73 
74 		if(p is null)
75 		{
76 			throw new ConstructionException("null returned by new");
77 		}
78 
79 		this(cast(DzlTaskCache*) p, true);
80 	}
81 
82 	/** */
83 	public bool evict(void* key)
84 	{
85 		return dzl_task_cache_evict(dzlTaskCache, key) != 0;
86 	}
87 
88 	/** */
89 	public void evictAll()
90 	{
91 		dzl_task_cache_evict_all(dzlTaskCache);
92 	}
93 
94 	/** */
95 	public void getAsync(void* key, bool forceUpdate, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)
96 	{
97 		dzl_task_cache_get_async(dzlTaskCache, key, forceUpdate, (cancellable is null) ? null : cancellable.getCancellableStruct(), callback, userData);
98 	}
99 
100 	/**
101 	 * Finish a call to dzl_task_cache_get_async().
102 	 *
103 	 * Returns: The result from the cache.
104 	 *
105 	 * Throws: GException on failure.
106 	 */
107 	public void* getFinish(AsyncResultIF result)
108 	{
109 		GError* err = null;
110 
111 		auto p = dzl_task_cache_get_finish(dzlTaskCache, (result is null) ? null : result.getAsyncResultStruct(), &err);
112 
113 		if (err !is null)
114 		{
115 			throw new GException( new ErrorG(err) );
116 		}
117 
118 		return p;
119 	}
120 
121 	/**
122 	 * Gets all the values in the cache.
123 	 *
124 	 * The caller owns the resulting GPtrArray, which itself owns a reference to the children.
125 	 *
126 	 * Returns: The values.
127 	 */
128 	public PtrArray getValues()
129 	{
130 		auto p = dzl_task_cache_get_values(dzlTaskCache);
131 
132 		if(p is null)
133 		{
134 			return null;
135 		}
136 
137 		return new PtrArray(cast(GPtrArray*) p);
138 	}
139 
140 	/**
141 	 * Peeks to see @key is contained in the cache and returns the
142 	 * matching #GObject if it does.
143 	 *
144 	 * The reference count of the resulting #GObject is not incremented.
145 	 * For that reason, it is important to remember that this function
146 	 * may only be called from the main thread.
147 	 *
148 	 * Params:
149 	 *     key = The key for the cache
150 	 *
151 	 * Returns: A #GObject or
152 	 *     %NULL if the key was not found in the cache.
153 	 */
154 	public ObjectG peek(void* key)
155 	{
156 		auto p = dzl_task_cache_peek(dzlTaskCache, key);
157 
158 		if(p is null)
159 		{
160 			return null;
161 		}
162 
163 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p);
164 	}
165 
166 	/** */
167 	public void setName(string name)
168 	{
169 		dzl_task_cache_set_name(dzlTaskCache, Str.toStringz(name));
170 	}
171 }