1 #ifndef RESOURCE_LIBRARY_H 2 #define RESOURCE_LIBRARY_H 5 #include "AnyCollection.h" 44 ResourceBase(
const std::string& name,
const std::string& fn);
46 virtual bool Load(
const std::string& fn);
48 virtual bool Save(
const std::string& fn);
50 virtual bool Load(std::istream& in) {
return false; }
51 virtual bool Save(std::ostream& out) {
return false; }
58 virtual const char*
Type()
const {
return typeid(*this).name(); }
66 std::string name,fileName;
69 typedef std::shared_ptr<ResourceBase> ResourcePtr;
105 void AddLoader(
const std::string& ext);
110 bool SaveXml(
const std::string& fn);
112 bool LoadXml(
const std::string& fn);
118 bool LazyLoadXml(
const std::string& fn);
120 bool SaveJSON(std::ostream& s);
122 bool LoadJSON(std::istream& s);
124 bool LazyLoadJSON(std::istream& s);
132 void AddBaseDirectory(
const std::string& dir);
136 void ChangeBaseDirectory(
const std::string& dir);
140 bool SaveItem(
const std::string& name);
142 bool LoadAll(
const std::string& dir);
144 bool LazyLoadAll(
const std::string& dir);
146 ResourcePtr LoadItem(
const std::string& fn);
152 T* Get(
const std::string& name,
int index);
154 std::vector<ResourcePtr>& Get(
const std::string& name);
156 std::vector<ResourcePtr>& GetByType(
const std::string& type);
158 std::vector<ResourcePtr>& GetByType();
160 std::vector<T*> GetPtrsByType();
162 size_t Count(
const std::string& name)
const;
164 size_t CountByType(
const std::string& type)
const;
166 size_t CountByType()
const;
168 std::vector<ResourcePtr> Enumerate()
const;
171 void Add(
const ResourcePtr& resource);
172 bool Erase(
const ResourcePtr& resource);
173 bool Erase(
const std::string& name,
int index=-1);
176 std::string DefaultFileName(
const ResourcePtr& r);
178 typedef std::map<std::string,std::vector<ResourcePtr> > Map;
181 Map itemsByName,itemsByType;
187 std::vector<T*> ResourcesByType(std::vector<ResourcePtr>& resources)
189 std::vector<T*> rtype;
190 for(
size_t i=0;i<resources.size();i++) {
191 T* ptr =
dynamic_cast<T*
>(resources[i].get());
192 if(ptr) rtype.push_back(ptr);
198 std::vector<ResourcePtr> ResourcesByType(std::vector<ResourcePtr>& resources,
const std::string& type);
201 std::vector<std::string> ResourceTypes(
const std::vector<ResourcePtr>& resources);
207 const char* BasicResourceTypeName() {
return typeid(T).name(); }
225 BasicResource(
const T& val,
const std::string& name,
const std::string& fileName) :
ResourceBase(name,fileName),data(val) {}
227 virtual bool Load(std::istream& in) {
234 virtual bool Save(std::ostream& out) {
235 out<<data<<std::endl;
238 virtual bool Load(
const std::string& fn) {
return ResourceBase::Load(fn); }
239 virtual bool Load() {
return ResourceBase::Load(); }
240 virtual bool Save(
const std::string& fn) {
return ResourceBase::Save(fn); }
241 virtual bool Save() {
return ResourceBase::Save(); }
242 virtual const char*
Type()
const {
return BasicResourceTypeName<T>(); }
249 template <>
const char* BasicResourceTypeName<int>();
250 template <>
const char* BasicResourceTypeName<double>();
251 template <>
const char* BasicResourceTypeName<std::string>();
272 virtual std::vector<std::string>
CastTypes()
const {
return std::vector<std::string>(); }
274 virtual std::vector<std::string>
ExtractTypes()
const {
return SubTypes(); }
276 virtual std::vector<std::string>
SubTypes()
const {
return std::vector<std::string>(); }
279 virtual ResourcePtr
Cast(
const char* type) {
return NULL; }
283 virtual bool Extract(
const char* subtype,std::vector<ResourcePtr>& subobjects);
289 virtual bool Pack(std::vector<ResourcePtr>& subobjects,std::string* errorMessage=NULL) {
if(errorMessage) *errorMessage = std::string(
Type())+
"::Pack not implemented";
return false; }
298 virtual bool Unpack(std::vector<ResourcePtr>& subobjects,
bool* incomplete=NULL) {
return false; }
315 virtual bool Load(std::istream& in) {
318 if(in.bad())
return false;
320 for(
size_t i=0;i<n;i++) {
322 if(in.bad())
return false;
326 virtual bool Save(std::ostream& out) {
327 out<<data.size()<<
'\t';
328 for(
size_t i=0;i<data.size();i++) {
330 if(i+1!=data.size()) out<<
" ";
335 virtual bool Load(
const std::string& fn) {
return ResourceBase::Load(fn); }
336 virtual bool Load() {
return ResourceBase::Load(); }
337 virtual bool Save(
const std::string& fn) {
return ResourceBase::Save(fn); }
338 virtual bool Save() {
return ResourceBase::Save(); }
339 virtual bool Load(
AnyCollection& c) {
return c[
"data"].asvector(data); }
340 virtual bool Save(
AnyCollection& c) { c[
"data"] = data;
return true;}
341 virtual const char*
Type()
const {
return BasicResourceTypeName<std::vector<T> >(); }
346 virtual std::vector<std::string>
SubTypes()
const {
return std::vector<std::string>(1,BasicResourceTypeName<T>()); }
347 virtual bool Extract(
const char* subtype,std::vector<ResourcePtr>& subobjects) {
348 if(0==strcmp(subtype,BasicResourceTypeName<T>()))
349 return Unpack(subobjects);
352 virtual bool Pack(std::vector<ResourcePtr>& subobjects,std::string* errorMessage=NULL) {
353 for(
size_t i=0;i<subobjects.size();i++)
354 if(
typeid(*subobjects[i]) !=
typeid(BaseType)) {
355 if(errorMessage) *errorMessage = std::string(
"Subobject does not have type ")+std::string(BasicResourceTypeName<T>());
358 data.resize(subobjects.size());
359 for(
size_t i=0;i<subobjects.size();i++)
360 data[i] = dynamic_cast<BaseType*>(&*subobjects[i])->data;
363 virtual bool Unpack(std::vector<ResourcePtr>& subobjects,
bool* incomplete=NULL) {
364 subobjects.resize(data.size());
365 for(
size_t i=0;i<data.size();i++) {
366 subobjects[i] = std::make_shared<BaseType>(data[i]);
367 std::stringstream ss;
369 subobjects[i]->name = ss.str();
377 template <>
const char* BasicResourceTypeName<std::vector<int> >();
378 template <>
const char* BasicResourceTypeName<std::vector<double> >();
379 template <>
const char* BasicResourceTypeName<std::vector<std::string> >();
391 virtual bool Load(
const std::string& fn);
392 virtual bool Save(
const std::string& fn);
395 virtual const char*
Type()
const {
return "ResourceLibrary"; }
398 virtual std::vector<std::string> SubTypes()
const;
399 virtual bool Extract(
const char* subtype,std::vector<ResourcePtr>& subobjects);
400 virtual bool Pack(std::vector<ResourcePtr>& subobjects,std::string* errorMessage=NULL);
401 virtual bool Unpack(std::vector<ResourcePtr>& subobjects,
bool* incomplete=NULL);
409 ResourcePtr res(
new T);
410 if(knownTypes.count(res->Type()) == 0) {
411 knownTypes[res->Type()].push_back(res);
415 if(knownTypes[res->Type()][0]->Type() != res->Type())
416 LOG4CXX_ERROR(KrisLibrary::logger(),
"ResourceLibrary: Potential conflict with type "<<res->Type());
417 knownTypes[res->Type()][0] = res;
425 ResourcePtr res(
new T);
426 loaders[ext].push_back(res);
430 T* ResourceLibrary::Get(
const std::string& name,
int index)
432 std::vector<ResourcePtr>& items=Get(name);
433 if(index < 0 || index > (
int)items.size())
return NULL;
434 return dynamic_cast<T*
>(items[index].get());
441 return GetByType(temp.Type());
445 std::vector<T*> ResourceLibrary::GetPtrsByType()
447 std::vector<ResourcePtr>& items = GetByType<T>();
448 std::vector<T*> cast_items(items.size());
449 for(
size_t i=0;i<items.size();i++)
450 cast_items[i] = dynamic_cast<T*>(items[i].
get());
458 return CountByType(temp.Type());
virtual ResourceBase * Make()
Definition: ResourceLibrary.h:396
A flexible hierarchical collection of AnyValues, which can be easily initialized to contain primitive...
Definition: AnyCollection.h:73
virtual const char * Type() const
Definition: ResourceLibrary.h:242
A basic data type.
Definition: ResourceLibrary.h:219
virtual std::vector< std::string > CastTypes() const
Returns a list of subtypes that can be used in Cast.
Definition: ResourceLibrary.h:272
virtual std::vector< std::string > SubTypes() const
Returns a list of subtypes that will be produced in Unpack.
Definition: ResourceLibrary.h:276
virtual ResourceBase * Make()
Definition: ResourceLibrary.h:342
virtual ResourceBase * Copy()
Definition: ResourceLibrary.h:244
virtual const char * Type() const
Definition: ResourceLibrary.h:395
virtual bool Pack(std::vector< ResourcePtr > &subobjects, std::string *errorMessage=NULL)
Definition: ResourceLibrary.h:289
std::vector< ResourcePtr > & GetByType(const std::string &type)
Get by type.
Definition: ResourceLibrary.cpp:633
A basic array data type.
Definition: ResourceLibrary.h:306
virtual bool Extract(const char *subtype, std::vector< ResourcePtr > &subobjects)
Definition: ResourceLibrary.h:347
virtual ResourceBase * Copy()
Definition: ResourceLibrary.h:343
A generic "resource" that can be saved/loaded to disk.
Definition: ResourceLibrary.h:39
size_t CountByType(const std::string &type) const
Count by type.
Definition: ResourceLibrary.cpp:645
virtual const char * Type() const
Definition: ResourceLibrary.h:341
void AddType()
Adds a new type not associated with a given filename.
Definition: ResourceLibrary.h:407
virtual ResourceBase * Make()
Definition: ResourceLibrary.h:61
virtual ResourcePtr Cast(const char *type)
Definition: ResourceLibrary.h:279
virtual bool Pack(std::vector< ResourcePtr > &subobjects, std::string *errorMessage=NULL)
Definition: ResourceLibrary.h:352
virtual bool Unpack(std::vector< ResourcePtr > &subobjects, bool *incomplete=NULL)
Definition: ResourceLibrary.h:363
void AddLoader(const std::string &ext)
Adds a new type associated with the given file extension ext.
Definition: ResourceLibrary.h:422
virtual ResourceBase * Make()
Definition: ResourceLibrary.h:243
virtual const char * Type() const
Definition: ResourceLibrary.h:58
The logging system used in KrisLibrary.
A resource that is composed of multiple sub-objects, which can themselves be considered resources...
Definition: ResourceLibrary.h:263
virtual std::vector< std::string > ExtractTypes() const
Returns a list of subtypes that can be used in Extract.
Definition: ResourceLibrary.h:274
A resource that contains a ResourceLibrary. Useful for hierarchical resource libraries.
Definition: ResourceLibrary.h:388
virtual bool Unpack(std::vector< ResourcePtr > &subobjects, bool *incomplete=NULL)
Definition: ResourceLibrary.h:298
virtual ResourceBase * Copy()
Definition: ResourceLibrary.h:64
virtual std::vector< std::string > SubTypes() const
Returns a list of subtypes that will be produced in Unpack.
Definition: ResourceLibrary.h:346
Definition: PropertyMap.cpp:9
A collection of resources, which may be loaded/saved as separate files in a directory or inlined into...
Definition: ResourceLibrary.h:97