]> git.sesse.net Git - casparcg/blobdiff - core/producer/binding.h
[scene] Fixed double evaluation of expressions
[casparcg] / core / producer / binding.h
index 6912efe2d4529e367f7f87bbf0c54ea063955df9..17c9cff199a40a306d3125b24d6bbd67d44acab7 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdexcept>
 
 #include <boost/lexical_cast.hpp>
+#include <boost/utility/value_init.hpp>
 
 #include <common/tweener.h>
 #include <common/except.h>
@@ -69,9 +70,9 @@ struct impl_base : std::enable_shared_from_this<impl_base>
                {
                        if (dependency == other)
                                return true;
-                       
+
                        if (dependency->depends_on(other))
-                               return true;            
+                               return true;
                }
 
                return false;
@@ -99,7 +100,7 @@ private:
                mutable bool            evaluated_              = false;
 
                impl()
-                       : value_()
+                       : value_(boost::value_initialized<T>())
                {
                }
 
@@ -110,7 +111,8 @@ private:
 
                template<typename Expr>
                impl(const Expr& expression)
-                       : expression_(expression)
+                       : value_(boost::value_initialized<T>())
+                       , expression_(expression)
                {
                }
 
@@ -142,18 +144,20 @@ private:
 
                void evaluate() const override
                {
-                       if (expression_)
+                       if (bound())
                        {
                                auto new_value = expression_();
 
+                               evaluated_ = true;
+
                                if (new_value != value_)
                                {
                                        value_ = new_value;
                                        on_change();
                                }
                        }
-
-                       evaluated_ = true;
+                       else
+                               evaluated_ = true;
                }
 
                using impl_base::on_change;
@@ -177,12 +181,12 @@ private:
                        unbind();
                        depend_on(other);
                        expression_ = [other]{ return other->get(); };
-                       //evaluate();
+                       evaluate();
                }
 
                void unbind()
                {
-                       if (expression_)
+                       if (bound())
                        {
                                expression_ = std::function<T ()>();
                                dependencies_.clear();
@@ -545,7 +549,7 @@ public:
                std::shared_ptr<void> subscription(new char);
 
                on_change(subscription, listener);
-               
+
                return subscription;
        }
 private:
@@ -600,7 +604,7 @@ public:
 
                binding<T> result([condition, true_result, false_result]()
                {
-                       return condition.get() ? true_result.get() : false_result.get();                
+                       return condition.get() ? true_result.get() : false_result.get();
                });
 
                result.depend_on(condition);
@@ -647,7 +651,7 @@ binding<T> add_tween(
                const std::wstring& easing)
 {
        tweener tween(easing);
-       
+
        double start_val = to_tween.as<double>().get();
        double destination_val = static_cast<double>(destination_value);
        double start_time = counter.as<double>().get();