Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ResourceDatabase.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef ResourceDatabase_INCLUDE_ONCE
33 #define ResourceDatabase_INCLUDE_ONCE
34 
35 #include <vlCore/Object.hpp>
36 #include <vlCore/String.hpp>
37 #include <vector>
38 #include <algorithm>
39 
40 namespace vl
41 {
42  class VirtualFile;
43 
49  {
51 
52  public:
54  {
55  VL_DEBUG_SET_OBJECT_NAME()
56  }
57 
58  const std::vector< ref<Object> >& resources() const { return mResources; }
59 
60  std::vector< ref<Object> >& resources() { return mResources; }
61 
63  template<class T>
64  T* next(int& cur_pos) const
65  {
66  for(unsigned i=cur_pos; i<mResources.size(); ++i)
67  {
68  ref<T> r = cast<T>(mResources[i].get());
69  if (r)
70  {
71  cur_pos = i+1;
72  return r.get();
73  }
74  }
75  return NULL;
76  }
77 
79  template<class T>
80  void get( std::vector< ref<T> >& resources, bool clear_vector=true )
81  {
82  if (clear_vector)
83  resources.clear();
84 
85  for( unsigned i=0; i<mResources.size(); ++i )
86  {
87  ref<T> r = cast<T>(mResources[i].get());
88  if (r)
89  resources.push_back(r);
90  }
91  }
92 
94  template<class T>
95  void extract( std::vector< ref<T> >& resources, bool clear_vector=true )
96  {
97  if (clear_vector)
98  resources.clear();
99 
100  size_t start = resources.size();
101 
102  for( unsigned i=mResources.size(); i--; )
103  {
104  ref<T> r = cast<T>(mResources[i].get());
105  if (r)
106  {
107  resources.push_back(r);
108  mResources.erase(mResources.begin()+i);
109  }
110  }
111 
112  std::reverse(resources.begin()+start, resources.end());
113  }
114 
116  template<class T>
117  size_t count() const
118  {
119  size_t count=0;
120  for(unsigned i=0; i<mResources.size(); ++i)
121  {
122  const T* r = cast_const<T>(mResources[i].get());
123  if (r)
124  ++count;
125  }
126  return count;
127  }
128 
130  template<class T>
131  const T* get(int j) const
132  {
133  int count=0;
134  for(unsigned i=0; i<mResources.size(); ++i)
135  {
136  const T* r = cast_const<T>(mResources[i].get());
137  if (r)
138  {
139  if (count == j)
140  return r;
141  else
142  ++count;
143  }
144  }
145  return NULL;
146  }
147 
149  template<class T>
150  T* get(int j)
151  {
152  int count=0;
153  for(unsigned i=0; i<mResources.size(); ++i)
154  {
155  T* r = cast<T>(mResources[i].get());
156  if (r)
157  {
158  if (count == j)
159  return r;
160  else
161  ++count;
162  }
163  }
164  return NULL;
165  }
166 
167  protected:
168  std::vector< ref<Object> > mResources;
169  };
170 
172  VLCORE_EXPORT bool canLoad(const String& path);
173 
175  VLCORE_EXPORT bool canWrite(const String& path);
176 
178  VLCORE_EXPORT bool canLoad(VirtualFile* file);
179 
181  VLCORE_EXPORT bool canWrite(VirtualFile* file);
182 
184  VLCORE_EXPORT ref<ResourceDatabase> loadResource(const String& path, bool quick=true);
185 
188 
190  VLCORE_EXPORT bool writeResource(const String& path, ResourceDatabase* resource);
191 
194 }
195 
196 #endif
VLCORE_EXPORT bool canLoad(const String &path)
Short version of defLoadWriterManager()->canLoad(path).
std::vector< ref< Object > > mResources
std::vector< ref< Object > > & resources()
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
T * next(int &cur_pos) const
Starts to look for the next object of the specified type from the given position. ...
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
size_t count() const
Don&#39;t use inside loops! Counts the number object of the specified type.
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
VLCORE_EXPORT ref< ResourceDatabase > loadResource(const String &path, bool quick=true)
Short version of defLoadWriterManager()->loadResource(path, quick).
Visualization Library main namespace.
const std::vector< ref< Object > > & resources() const
The base class for all the reference counted objects.
Definition: Object.hpp:158
VLCORE_EXPORT bool canWrite(const String &path)
Short version of defLoadWriterManager()->canWrite(path).
void extract(std::vector< ref< T > > &resources, bool clear_vector=true)
Returns all the objects of the specified type in the given vector and removes them from the ResourceD...
#define NULL
Definition: OpenGLDefs.hpp:81
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
VLCORE_EXPORT bool writeResource(const String &path, ResourceDatabase *resource)
Short version of defLoadWriterManager()->writeResource(path, resource).
The ResourceDatabase class contains and manipulates a set of resources.