stlab.adobe.com Adobe Systems Incorporated

debounce.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /*************************************************************************************************/
00008 
00009 #ifndef ADOBE_DEBOUNCE_HPP
00010 #define ADOBE_DEBOUNCE_HPP
00011 
00012 /*************************************************************************************************/
00013 
00014 #include <adobe/config.hpp>
00015 
00016 #include <boost/function.hpp>
00017 
00018 #include <adobe/any_regular.hpp>
00019 
00020 /*************************************************************************************************/
00021 
00022 namespace adobe {
00023 
00024 /*************************************************************************************************/
00025 
00054 template <typename ModeType>    
00055 class debounce_t
00056 {
00057 public:
00058     typedef ModeType                                  model_type;
00059     typedef boost::function<void (const model_type&)> proc_type;
00060 
00061     debounce_t() :
00062         type_2_debounce_m(false),
00063         first_time_m(true)
00064     { }
00065 
00076     template <typename UnaryFunction1, typename UnaryFunction2>
00077     debounce_t(const UnaryFunction1& set_model_proc, const UnaryFunction2& monitor_model_proc) :
00078         set_model_m(set_model_proc),
00079         monitor_model_m(monitor_model_proc),
00080         type_2_debounce_m(false),
00081         first_time_m(true)
00082     { }
00083 
00090     void set_model_value(const model_type& from_controller)
00091     {
00092         assert (set_model_m);
00093 
00094         if (first_time_m)
00095             type_2_debounce_m = false;
00096 
00097         if (type_2_debounce_m)
00098         {
00099             type_2_debounce_m = false;
00100 
00101             return;
00102         }
00103 
00104         type_1_debounce_m = from_controller;
00105 
00106         set_model_m(from_controller);
00107 
00108         first_time_m = false;
00109     }
00110 
00116     void display(const model_type& new_value)
00117     {
00118         assert (monitor_model_m);
00119 
00120         if (new_value == type_1_debounce_m)
00121             return;
00122         
00123         type_1_debounce_m = new_value;
00124         type_2_debounce_m = true;
00125 
00126         monitor_model_m(new_value);
00127     }
00128 
00129     void reset_set_model_proc(const proc_type& proc)
00130     { type_1_debounce_m = model_type(); set_model_m = proc; }
00131 
00132     void reset_monitor_model_proc(const proc_type& proc)
00133     { type_2_debounce_m = false; monitor_model_m = proc; }
00134 
00135     const model_type& last() const
00136     { return type_1_debounce_m; }
00137 
00138     bool is_set_model_proc_installed() const
00139     { return set_model_m; }
00140 
00141     bool is_monitor_model_proc_installed() const
00142     { return monitor_model_m; }
00143 
00144 private:
00145     proc_type  set_model_m;
00146     proc_type  monitor_model_m;
00147     model_type type_1_debounce_m;
00148     bool       type_2_debounce_m;
00149     bool       first_time_m;
00150 };
00151 
00152 /*************************************************************************************************/
00153 
00154 } // namespace adobe
00155 
00156 /*************************************************************************************************/
00157 
00158 // ADOBE_DEBOUNCE_HPP
00159 #endif
00160 
00161 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google