RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
genericshelper.hpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #ifndef RIOENGINE_MISC_GENERICSHELPER_H_
31 #define RIOENGINE_MISC_GENERICSHELPER_H_
32 
33 //-----------------------------------------------------------------------------
34 
35 #include "typedefs.h"
36 #include "cengine/cobject.h"
37 
38 //-----------------------------------------------------------------------------
39 
41 public:
42 
47  template<class T>
48  static std::vector<T*>* getCopyFromVector(std::vector<T*>& original_vec) {
49  std::vector<T*>* copy_vec = new std::vector<T*>();
50 
51  for (std::vector<T*>::iterator it = original_vec.begin();
52  it != original_vec.end();
53  ++it) {
54  T* copy_obj = new T(*(*it));
55  copy_vec->push_back(copy_obj);
56  }
57  return copy_vec;
58  }
59 
60  //-----------------------------------------------------------------------------
61 
66  template <class T>
67  static void deleteVector(std::vector<T*>* vec) {
68  if (!vec) return;
69 
70  for (std::vector<T*>::iterator it = vec->begin();
71  it != vec->end();
72  ++it) {
73  SAFE_RELEASE(*it);
74  }
75 
76  SAFE_RELEASE(vec);
77  vec = NULL;
78  }
79 
80  //-----------------------------------------------------------------------------
81 
82  template <class T>
83  static int getIndexOfObjectInVector(std::vector<T*>* vec, T* obj_to_find) {
84  if (!vec || !obj_to_find) {
85  return -1;
86  }
87 
88  for (std::vector<T*>::iterator it = vec->begin();
89  it != vec->end();
90  ++it) {
91  if (*it == obj_to_find) {
92  return std::distance(vec->begin(), it);
93  }
94  }
95  return -1;
96  }
97 
98  //-----------------------------------------------------------------------------
99 
100  template <class T>
101  static bool vectorContainsObject(std::vector<T>* vec, T object_to_find) {
102  return getIndexOfObjectInVector(vec, object_to_find) > -1;
103  }
104 
105  //-----------------------------------------------------------------------------
106 
111  template <class T>
112  static bool deleteObjectFromVector(std::vector<T*>* vec, void* object_to_delete) {
113  int index = -1;
114 
115  for (std::vector<T*>::iterator it = vec->begin();
116  it != vec->end();
117  ++it) {
118  if (*it == object_to_delete) {
119  index = std::distance(vec->begin(), it);
120  break;
121  }
122  }
123 
124  if (index > -1) {
125  void* ref = vec->at(index);
126  vec->erase(vec->begin() + index);
127  SAFE_RELEASE(ref);
128  return true;
129  }
130  return false;
131  }
132 
133  //-----------------------------------------------------------------------------
134 
138  template <class TKey, class TVal>
139  static void deleteMap(std::map<TKey, TVal*>* map) {
140  if (!map) {
141  return;
142  }
143 
144  std::map<TKey, TVal*>::iterator it = map->begin();
145 
146  for (; it != map->end(); it++) {
147  SAFE_RELEASE(it->second);
148  }
149 
150  SAFE_RELEASE(map);
151  }
152 
153  //-----------------------------------------------------------------------------
154 
155  template <class T>
156  static bool replaceObjectInVector(std::vector<T*>* vec, T* object_to_replace, T* replacement) {
157  int index = getIndexOfObjectInVector(vec, object_to_replace);
158 
159  if (index < 0) {
160  return false;
161  }
162 
163  SAFE_RELEASE(object_to_replace);
164  SAFE_RETAIN(replacement);
165  vec->at(index) = replacement;
166 
167  return true;
168  }
169 };
170 
171 #endif // RIOENGINE_MISC_GENERICSHELPER_H_
static void deleteMap(std::map< TKey, TVal * > *map)
static void deleteVector(std::vector< T * > *vec)
static bool replaceObjectInVector(std::vector< T * > *vec, T *object_to_replace, T *replacement)
#define SAFE_RELEASE(x)
Definition: cobject.h:36
static int getIndexOfObjectInVector(std::vector< T * > *vec, T *obj_to_find)
#define SAFE_RETAIN(x)
Definition: cobject.h:35
static bool deleteObjectFromVector(std::vector< T * > *vec, void *object_to_delete)
static bool vectorContainsObject(std::vector< T > *vec, T object_to_find)
static std::vector< T * > * getCopyFromVector(std::vector< T * > &original_vec)