1 // Copyright (C) 2009 Tim Moore timoore@redhat.com
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 #ifndef SIMGEAR_EFFECTBUILDER_HXX
18 #define SIMGEAR_EFFECTBUILDER_HXX 1
25 #include <osgDB/Registry>
27 #include <simgear/props/props.hxx>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/structure/SGSharedPtr.hxx>
30 #include <simgear/structure/Singleton.hxx>
33 * Support classes for parsing effects.
41 * Builder that returns an object, probably an OSG object.
44 class EffectBuilder : public SGReferenced
47 virtual ~EffectBuilder() {}
48 virtual T* build(Effect* effect, const SGPropertyNode*,
49 const osgDB::ReaderWriter::Options* options) = 0;
50 static T* buildFromType(Effect* effect, const std::string& type,
51 const SGPropertyNode*props,
52 const osgDB::ReaderWriter::Options* options)
54 BuilderMap& builderMap = getMap();
55 typename BuilderMap::iterator iter = builderMap.find(type);
56 if (iter != builderMap.end())
57 return iter->second->build(effect, props, options);
62 friend struct Registrar;
65 Registrar(const std::string& type, EffectBuilder* builder)
67 getMap().insert(std::make_pair(type, builder));
71 typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
72 struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
76 static BuilderMap& getMap()
78 return BuilderMapSingleton::instance()->_map;
82 // Simple tables of strings and constants. The table intialization
83 // *must* be in alphabetical order.
85 struct EffectNameValue
87 // Don't use std::pair because we want to use aggregate intialization.
94 static bool compare(const char* lhs, const char* rhs)
96 return std::strcmp(lhs, rhs) < 0;
99 bool operator()(const EffectNameValue& lhs,
100 const EffectNameValue& rhs) const
102 return compare(lhs.first, rhs.first);
104 bool operator()(const char* lhs, const EffectNameValue& rhs) const
106 return compare(lhs, rhs.first);
108 bool operator()(const EffectNameValue& lhs, const char* rhs) const
110 return compare (lhs.first, rhs);
115 template<typename ENV, typename T, int N>
116 bool findAttr(const ENV (&attrs)[N], const SGPropertyNode* prop, T& result)
120 const char* name = prop->getStringValue();
123 std::pair<const ENV*, const ENV*> itrs
124 = std::equal_range(&attrs[0], &attrs[N], name, typename ENV::Compare());
125 if (itrs.first == itrs.second) {
128 result = itrs.first->second;
134 * Given a property node from a pass, get its value either from it or
135 * from the effect parameters.
138 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
139 const SGPropertyNode* prop);
141 * Get a named child property from pass parameters or effect
144 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
145 const SGPropertyNode* prop,
148 class BuilderException : public sg_exception
152 BuilderException(const char* message, const char* origin = 0);
153 BuilderException(const std::string& message, const std::string& = "");
154 virtual ~BuilderException() throw();