Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/ResourceDatabase.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef ResourceDatabase_INCLUDE_ONCE
00033 #define ResourceDatabase_INCLUDE_ONCE
00034 
00035 #include <vlCore/Object.hpp>
00036 #include <vlCore/String.hpp>
00037 #include <vector>
00038 #include <algorithm>
00039 
00040 namespace vl
00041 {
00042   class VirtualFile;
00043 
00048   class VLCORE_EXPORT ResourceDatabase: public Object
00049   {
00050     VL_INSTRUMENT_CLASS(vl::ResourceDatabase, Object)
00051 
00052   public:
00053     ResourceDatabase()
00054     {
00055       VL_DEBUG_SET_OBJECT_NAME()
00056     }
00057 
00058     const std::vector< ref<Object> >& resources() const { return mResources; }
00059 
00060     std::vector< ref<Object> >& resources() { return mResources; }
00061 
00063     template<class T>
00064     T* next(int& cur_pos) const
00065     {
00066       for(unsigned i=cur_pos; i<mResources.size(); ++i)
00067       {
00068         ref<T> r = cast<T>(mResources[i].get());
00069         if (r)
00070         {
00071           cur_pos = i+1;
00072           return r.get();
00073         }
00074       }
00075       return NULL;
00076     }
00077 
00079     template<class T>
00080     void get( std::vector< ref<T> >& resources, bool clear_vector=true )
00081     {
00082       if (clear_vector)
00083         resources.clear();
00084 
00085       for( unsigned i=0; i<mResources.size(); ++i )
00086       {
00087         ref<T> r = cast<T>(mResources[i].get());
00088         if (r)
00089           resources.push_back(r);
00090       }
00091     }
00092 
00094     template<class T>
00095     void extract( std::vector< ref<T> >& resources, bool clear_vector=true )
00096     {
00097       if (clear_vector)
00098         resources.clear();
00099 
00100       size_t start = resources.size();
00101 
00102       for( unsigned i=mResources.size(); i--; )
00103       {
00104         ref<T> r = cast<T>(mResources[i].get());
00105         if (r)
00106         {
00107           resources.push_back(r);
00108           mResources.erase(mResources.begin()+i);
00109         }
00110       }
00111 
00112       std::reverse(resources.begin()+start, resources.end());
00113     }
00114 
00116     template<class T>
00117     size_t count() const
00118     {
00119       size_t count=0;
00120       for(unsigned i=0; i<mResources.size(); ++i)
00121       {
00122         const T* r = cast_const<T>(mResources[i].get());
00123         if (r)
00124           ++count;
00125       }
00126       return count;
00127     }
00128 
00130     template<class T>
00131     const T* get(int j) const
00132     {
00133       int count=0;
00134       for(unsigned i=0; i<mResources.size(); ++i)
00135       {
00136         const T* r = cast_const<T>(mResources[i].get());
00137         if (r)
00138         {
00139           if (count == j)
00140             return r;
00141           else
00142             ++count;
00143         }
00144       }
00145       return NULL;
00146     }
00147 
00149     template<class T>
00150     T* get(int j)
00151     {
00152       int count=0;
00153       for(unsigned i=0; i<mResources.size(); ++i)
00154       {
00155         T* r = cast<T>(mResources[i].get());
00156         if (r)
00157         {
00158           if (count == j)
00159             return r;
00160           else
00161             ++count;
00162         }
00163       }
00164       return NULL;
00165     }
00166 
00167   protected:
00168     std::vector< ref<Object> > mResources;
00169   };
00170 
00172   VLCORE_EXPORT bool canLoad(const String& path);
00173 
00175   VLCORE_EXPORT bool canWrite(const String& path);
00176 
00178   VLCORE_EXPORT bool canLoad(VirtualFile* file);
00179 
00181   VLCORE_EXPORT bool canWrite(VirtualFile* file);
00182 
00184   VLCORE_EXPORT ref<ResourceDatabase> loadResource(const String& path, bool quick=true);
00185 
00187   VLCORE_EXPORT ref<ResourceDatabase> loadResource(VirtualFile* file, bool quick=true);
00188 
00190   VLCORE_EXPORT bool writeResource(const String& path, ResourceDatabase* resource);
00191 
00193   VLCORE_EXPORT bool writeResource(VirtualFile* file, ResourceDatabase* resource);
00194 }
00195 
00196 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:31.
Permission is granted to use this page to write and publish articles regarding Visualization Library.