SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ModuleStateInterface.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9  License for the specific language governing rights and limitations under
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// @todo Documentation Dataflow/Network/ModuleStateInterface.h
30 
31 #ifndef DATAFLOW_NETWORK_MODULE_STATE_INTERFACE_H
32 #define DATAFLOW_NETWORK_MODULE_STATE_INTERFACE_H
33 
34 #include <string>
35 #include <iostream>
36 #include <boost/signals2/signal.hpp>
37 #include <boost/optional.hpp>
38 #include <boost/any.hpp>
39 #include <boost/atomic.hpp>
42 #include <Dataflow/Network/share.h>
43 
44 namespace SCIRun {
45 namespace Dataflow {
46 namespace Networks {
47 
49  {
50  public:
51  virtual ~ModuleStateInterface();
52 
53  typedef std::vector<SCIRun::Core::Algorithms::AlgorithmParameterName> Keys;
56 
57  //serialized state
58  virtual const Value getValue(const Name& name) const = 0;
59  virtual void setValue(const Name& name, const SCIRun::Core::Algorithms::AlgorithmParameter::Value& value) = 0;
60  virtual bool containsKey(const Name& name) const = 0;
61  virtual Keys getKeys() const = 0;
62  virtual ModuleStateHandle clone() const = 0;
63 
64  //non-serialized state: algorithm output needing to be pushed, for instance--TODO: make classes instead of raw string/any
65  typedef boost::any TransientValue;
66  typedef boost::optional<TransientValue> TransientValueOption;
67  virtual TransientValueOption getTransientValue(const std::string& name) const = 0;
68  virtual void setTransientValue(const std::string& name, const TransientValue& value, bool fireSignal) = 0;
69  virtual void fireTransientStateChangeSignal() = 0;
70 
71  typedef boost::signals2::signal<void()> state_changed_sig_t;
72 
73  virtual boost::signals2::connection connect_state_changed(state_changed_sig_t::slot_function_type subscriber) = 0;
74  };
75 
77  {
78  public:
79  virtual ~ModuleStateInterfaceFactory();
80  virtual ModuleStateInterface* make_state(const std::string& name) const = 0;
81  };
82 
83  /// @todo split
84  template <class T>
85  T any_cast_or_default_(const boost::any& x)
86  {
87  try
88  {
89  return boost::any_cast<T>(x);
90  }
91  catch (boost::bad_any_cast&)
92  {
93  /// @todo: use real logger here
94  //std::cout << "Attempted any_cast failed, returning default value." << std::endl;
95  return T();
96  }
97  }
98 
99  template <class T>
100  T optional_any_cast_or_default(const boost::optional<boost::any>& x)
101  {
102  return x ? any_cast_or_default_<T>(*x) : T();
103  }
104 
106  {
107  public:
109  void initStateObserver(ModuleStateInterface* state);
110  void stateChanged();
111  void resetStateChanged();
112  bool newStatePresent() const;
113  private:
114  boost::atomic<bool> stateChanged_;
115  };
116 
117 }}}
118 
119 #endif
Definition: AlgorithmBase.h:52
Definition: ModuleStateInterface.h:105
boost::variant< int, double, std::string, bool, AlgoOption, std::vector< Variable > > Value
Definition: AlgorithmBase.h:99
boost::shared_ptr< ModuleStateInterface > ModuleStateHandle
Definition: NetworkFwd.h:75
#define SCISHARE
Definition: share.h:39
SCIRun::Core::Algorithms::AlgorithmParameter Value
Definition: ModuleStateInterface.h:55
const char * name[]
Definition: BoostGraphExampleTests.cc:87
boost::any TransientValue
Definition: ModuleStateInterface.h:65
T optional_any_cast_or_default(const boost::optional< boost::any > &x)
Definition: ModuleStateInterface.h:100
boost::signals2::signal< void()> state_changed_sig_t
Definition: ModuleStateInterface.h:71
std::vector< SCIRun::Core::Algorithms::AlgorithmParameterName > Keys
Definition: ModuleStateInterface.h:53
Definition: ModuleStateInterface.h:76
Definition: ModuleStateInterface.h:48
Definition: AlgorithmBase.h:88
T any_cast_or_default_(const boost::any &x)
Definition: ModuleStateInterface.h:85
boost::optional< TransientValue > TransientValueOption
Definition: ModuleStateInterface.h:66
SCIRun::Core::Algorithms::AlgorithmParameterName Name
Definition: ModuleStateInterface.h:54