1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
26 //----------------------------------------------------------------------------------------------------------------------
27 // File: vogl_handle_tracker.h
28 //----------------------------------------------------------------------------------------------------------------------
29 #ifndef VOGL_HANDLE_TRACKER_H
30 #define VOGL_HANDLE_TRACKER_H
32 #include "vogl_common.h"
33 #include "vogl_hash_map.h"
34 #include "vogl_sparse_vector.h"
35 #include "vogl_json.h"
37 class vogl_handle_tracker
40 typedef uint32 handle_t;
44 friend class vogl_handle_tracker;
51 handle_def(handle_t handle, handle_t inv_handle, GLenum target)
52 : m_handle(handle), m_inv_handle(inv_handle), m_target(target), m_is_valid(true)
57 void init(handle_t handle, handle_t inv_handle, GLenum target)
60 m_inv_handle = inv_handle;
68 utils::zero_object(*this);
76 handle_t get_handle() const
80 handle_t get_inv_handle() const
84 GLenum get_target() const
89 void set_target(GLenum target)
94 bool operator==(const handle_def &rhs) const
96 return (m_handle == rhs.m_handle) && (m_inv_handle == rhs.m_inv_handle) && (m_target == rhs.m_target) && (m_is_valid == rhs.m_is_valid);
101 handle_t m_inv_handle;
105 void set_handle(handle_t handle)
109 void set_inv_handle(handle_t inv_handle)
111 m_inv_handle = inv_handle;
115 typedef vogl::sparse_vector<handle_def, 5> handle_def_vec;
116 typedef vogl::hash_map<handle_t, uint> handle_hash_map_t;
118 vogl_handle_tracker();
119 vogl_handle_tracker(vogl_namespace_t handle_namespace);
120 ~vogl_handle_tracker();
124 vogl_namespace_t get_namespace() const
128 void set_namespace(vogl_namespace_t handle_namespace)
130 m_namespace = handle_namespace;
133 uint get_total_valid_handles() const
135 return m_inv_handles.size();
138 // size() and operator[] allow you to iterate over the entire handle namespace, but beware that not all handles may be valid!
139 // size() is not necessarily always equal to get_total_valid_handles()!
142 return m_handles.size();
144 const handle_def &operator[](uint index) const
146 return m_handles[index];
149 void get_handles(uint_vec &handles);
150 void get_inv_handles(uint_vec &inv_handles);
152 const handle_def_vec &get_handles() const
156 const handle_hash_map_t get_inv_handles() const
158 return m_inv_handles;
161 // true if insertion occured, otherwise it already existed and wasn't updated.
162 bool insert(handle_t handle, handle_t inv_handle, GLenum target);
163 //bool insert(handle_t handle, GLenum target) { return insert(handle, handle, target); }
165 // If handle is missing, the handle/inv_handle/target is added.
166 // Otherwise, it's only updated if the target is compare_target (and the inv_handle is ignored).
167 bool conditional_update(handle_t handle, handle_t inv_handle, GLenum compare_target, GLenum target);
168 bool conditional_update(handle_t handle, GLenum compare_target, GLenum target)
170 return conditional_update(handle, handle, compare_target, target);
173 // If handle is missing, the handle/inv_handle/target is added.
174 // Otherwise, the target of the handle is changed, and the inv_handle is ignored.
175 // Returns false if the insertion failed.
176 bool update(handle_t handle, handle_t inv_handle, GLenum target);
177 bool update(handle_t handle, GLenum target)
179 return update(handle, handle, target);
182 bool update_inv(handle_t inv_handle, handle_t handle, GLenum target);
184 // Sets handle's target, false if the handle does not exist.
185 bool set_target(handle_t handle, GLenum target);
186 bool set_target_inv(handle_t inv_handle, GLenum target);
188 bool contains(handle_t handle) const
190 return is_valid_handle(handle);
192 bool contains_inv(handle_t inv_handle) const
194 return is_valid_inv_handle(inv_handle);
197 // Given an inverse handle, return the handle associated with it, or false.
198 // Does not set handle on error.
199 bool map_inv_handle_to_handle(handle_t inv_handle, handle_t &handle) const;
201 // Given a handle, return the inverse handle associated with it, or false.
202 // Does not set inv_handle on error.
203 bool map_handle_to_inv_handle(handle_t handle, handle_t &inv_handle) const;
205 GLenum get_target(handle_t handle) const;
206 GLenum get_target_inv(handle_t inv_handle) const;
208 // true if erasure successfully occurred
209 bool erase(handle_t handle);
210 bool erase_inv(handle_t inv_handle);
212 bool invert(vogl_handle_tracker &inverted_map) const;
217 bool operator==(const vogl_handle_tracker &other) const;
218 bool operator!=(const vogl_handle_tracker &other) const
220 return !(*this == other);
223 bool serialize(json_node &node) const;
224 bool deserialize(const json_node &node);
227 vogl_namespace_t m_namespace;
228 handle_def_vec m_handles;
229 handle_hash_map_t m_inv_handles;
231 bool is_valid_handle(handle_t handle) const
233 return (handle < m_handles.size()) && (m_handles[handle].is_valid());
235 bool is_valid_inv_handle(handle_t inv_handle) const
237 return m_inv_handles.contains(inv_handle);
241 #endif // VOGL_HANDLE_TRACKER_H