]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/EffectBuilder.hxx
Partial fix for crash in SGPropertyNode::fireValueChanged
[simgear.git] / simgear / scene / material / EffectBuilder.hxx
index c1d48405b04e1433c3c5c8cf25b84dfe1971e14b..52e2cf1c98614a2e0c4dee3ea65ff3bf679d7584 100644 (file)
@@ -23,6 +23,7 @@
 #include <string>
 #include <cstring>
 
+#include <osg/Object>
 #include <osgDB/Registry>
 
 #include <boost/bind.hpp>
 #include <boost/multi_index/member.hpp>
 #include <boost/multi_index/ordered_index.hpp>
 
-#include <simgear/math/SGMath.hxx>
 #include <simgear/props/AtomicChangeListener.hxx>
 #include <simgear/props/props.hxx>
-#include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
+#include <simgear/scene/util/SGReaderWriterOptions.hxx>
 #include <simgear/structure/exception.hxx>
 #include <simgear/structure/SGSharedPtr.hxx>
 #include <simgear/structure/Singleton.hxx>
@@ -47,7 +47,7 @@ namespace simgear
 {
 class Effect;
 class Pass;
-class SGReaderWriterXMLOptions;
+class SGReaderWriterOptions;
 
 /**
  * Builder that returns an object, probably an OSG object.
@@ -57,16 +57,16 @@ class EffectBuilder : public SGReferenced
 {
 public:
     virtual ~EffectBuilder() {}
-    virtual T* build(Effect* effect, const SGPropertyNode*,
-                     const SGReaderWriterXMLOptions* options) = 0;
-    static T* buildFromType(Effect* effect, const std::string& type,
+    virtual T* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                     const SGReaderWriterOptions* options) = 0;
+    static T* buildFromType(Effect* effect, Pass* pass, const std::string& type,
                             const SGPropertyNode*props,
-                            const SGReaderWriterXMLOptions* options)
+                            const SGReaderWriterOptions* options)
     {
         BuilderMap& builderMap = getMap();
         typename BuilderMap::iterator iter = builderMap.find(type);
         if (iter != builderMap.end())
-            return iter->second->build(effect, props, options);
+            return iter->second->build(effect, pass, props, options);
         else
             return 0;
     }
@@ -167,7 +167,7 @@ EffectPropertyMap<T>::EffectPropertyMap(const EffectNameValue<T> (&attrs)[N])
 template<typename T>
 struct SimplePropertyMap
 {
-    typedef std::map<string, T> map_type;
+    typedef std::map<std::string, T> map_type;
     map_type _map;
     template<int N>
     SimplePropertyMap(const EffectNameValue<T> (&attrs)[N])
@@ -195,10 +195,10 @@ void findAttr(const effect::EffectPropertyMap<T>& pMap,
 {
     using namespace effect;
     typename EffectPropertyMap<T>::BMap::iterator itr
-        = pMap._map.get<from>().find(name);
+        = pMap._map.template get<from>().find(name);
     if (itr == pMap._map.end()) {
-        throw effect::BuilderException(string("findAttr: could not find attribute ")
-                               string(name));
+        throw effect::BuilderException(std::string("findAttr: could not find attribute ")
+                                       + std::string(name));
     } else {
         result = itr->second;
     }
@@ -233,7 +233,7 @@ const T* findAttr(const effect::EffectPropertyMap<T>& pMap,
 {
     using namespace effect;
     typename EffectPropertyMap<T>::BMap::iterator itr
-        = pMap._map.get<from>().find(name);
+        = pMap._map.template get<from>().find(name);
     if (itr == pMap._map.end())
         return 0;
     else 
@@ -267,8 +267,8 @@ std::string findName(const effect::EffectPropertyMap<T>& pMap, T value)
     using namespace effect;
     std::string result;
     typename EffectPropertyMap<T>::BMap::template index_iterator<to>::type itr
-        = pMap._map.get<to>().find(value);
-    if (itr != pMap._map.get<to>().end())
+        = pMap._map.template get<to>().find(value);
+    if (itr != pMap._map.template get<to>().end())
         result = itr->first;
     return result;
 }
@@ -295,18 +295,18 @@ const SGPropertyNode* getEffectPropertyChild(Effect* effect,
                                              const char* name);
 
 /**
- * Get the name of a node mentioned in a <use> clause from the global property
+ * Get the name of a node mentioned in a \<use\> clause from the global property
  * tree.
- * @return empty if prop doesn't contain a <use> clause; otherwise the
+ * @return empty if prop doesn't contain a \<use\> clause; otherwise the
  * mentioned node name.
  */
 std::string getGlobalProperty(const SGPropertyNode* prop,
-                              const SGReaderWriterXMLOptions *);
+                              const SGReaderWriterOptions *);
 
 template<typename NameItr>
 std::vector<std::string>
 getVectorProperties(const SGPropertyNode* prop,
-                    const SGReaderWriterXMLOptions *options, size_t vecSize,
+                    const SGReaderWriterOptions *options, size_t vecSize,
                     NameItr defaultNames)
 {
     using namespace std;
@@ -316,8 +316,8 @@ getVectorProperties(const SGPropertyNode* prop,
     PropertyList useProps = prop->getChildren("use");
     if (useProps.size() == 1) {
         string parentName = useProps[0]->getStringValue();
-        if (parentName.size() == 0 || parentName[0] != '/')
-            parentName = options->getPropRoot()->getPath() + "/" + parentName;
+        if (parentName.empty() || parentName[0] != '/')
+            parentName = options->getPropertyNode()->getPath() + "/" + parentName;
         if (parentName[parentName.size() - 1] != '/')
             parentName.append("/");
         NameItr itr = defaultNames;
@@ -331,7 +331,7 @@ getVectorProperties(const SGPropertyNode* prop,
              itr != end;
              ++itr) {
             string childName = (*itr)->getStringValue();
-            if (childName.size() == 0 || childName[0] != '/')
+            if (childName.empty() || childName[0] != '/')
                 result.push_back(parentName + childName);
             else
                 result.push_back(childName);
@@ -349,11 +349,14 @@ protected:
     struct PassAttrMapSingleton : public simgear::Singleton<PassAttrMapSingleton>
     {
         PassAttrMap passAttrMap;
+      
     };
 public:
+    virtual ~PassAttributeBuilder(); // anchor into the compilation unit.
+  
     virtual void buildAttribute(Effect* effect, Pass* pass,
                                 const SGPropertyNode* prop,
-                                const SGReaderWriterXMLOptions* options)
+                                const SGReaderWriterOptions* options)
     = 0;
     static PassAttributeBuilder* find(const std::string& str)
     {
@@ -370,7 +373,7 @@ public:
 template<typename T>
 struct InstallAttributeBuilder
 {
-    InstallAttributeBuilder(const string& name)
+    InstallAttributeBuilder(const std::string& name)
     {
         PassAttributeBuilder::PassAttrMapSingleton::instance()
             ->passAttrMap.insert(make_pair(name, new T));
@@ -493,19 +496,17 @@ make_OSGFunctor(Obj* obj, void (Obj::*const func)(const OSGParam&))
 template<typename OSGParamType, typename ObjType, typename F>
 class ScalarChangeListener
     : public SGPropertyChangeListener, public InitializeWhenAdded,
+      public PropertyPoller,
       public Effect::Updater
 {
 public:
     ScalarChangeListener(ObjType* obj, const F& setter,
                          const std::string& propName)
-        : _obj(obj), _setter(setter)
+        : _obj(obj), _setter(setter), _propName(propName)
     {
-        _propName = new std::string(propName);
     }
     virtual ~ScalarChangeListener()
     {
-        delete _propName;
-        _propName = 0;
     }
     void valueChanged(SGPropertyNode* node)
     {
@@ -513,16 +514,20 @@ public:
     }
     void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
     {
-        SGPropertyNode* listenProp = makeNode(propRoot, *_propName);
-        delete _propName;
-        _propName = 0;
-        if (listenProp)
-            listenProp->addChangeListener(this, true);
+        _listenProp = makeNode(propRoot, _propName);
+//        if ( _listenProp.valid() )
+//            _listenProp->addChangeListener(this, true);
+    }
+    void pollProperties(Effect* effect)
+    {
+        if( false == _listenProp.valid() ) return;
+        valueChanged(_listenProp);
     }
 private:
+    SGPropertyNode_ptr _listenProp;
     osg::ref_ptr<ObjType> _obj;
     F _setter;
-    std::string* _propName;
+    std::string _propName;
 };
 
 template<typename T, typename Func>
@@ -578,12 +583,25 @@ new_EEPropListener(const Func& func, const std::string* propName,
         (func, 0, namesBegin, namesEnd);
 }
 
+/**
+ * Set DYNAMIC data variance on an osg::Object.
+ */
+
+inline void setDynamicVariance(void* obj)
+{
+}
+
+inline void setDynamicVariance(osg::Object* obj)
+{
+    obj->setDataVariance(osg::Object::DYNAMIC);
+}
+
 /**
  * Initialize the value and the possible updating of an effect
  * attribute. If the value is specified directly, set it. Otherwise,
- * use the <use> tag to look at the parameters. Again, if there is a
+ * use the \<use\> tag to look at the parameters. Again, if there is a
  * value there set it directly. Otherwise, the parameter contains its
- * own <use> tag referring to a property in the global property tree;
+ * own \<use\> tag referring to a property in the global property tree;
  * install a change listener that will set the attribute when the
  * property changes.
  *
@@ -593,7 +611,7 @@ new_EEPropListener(const Func& func, const std::string* propName,
 template<typename OSGParamType, typename ObjType, typename F>
 void
 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
-                   const F& setter, const SGReaderWriterXMLOptions* options)
+                   const F& setter, const SGReaderWriterOptions* options)
 {
     const SGPropertyNode* valProp = getEffectPropertyNode(effect, prop);
     if (!valProp)
@@ -601,6 +619,7 @@ initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
     if (valProp->nChildren() == 0) {
         setter(obj, valProp->getValue<OSGParamType>());
     } else {
+        setDynamicVariance(obj);
         std::string propName = getGlobalProperty(valProp, options);
         ScalarChangeListener<OSGParamType, ObjType, F>* listener
             = new ScalarChangeListener<OSGParamType, ObjType, F>(obj, setter,
@@ -613,7 +632,7 @@ template<typename OSGParamType, typename ObjType, typename SetterReturn>
 inline void
 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
                    SetterReturn (ObjType::*setter)(const OSGParamType),
-                   const SGReaderWriterXMLOptions* options)
+                   const SGReaderWriterOptions* options)
 {
     initFromParameters<OSGParamType>(effect, prop, obj,
                                      boost::bind(setter, _1, _2), options);
@@ -624,12 +643,12 @@ initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
  * The parameter may be updated at runtime.
  *
  * If the value is specified directly, set it. Otherwise, use the
- * <use> tag to look at the parameters. Again, if there is a value
+ * \<use\> tag to look at the parameters. Again, if there is a value
  * there set it directly. Otherwise, the parameter contains one or several
- * <use> tags. If there is one tag, it is a property that is the root
+ * \<use\> tags. If there is one tag, it is a property that is the root
  * for the values needed to update the parameter; nameIter holds the
  * names of the properties relative to the root. If there are several
- * <use> tags, they each hold the name of the property holding the
+ * \<use\> tags, they each hold the name of the property holding the
  * value for the corresponding vector member.
  *
  * Install a change listener that will set the attribute when the
@@ -643,7 +662,7 @@ template<typename OSGParamType, typename ObjType, typename NameItrType,
 void
 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
                    const F& setter,
-                   NameItrType nameItr, const SGReaderWriterXMLOptions* options)
+                   NameItrType nameItr, const SGReaderWriterOptions* options)
 {
     typedef typename Bridge<OSGParamType>::sg_type sg_type;
     const int numComponents = props::NumComponents<sg_type>::num_components;
@@ -653,6 +672,7 @@ initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
     if (valProp->nChildren() == 0) { // Has <use>?
         setter(obj, Bridge<OSGParamType>::get(valProp->getValue<sg_type>()));
     } else {
+        setDynamicVariance(obj);
         std::vector<std::string> paramNames
             = getVectorProperties(valProp, options,numComponents, nameItr);
         if (paramNames.empty())
@@ -671,7 +691,7 @@ template<typename OSGParamType, typename ObjType, typename NameItrType,
 inline void
 initFromParameters(Effect* effect, const SGPropertyNode* prop, ObjType* obj,
                    SetterReturn (ObjType::*setter)(const OSGParamType&),
-                   NameItrType nameItr, const SGReaderWriterXMLOptions* options)
+                   NameItrType nameItr, const SGReaderWriterOptions* options)
 {
     initFromParameters<OSGParamType>(effect, prop, obj,
                                      boost::bind(setter, _1, _2), nameItr,