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 <boost/multi_index_container.hpp>
28 #include <boost/multi_index/member.hpp>
29 #include <boost/multi_index/ordered_index.hpp>
31 #include <simgear/props/props.hxx>
32 #include <simgear/structure/exception.hxx>
33 #include <simgear/structure/SGSharedPtr.hxx>
34 #include <simgear/structure/Singleton.hxx>
37 * Support classes for parsing effects.
46 * Builder that returns an object, probably an OSG object.
49 class EffectBuilder : public SGReferenced
52 virtual ~EffectBuilder() {}
53 virtual T* build(Effect* effect, const SGPropertyNode*,
54 const osgDB::ReaderWriter::Options* options) = 0;
55 static T* buildFromType(Effect* effect, const std::string& type,
56 const SGPropertyNode*props,
57 const osgDB::ReaderWriter::Options* options)
59 BuilderMap& builderMap = getMap();
60 typename BuilderMap::iterator iter = builderMap.find(type);
61 if (iter != builderMap.end())
62 return iter->second->build(effect, props, options);
67 friend struct Registrar;
70 Registrar(const std::string& type, EffectBuilder* builder)
72 getMap().insert(std::make_pair(type, builder));
76 typedef std::map<std::string, SGSharedPtr<EffectBuilder> > BuilderMap;
77 struct BuilderMapSingleton : public simgear::Singleton<BuilderMapSingleton>
81 static BuilderMap& getMap()
83 return BuilderMapSingleton::instance()->_map;
87 // Tables of strings and constants. We want to reconstruct the effect
88 // property tree from OSG state sets, so the tables should be bi-directional.
90 // two-way map for building StateSets from property descriptions, and
91 // vice versa. Mostly copied from the boost documentation.
95 using boost::multi_index_container;
96 using namespace boost::multi_index;
98 // tags for accessing both sides of a bidirectional map
103 template <typename T>
104 struct EffectNameValue
106 // Don't use std::pair because we want to use aggregate intialization.
111 // The class template bidirectional_map wraps the specification
112 // of a bidirectional map based on multi_index_container.
114 template<typename FromType,typename ToType>
115 struct bidirectional_map
117 typedef std::pair<FromType,ToType> value_type;
119 /* A bidirectional map can be simulated as a multi_index_container
120 * of pairs of (FromType,ToType) with two unique indices, one
121 * for each member of the pair.
124 typedef multi_index_container<
128 tag<from>, member<value_type, FromType, &value_type::first> >,
130 tag<to>, member<value_type, ToType, &value_type::second> >
136 struct EffectPropertyMap
138 typedef typename bidirectional_map<std::string, T>::type BMap;
141 EffectPropertyMap(const EffectNameValue<T> (&attrs)[N]);
146 EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
148 for (int i = 0; i < N; ++i)
149 _map.insert(typename BMap::value_type(attrs[i].first, attrs[i].second));
155 bool findAttr(const effect::EffectPropertyMap<T>& pMap,
156 const SGPropertyNode* prop,
159 using namespace effect;
162 const char* name = prop->getStringValue();
165 typename EffectPropertyMap<T>::BMap::iterator itr
166 = pMap._map.get<from>().find(name);
167 if (itr == pMap._map.end()) {
170 result = itr->second;
176 std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
178 using namespace effect;
180 typename EffectPropertyMap<T>::BMap::template index_iterator<to>::type itr
181 = pMap._map.get<to>().find(value);
182 if (itr != pMap._map.get<to>().end())
188 std::string findName(const effect::EffectPropertyMap<T>& pMap, GLenum value)
190 return findName(pMap, static_cast<T>(value));
194 * Given a property node from a pass, get its value either from it or
195 * from the effect parameters.
198 const SGPropertyNode* getEffectPropertyNode(Effect* effect,
199 const SGPropertyNode* prop);
201 * Get a named child property from pass parameters or effect
204 const SGPropertyNode* getEffectPropertyChild(Effect* effect,
205 const SGPropertyNode* prop,
208 class BuilderException : public sg_exception
212 BuilderException(const char* message, const char* origin = 0);
213 BuilderException(const std::string& message, const std::string& = "");
214 virtual ~BuilderException() throw();
217 class PassAttributeBuilder : public SGReferenced
220 typedef std::map<const std::string, SGSharedPtr<PassAttributeBuilder> >
223 struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
225 PassAttrMap passAttrMap;
228 virtual void buildAttribute(Effect* effect, Pass* pass,
229 const SGPropertyNode* prop,
230 const osgDB::ReaderWriter::Options* options)
232 static PassAttributeBuilder* find(const std::string& str)
234 PassAttrMap::iterator itr
235 = PassAttrMapSingleton::instance()->passAttrMap.find(str);
236 if (itr == PassAttrMapSingleton::instance()->passAttrMap.end())
239 return itr->second.ptr();
241 template<typename T> friend class InstallAttributeBuilder;
245 struct InstallAttributeBuilder
247 InstallAttributeBuilder(const string& name)
249 PassAttributeBuilder::PassAttrMapSingleton::instance()
250 ->passAttrMap.insert(make_pair(name, new T));
254 // The description of an attribute may exist in a pass' XML, but a
255 // derived effect might want to disable the attribute altogether. So,
256 // some attributes have an "active" property; if it exists and is
257 // false, the OSG attribute is not built at all. This is different
258 // from any OSG mode settings that might be around.
259 bool isAttributeActive(Effect* effect, const SGPropertyNode* prop);