KrisLibrary  1.0.0
Public Types | Public Member Functions | Public Attributes | List of all members
ResourceLibrary Class Reference

A collection of resources, which may be loaded/saved as separate files in a directory or inlined into a single XML file. Helps with managing subdirectories of data. More...

#include <ResourceLibrary.h>

Public Types

typedef std::map< std::string, std::vector< ResourcePtr > > Map
 

Public Member Functions

template<class T >
void AddType ()
 Adds a new type not associated with a given filename.
 
template<class T >
void AddLoader (const std::string &ext)
 Adds a new type associated with the given file extension ext.
 
void Clear ()
 Erases all elements (maintains loaders)
 
bool SaveXml (const std::string &fn)
 Saves to an XML file.
 
bool LoadXml (const std::string &fn)
 Loads from an XML file.
 
bool Save (TiXmlElement *e)
 Saves to an XML element.
 
bool Load (TiXmlElement *e)
 Loads from an XML element.
 
bool LazyLoadXml (const std::string &fn)
 Loads from an XML file without loading external files.
 
bool SaveJSON (std::ostream &s)
 Saves to a JSON stream.
 
bool LoadJSON (std::istream &s)
 Loads from a JSON stream.
 
bool LazyLoadJSON (std::istream &s)
 Loads from a JSON stream without loading external files.
 
bool Save (AnyCollection &c)
 Saves to an AnyCollection.
 
bool Load (AnyCollection &c)
 Loads from an AnyCollection.
 
bool LazyLoad (AnyCollection &c)
 Loads from an AnyCollection without loading external files.
 
void AddBaseDirectory (const std::string &dir)
 Prepends the given directory onto each resource's filename.
 
void ChangeBaseDirectory (const std::string &dir)
 
bool SaveAll ()
 Saves all resources to their given fileName's.
 
bool SaveItem (const std::string &name)
 Saves the named resource only.
 
bool LoadAll (const std::string &dir)
 Loads all resources in a given path.
 
bool LazyLoadAll (const std::string &dir)
 Loads all resources in a given path without loading external files.
 
ResourcePtr LoadItem (const std::string &fn)
 Loads a new resource from the given file and adds it to the library.
 
bool ReloadAll ()
 Reloads all resources from their given filenames.
 
template<class T >
T * Get (const std::string &name, int index)
 
std::vector< ResourcePtr > & Get (const std::string &name)
 Get by name.
 
std::vector< ResourcePtr > & GetByType (const std::string &type)
 Get by type.
 
template<class T >
std::vector< ResourcePtr > & GetByType ()
 
template<class T >
std::vector< T * > GetPtrsByType ()
 
size_t Count (const std::string &name) const
 Count by name.
 
size_t CountByType (const std::string &type) const
 Count by type.
 
template<class T >
size_t CountByType () const
 
std::vector< ResourcePtr > Enumerate () const
 Return all.
 
void Add (const ResourcePtr &resource)
 
bool Erase (const ResourcePtr &resource)
 
bool Erase (const std::string &name, int index=-1)
 
std::string DefaultFileName (const ResourcePtr &r)
 

Public Attributes

Map knownTypes
 
Map loaders
 
Map itemsByName
 
Map itemsByType
 

Detailed Description

A collection of resources, which may be loaded/saved as separate files in a directory or inlined into a single XML file. Helps with managing subdirectories of data.

Inlined XML files are structured: <resource_library> <{resource.Type()} name={resource.name} file={resource.fileName}> data </{resource.Type()}>

</resource_library> where data is given if the item supports inline reading/writing to iostreams, the xml sub-tree if it supports saving to xml, and otherwise it is omitted. In the latter case, the resource is saved to its fileName.

Inlined JSON files are structured [ {type:resource.Type(), name:resource.name, file:resource.fileName, data:resource.data } ]

Resources are accessed either by name (Get()) or type (GetByType()).

Member Function Documentation

void ResourceLibrary::ChangeBaseDirectory ( const std::string &  dir)

Changes the first directory in each resource's filename to dir. If the filename is not in a subdirectory, dir is prepended. If dir is empty, this deletes a base directory from the filename

References ArrayUtils::copy(), JoinPath(), and SplitPath().


The documentation for this class was generated from the following files: