sftools  2.0 dev
Bunch of tools for SFML application development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Loaders.hpp
Go to the documentation of this file.
1 /*
2 
3  sftools - Copyright (c) 2012-2013 Marco Antognini <antognini.marco@gmail.com>
4 
5  This software is provided 'as-is', without any express or implied warranty. In
6  no event will the authors be held liable for any damages arising from the use
7  of this software.
8 
9  Permission is granted to anyone to use this software for any purpose, including
10  commercial applications, and to alter it and redistribute it freely, subject to
11  the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not claim
14  that you wrote the original software. If you use this software in a product,
15  an acknowledgment in the product documentation would be appreciated but is
16  not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source distribution.
22 
23  */
24 
30 #ifndef __SFTOOLS_LOADERS_HPP__
31 #define __SFTOOLS_LOADERS_HPP__
32 
34 #include <string>
35 
40 namespace sftools
41 {
46  namespace loader
47  {
60  template <typename R>
62  {
66  virtual ~ResourceLoader()
67  {
68  // Nothing to do
69  }
70 
80  R* operator()(std::string const& id)
81  {
82  R* ptr = new R;
83 
85 
86  // Find the first location that holds an file called "id"
87  for (Locations::ConstIterator it = locs.begin();
88  it != locs.end();
89  ++it)
90  {
91  if (load(*ptr, *it + id))
92  {
93  return ptr; // Success!
94  }
95  }
96 
97  // Hum... got here ?
98  // This means the resource was not loaded properly.
99  delete ptr;
100  ptr = 0;
101  return 0;
102  }
103 
114  virtual bool load(R& res, std::string src) = 0;
115  };
116 
122  template <typename R>
124  {
125  bool load(R& res, std::string src)
126  {
127  return res.loadFromFile(src);
128  }
129  };
130 
136  template <typename R>
138  {
139  bool load(R& res, std::string src)
140  {
141  return res.openFromFile(src);
142  }
143  };
144  }
145 }
146 
147 #endif // __SFTOOLS_LOADERS_HPP__