zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Database.h
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
32 #ifndef ZENI_DATABASE_H
33 #define ZENI_DATABASE_H
34 
35 #include <Zeni/Error.h>
36 #include <Zeni/Hash_Map.h>
37 #include <Zeni/XML.h>
38 /* \cond */
39 #include <list>
40 #include <set>
41 /* \endcond */
42 
43 namespace Zeni {
44 
45  template <class TYPE>
46  class Database {
47  struct Lookup {
48  struct Handle {
49  Handle();
50  Handle(const String &filename_);
51  Handle(TYPE * const &ptr_, const String &filename_, const bool &lent_, const bool &keep_);
52 
53  bool operator==(const Handle &rhs) const;
54 
55  TYPE * ptr;
57  bool lent;
58  bool keep;
59  };
60 
61  typedef std::list<Handle> Handles;
62 
63  private:
64  // Undefined
65  Lookup(const Lookup &);
66  Lookup & operator=(const Lookup &);
67 
68  public:
69  Lookup();
70  Lookup(const unsigned long &id_, const Handle &handle_);
71 
72  unsigned long id;
73  Handles handles;
74  };
75 
76  typedef std::list<String> Filenames;
77  typedef Unordered_Map<String, Lookup *> Lookups; // (id, filename)
78  typedef Unordered_Map<unsigned long, TYPE *> Entries; // (datum, lent)
79 
80  // Undefined
81  Database(const Database &);
82  Database & operator=(const Database &);
83 
84  public:
85  Database(const String &filename, const String &xml_identifier);
86  virtual ~Database();
87 
88  unsigned long get_id(const String &name) const;
89  unsigned long find(const String &name) const;
90  bool find(const unsigned long &id) const;
91 
92  TYPE & operator[](const String &name) const;
93  TYPE & operator[](const unsigned long &id) const;
94 
95  // Loaders
96  unsigned long give(const String &name, TYPE * const &type, const bool &keep, const String &filename = "");
97  unsigned long lend(const String &name, TYPE * const &type, const bool &keep);
98  void clear(const String &name, const String &filename = "");
99 
100  // Initialization Functions
101  void clear();
102  void load_file(const String &filename);
103  void unload_file(const String &filename);
104  void reload();
105 
106  const bool & lost_resources();
107  void lose_resources();
108  void unlose_resources();
109 
110  protected:
111  void init();
112  void uninit();
113  bool give_priority(const String &name, const bool &lent, const bool &keep, const String &filename = "");
114 
115  private:
116  virtual void on_load() {}
117  virtual void on_clear() {}
118  virtual void on_lose() {}
119 
120  virtual TYPE * load(XML_Element_c &xml_element, const String &name, const String &filename) = 0;
121 
122  String m_xml_identifier;
123 
124 #ifdef _WINDOWS
125 #pragma warning( push )
126 #pragma warning( disable : 4251 )
127 #endif
128  Filenames m_filenames;
129  Lookups m_lookups;
130  Entries m_entries;
131 #ifdef _WINDOWS
132 #pragma warning( pop )
133 #endif
134 
135  bool m_lost;
136  };
137 
138  struct ZENI_DLL Database_File_Not_Loaded : public Error {
139  Database_File_Not_Loaded(const String &identifier) : Error("Zeni File '" + identifier + "' Not Loaded") {}
140  };
141 
142  struct ZENI_DLL Database_Entry_Not_Found : public Error {
143  Database_Entry_Not_Found(const String &identifier) : Error("Zeni Database Entry '" + identifier + "' Not Found") {}
144  };
145 
146  struct ZENI_DLL Database_Load_Entry_Failed : public Error {
147  Database_Load_Entry_Failed(const String &identifier) : Error("Zeni Database Entry '" + identifier + "' Could Not Be Loaded") {}
148  };
149 
150  struct ZENI_DLL Null_Database_Entry_Set : public Error {
151  Null_Database_Entry_Set() : Error("Null Entry Added to Zeni Database") {}
152  };
153 
154 }
155 
156 #endif
Definition: Database.h:150
void lose_resources()
Wipe losable resources and prepare to reload them when they are next needed.
Definition: Database.hxx:386
Definition: Database.h:142
unsigned long give(const String &name, TYPE *const &type, const bool &keep, const String &filename="")
Add an entry (which it will later delete)
Definition: Database.hxx:91
unsigned long lend(const String &name, TYPE *const &type, const bool &keep)
Add an entry (which it will NEVER delete)
Definition: Database.hxx:118
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum type
Definition: gl2ext.h:845
Definition: Database.h:146
Database_Load_Entry_Failed(const String &identifier)
Definition: Database.h:147
unsigned long get_id(const String &name) const
Get an id by name, possibly throwing an Error.
Definition: Database.hxx:172
EGLImageKHR EGLint * name
Definition: eglext.h:284
void reload()
lose_resources + init
Definition: Database.hxx:308
GLuint id
Definition: gl2ext.h:1142
void load_file(const String &filename)
Load all resources from a given file, giving them highest priority.
Definition: Database.hxx:225
A Texture Database Singleton.
Definition: Database.h:46
void clear()
Permanently clear all resources.
Definition: Database.hxx:220
virtual ~Database()
Definition: Database.hxx:86
Database_File_Not_Loaded(const String &identifier)
Definition: Database.h:139
TYPE & operator[](const String &name) const
Get a TYPE by name.
Definition: Database.hxx:215
bool operator==(const Handle &rhs) const
Definition: Database.hxx:60
unsigned long find(const String &name) const
Get an id by name, without throwing an Error.
Definition: Database.hxx:182
void unlose_resources()
If resources have been lost, then reload them.
Definition: Database.hxx:415
const bool & lost_resources()
Check to see if resources have been lost.
Definition: Database.hxx:381
bool give_priority(const String &name, const bool &lent, const bool &keep, const String &filename="")
If &#39;lent&#39;, &#39;keep&#39;, and &#39;filename&#39; match, give priority over other &#39;name&#39; entries. ...
Definition: Database.hxx:354
The Error Class.
Definition: Error.h:52
Definition: infutil.h:15
Database_Entry_Not_Found(const String &identifier)
Definition: Database.h:143
void unload_file(const String &filename)
Unload all resources from a given file, reloading lower priority resources.
Definition: Database.hxx:269
Null_Database_Entry_Set()
Definition: Database.h:151