X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fmodulecache.cpp;fp=source%2Fglsl%2Fmodulecache.cpp;h=116eb20a288e977b1a5353576a5ed71f4fd6a023;hb=f901fcf41d8ca544085f448227f84bc6f966660d;hp=0000000000000000000000000000000000000000;hpb=e484dab089e86e889f007ad362f72337542015a0;p=libs%2Fgl.git diff --git a/source/glsl/modulecache.cpp b/source/glsl/modulecache.cpp new file mode 100644 index 00000000..116eb20a --- /dev/null +++ b/source/glsl/modulecache.cpp @@ -0,0 +1,73 @@ +#include +#include "modulecache.h" +#include "parser.h" +#include "resources.h" + +using namespace std; + +namespace Msp { +namespace GL { +namespace SL { + +ModuleCache::ModuleCache(DataFile::Collection *r): + resources(r), + next_source(1) +{ } + +ModuleCache::ModuleCache(const ModuleCache &other) +{ + for(map::const_iterator i=other.modules.begin(); i!=other.modules.end(); ++i) + modules[i->first] = new Module(*i->second); +} + +ModuleCache &ModuleCache::operator=(const ModuleCache &other) +{ + for(map::iterator i=modules.begin(); i!=modules.end(); ++i) + delete i->second; + modules.clear(); + for(map::const_iterator i=other.modules.begin(); i!=other.modules.end(); ++i) + modules[i->first] = new Module(*i->second); + return *this; +} + +ModuleCache::~ModuleCache() +{ + for(map::iterator i=modules.begin(); i!=modules.end(); ++i) + delete i->second; +} + +const Module &ModuleCache::add_module(const string &source, const string &src_name) +{ + RefPtr module = new Module; + Parser parser(this); + unsigned src_index = next_source++; + parser.parse(*module, source, src_name, src_index); + return *(modules[src_name] = module.release()); +} + +const Module &ModuleCache::add_module(IO::Base &io, const string &src_name) +{ + RefPtr module = new Module; + Parser parser(this); + unsigned src_index = next_source++; + parser.parse(*module, io, src_name, src_index); + return *(modules[src_name] = module.release()); +} + +const Module &ModuleCache::get_module(const string &name) +{ + string fn = name+".glsl"; + map::const_iterator i = modules.find(fn); + if(i!=modules.end()) + return *i->second; + + RefPtr io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn)); + if(!io) + throw runtime_error(format("module %s not found", name)); + + return add_module(*io, fn); +} + +} // namespace SL +} // namespace GL +} // namespace Msp