Commit ca449c1b authored by Robert Menzel's avatar Robert Menzel

updated manager documentation and only create Shader manager per default

parent 2534cd73
......@@ -34,7 +34,7 @@ public:
: Resource::SingleFileBasedCreator<Shader>(_filename, Base::Settings::the()->getFullShaderPath()),
mType(GL_INVALID_ENUM)
{}
virtual ~ShaderCreator(void) {}
virtual ~ShaderCreator() {}
// ==================================================================================================== \/
// ============================================================================================ METHODS \/
......@@ -46,7 +46,7 @@ public:
// ============================================================================================ OVERRIDE \/
// ===================================================================================================== \/
public:
virtual SharedShader create(void);
virtual SharedShader create();
virtual bool update(SharedShader& shader);
// =================================================================================================== \/
......
......@@ -125,19 +125,11 @@ public:
inline ShaderProgramCreator& externUniformBufferLocations(SharedLocationMappings _mapping) { mUniformBufferLocations = _mapping; return *this; }
//
// Modifying the resource name:
// A ShaderProgram consists of multiple shader files, so associating the ShaderProgram with just one of those file names
// is often not useful.
//
inline ShaderProgramCreator& setResourceName(const std::string &_resourceName) { mResourceName = _resourceName; return *this; }
// ===================================================================================================== \/
// ============================================================================================ OVERRIDE \/
// ===================================================================================================== \/
public:
virtual SharedShaderProgram create(void);
virtual SharedShaderProgram create();
virtual bool update(SharedShaderProgram &_shaderProgram);
// =================================================================================================== \/
......
......@@ -22,6 +22,11 @@
namespace ACGL{
namespace OpenGL{
typedef Resource::MultiFileManager<ShaderProgram> ShaderProgramFileManager;
typedef Resource::FileManager<Shader> ShaderFileManager;
#ifdef ACGL_INCLUDE_DEPRECATED_FUNCTIONALITY
typedef Resource::NameManager<ArrayBuffer> ArrayBufferNameManager;
typedef Resource::FileManager<ArrayBuffer> ArrayBufferFileManager;
......@@ -32,17 +37,14 @@ typedef Resource::NameManager<FrameBufferObject> FrameBufferObjectNameManager;
typedef Resource::NameManager<RenderBuffer> RenderBufferNameManager;
typedef Resource::NameManager<Shader> ShaderNameManager;
typedef Resource::FileManager<Shader> ShaderFileManager;
typedef Resource::NameManager<ShaderProgram> ShaderProgramNameManager;
typedef Resource::MultiFileManager<ShaderProgram> ShaderProgramFileManager;
typedef Resource::NameManager<ShaderProgramObject> ShaderProgramObjectNameManager;
#ifdef ACGL_INCLUDE_DEPRECATED_FUNCTIONALITY
typedef Resource::NameManager<Texture> TextureNameManager;
typedef Resource::FileManager<Texture> TextureFileManager;
#endif
typedef Resource::NameManager<TextureData> TextureDataNameManager;
typedef Resource::FileManager<TextureData> TextureDataFileManager;
......@@ -67,14 +69,10 @@ typedef Resource::FileManager<VertexArrayObject> VertexArrayObjectFileManager;
typedef Resource::NameManager<VertexArrayObject> VertexArrayObjectNameManager;
#endif // GL >= 3.0
#endif
} // OpenGL
} // ACGL
//note: when we start having managers for non-OpenGL objects we should move this ;-)
#define ACGL_FOR_EACH_RESOURCE(MANAGER,RESOURCE,ELEMENT) \
for(ACGL::Resource::MANAGER<RESOURCE>::ResourceContainer::const_iterator ELEMENT = ACGL::Resource::MANAGER<RESOURCE>::the()->begin(); \
ELEMENT != MANAGER<RESOURCE>::the()->end(); \
++ELEMENT)
#endif // ACGL_OPENGL_MANAGER_HH
/***********************************************************************
* Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
* Copyright 2011-2013 Computer Graphics Group RWTH Aachen University. *
* All rights reserved. *
* Distributed under the terms of the MIT License (see LICENSE.TXT). *
**********************************************************************/
......@@ -7,6 +7,40 @@
#ifndef ACGL_RESOURCE_FILEMANAGER_HH
#define ACGL_RESOURCE_FILEMANAGER_HH
/**
* The FileManager and MultiFileManager are like the NameManager Singleton-
* maps to access resources from everywhere by calling them by name.
* In addition to the NameManager, these containers also feature the
* functionality to reload all contained resources. For this it is important
* that in addition to the resource and the name a Creator is also provided.
*
* Note that changes to the objects returned by these managers might get
* destroyed in case the resource gets reloaded. The update() function
* of the Creator should as far as possible transfer the state from the
* old object to the new one, but this might not always work.
* Note that declaring the object as const isn't always helpful as well
* as for OpenGL objects the state often gets altered (e.g. uniforms are being set)
* and disalowing the change of OpenGL states is not useful.
*
* To use one, create a FileManager for the resource:
*
* typedef Resource::FileManager<Foo> FooFileManager;
*
* Then add your resources:
*
* SharedFoo foo = FooNameManager::the()->get( FooCreator("filename").option1().option2() );
*
* And later query the resource:
*
* SharedFoo foo = FooNameManager::the()->query( "filename" );
*
* If get() gets called more than once for the same resource, only one copy gets created.
*
* Per default the resource name is the filename (FileManager) or the concatenated list of
* alphabetically ordered filenames (MultiFileManager).
* The resource name can get explicitly set by (Multi)FileBasedCreator.setResourceName().
*/
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Singleton.hh>
#include <ACGL/Base/Macros.hh>
......@@ -34,17 +68,25 @@ public:
};
typedef std::map<std::string, Resource> ResourceMap;
typedef ResourceMap ResourceContainer;
virtual ~FileManager(void) {}
virtual ~FileManager() {}
//! returns the resource and creates it if it isn't created yet.
template<typename CONTROLLER>
ConstSharedRESOURCE get(const CONTROLLER& _controller);
ConstSharedRESOURCE query(const std::string& _filename);
bool exists(const std::string& _key);
bool erase(const std::string& key);
void eraseAll(void);
SharedRESOURCE get(const CONTROLLER& _controller);
//! returns the resource by name (or NULL if it isn't stored)
SharedRESOURCE query(const std::string &_filename);
//! test the existance of a resource
bool exists(const std::string &_key);
bool erase(const std::string &_key);
void eraseAll();
//! update a specific resource
bool update(const std::string& key);
//! update all resources - will only update the resources if the file modification time has changed
void updateAll(void);
typename ResourceMap::const_iterator begin(void) const { return mResourceMap.begin(); }
......@@ -59,8 +101,10 @@ private:
ResourceMap mResourceMap;
};
template<typename RESOURCE> template<typename CONTROLLER>
typename FileManager<RESOURCE>::ConstSharedRESOURCE FileManager<RESOURCE>::get(const CONTROLLER& _controller)
typename FileManager<RESOURCE>::SharedRESOURCE FileManager<RESOURCE>::get(const CONTROLLER &_controller)
{
typename ResourceMap::iterator existingResource = mResourceMap.find(_controller.getResourceName());
if(existingResource != mResourceMap.end())
......@@ -83,7 +127,7 @@ typename FileManager<RESOURCE>::ConstSharedRESOURCE FileManager<RESOURCE>::get(c
}
template<typename RESOURCE>
typename FileManager<RESOURCE>::ConstSharedRESOURCE FileManager<RESOURCE>::query(const std::string& _key)
typename FileManager<RESOURCE>::SharedRESOURCE FileManager<RESOURCE>::query(const std::string &_key)
{
typename ResourceMap::iterator existingResource = mResourceMap.find(_key);
if(existingResource != mResourceMap.end())
......@@ -92,7 +136,7 @@ typename FileManager<RESOURCE>::ConstSharedRESOURCE FileManager<RESOURCE>::query
}
template<typename RESOURCE>
bool FileManager<RESOURCE>::exists(const std::string& _key)
bool FileManager<RESOURCE>::exists(const std::string &_key)
{
typename ResourceMap::iterator existingResource = mResourceMap.find(_key);
if(existingResource != mResourceMap.end())
......@@ -101,7 +145,7 @@ bool FileManager<RESOURCE>::exists(const std::string& _key)
}
template<typename RESOURCE>
bool FileManager<RESOURCE>::erase(const std::string& _key)
bool FileManager<RESOURCE>::erase(const std::string &_key)
{
typename ResourceMap::iterator existingResource = mResourceMap.find(_key);
if(existingResource != mResourceMap.end())
......@@ -115,7 +159,7 @@ bool FileManager<RESOURCE>::erase(const std::string& _key)
}
template<typename RESOURCE>
void FileManager<RESOURCE>::eraseAll(void)
void FileManager<RESOURCE>::eraseAll()
{
mResourceMap.clear();
}
......@@ -157,7 +201,6 @@ public:
};
typedef std::map<std::string, Resource> ResourceMap;
typedef ResourceMap ResourceContainer;
virtual ~MultiFileManager(void) {}
......
......@@ -64,6 +64,12 @@ public:
return mResourceName;
}
//
// Modifying the resource name:
//
MultiFileBasedCreator& setResourceName(const std::string &_resourceName) { mResourceName = _resourceName; return *this; }
protected:
//! This constructor does not add any files, use this only on derived types which constructor will add a file itself!
MultiFileBasedCreator() : mBasePath("") {}
......
/***********************************************************************
* Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
* Copyright 2011-2013 Computer Graphics Group RWTH Aachen University. *
* All rights reserved. *
* Distributed under the terms of the MIT License (see LICENSE.TXT). *
**********************************************************************/
......@@ -7,6 +7,29 @@
#ifndef ACGL_RESOURCE_NAMEMANAGER_HH
#define ACGL_RESOURCE_NAMEMANAGER_HH
/**
* A NameManager is basically a string-to-resource map implemented as a singleton. This way
* resources can be referenced via a string and accessed from the whole progam. While this is
* not very efficient or well organized, it can help the prototyping process.
*
* To use one, create a NameManager for the resource:
*
* typedef Resource::NameManager<Foo> FooNameManager;
*
* Then add your resources:
*
* SharedFoo foo = ...;
* FooNameManager::the()->add( "nameOfFoo", foo );
*
* And later query the resource:
*
* SharedFoo foo = FooNameManager::the()->query( "nameOfFoo" );
*
*
* Resources for which a Creator exists (especially such with can update themself)
* can also be managed by the FileManager / MultiFileManager!
*/
#include <ACGL/ACGL.hh>
#include <ACGL/Base/Singleton.hh>
#include <ACGL/Resource/BasicCreateInterface.hh>
......@@ -17,30 +40,42 @@
namespace ACGL{
namespace Resource{
//! NameManager templte, create your own version with a typedef (see above).
template<typename RESOURCE>
class NameManager : public Base::Singleton< NameManager<RESOURCE> >
{
friend class Base::Singleton< NameManager<RESOURCE> >;
public:
//! for the resource type, a shared-pointer based version gets defined:
typedef ptr::shared_ptr<RESOURCE> SharedResource;
private:
//! better readyble name for the map:
typedef std::map<std::string, SharedResource> ResourceMap;
typedef ResourceMap ResourceContainer;
public:
virtual ~NameManager(void) {}
virtual ~NameManager() {}
SharedResource get(const std::string& _key, BasicCreateInterface<RESOURCE>& _controller);
//! returns a shared pointer to the resource if it's stored in the map, NULL otherwise!
SharedResource query(const std::string& _key);
//! test the existance of a resource:
bool exists(const std::string& _key);
//! delete one specific resource:
bool deleteResource(const std::string& key);
void clearAllResources(void);
//! delete all resources:
void clearAllResources();
//! add one resource, if one is already presend with that name, replace it:
void add( const std::string& _key, SharedResource _resource );
typename ResourceMap::const_iterator begin(void) const { return mResourceMap.begin(); }
typename ResourceMap::const_iterator end(void) const { return mResourceMap.end(); }
typename ResourceMap::const_iterator begin() const { return mResourceMap.begin(); }
typename ResourceMap::const_iterator end() const { return mResourceMap.end(); }
protected:
NameManager(void)
NameManager()
: mResourceMap()
{}
private:
......@@ -50,27 +85,6 @@ private:
ResourceMap mResourceMap;
};
template<typename RESOURCE>
typename NameManager<RESOURCE>::SharedResource NameManager<RESOURCE>::get(const std::string& _key, BasicCreateInterface<RESOURCE>& _controller)
{
typename ResourceMap::iterator existingResource = mResourceMap.find(_key);
if(existingResource != mResourceMap.end())
return existingResource->second;
SharedResource pResource = _controller.create();
if(pResource)
{
mResourceMap[_key] = pResource;
//Utils::debug() << "NameManager::getResource: Resource loaded: " << _key << std::endl;
return pResource;
}
else
{
Utils::warning() << "NameManager::getResource: Resource could not be loaded: " << _key << std::endl;
}
return SharedResource();
}
template<typename RESOURCE>
typename NameManager<RESOURCE>::SharedResource NameManager<RESOURCE>::query(const std::string& _key)
{
......@@ -104,7 +118,7 @@ bool NameManager<RESOURCE>::deleteResource(const std::string& _key)
}
template<typename RESOURCE>
void NameManager<RESOURCE>::clearAllResources(void)
void NameManager<RESOURCE>::clearAllResources()
{
mResourceMap.clear();
}
......
......@@ -33,6 +33,7 @@ public:
SingleFileBasedCreator(const std::string& _filename, const std::string& _basePath = "")
: mFilename(_filename),
mFullFilePath(_basePath + _filename),
mResourceName(_filename),
mFileModificationTime()
{}
virtual ~SingleFileBasedCreator(){}
......@@ -41,10 +42,16 @@ public:
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
public:
virtual std::string getResourceName(void) const { return getFilename(); }
virtual std::string getResourceName(void) const { return mResourceName; }
const std::string& getFilename(void) const { return mFilename; }
const std::string& getFullFilePath(void) const { return mFullFilePath; }
//
// Modifying the resource name:
//
SingleFileBasedCreator& setResourceName(const std::string &_resourceName) { mResourceName = _resourceName; return *this; }
protected:
//! returns true if the file has not changed (based on the modification time)
inline bool fileIsUpToDate(void) { return Utils::FileHelpers::getFileModificationTime(mFullFilePath) == mFileModificationTime; }
......@@ -56,6 +63,7 @@ protected:
protected:
std::string mFilename;
std::string mFullFilePath;
std::string mResourceName;
Utils::FileHelpers::FileModificationTime mFileModificationTime;
};
......
......@@ -11,7 +11,7 @@
using namespace ACGL::Utils;
using namespace ACGL::OpenGL;
SharedShader ShaderCreator::create(void)
SharedShader ShaderCreator::create()
{
updateFileModificationTime();
......
......@@ -36,7 +36,7 @@ ShaderProgramCreator& ShaderProgramCreator::autoFiles(const std::string &_fileNa
return *this;
}
SharedShaderProgram ShaderProgramCreator::create(void)
SharedShaderProgram ShaderProgramCreator::create()
{
SharedShaderProgram shaderProgram(new ShaderProgram());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment