Libbarrett  1.2.4
tests/systems/exposed_io_system.h
00001 /*
00002  * ExposedIOSystem.h
00003  *
00004  *  Created on: Sep 18, 2009
00005  *      Author: dc
00006  */
00007 
00008 #ifndef EXPOSEDIOSYSTEM_H_
00009 #define EXPOSEDIOSYSTEM_H_
00010 
00011 
00012 #include <barrett/detail/ca_macro.h>
00013 #include <barrett/systems/manual_execution_manager.h>
00014 #include <barrett/systems/abstract/single_io.h>
00015 
00016 
00017 template<typename T>
00018 class ExposedIOSystem : public barrett::systems::SingleIO<T, T> {
00019 public:
00020         mutable bool operateCalled;
00021         mutable bool executionManagerChanged;
00022 
00023         ExposedIOSystem(const std::string& sysName = "ExposedIOSystem") :
00024                 barrett::systems::SingleIO<T, T>(sysName),
00025                 operateCalled(false), executionManagerChanged(false), data() {}
00026         virtual ~ExposedIOSystem() { this->mandatoryCleanUp(); }
00027 
00028         const T& getInputValue() const {
00029                 return this->input.getValue();
00030         }
00031 
00032         bool inputValueDefined() const {
00033                 return this->input.valueDefined();
00034         }
00035 
00036         void setOutputValue(const T& value) {
00037                 data = value;
00038                 this->outputValue->setData(&data);
00039         }
00040 
00041         void setOutputValueUndefined() {
00042                 this->outputValue->setUndefined();
00043         }
00044 
00045         void delegateOutputValueTo(barrett::systems::System::Output<T>& delegate) {
00046                 this->outputValue->delegateTo(delegate);
00047         }
00048 
00049         void undelegate() {
00050                 this->outputValue->undelegate();
00051         }
00052 
00053 protected:
00054         // This System has no invalid Input state.
00055         virtual bool inputsValid() {
00056                 return true;
00057         }
00058 
00059         virtual void operate() {
00060                 operateCalled = true;
00061         }
00062 
00063         // This System's Outputs are not a function of its Inputs.
00064         virtual void invalidateOutputs() {
00065                 /* do nothing */
00066         }
00067 
00068         virtual void onExecutionManagerChanged() {
00069                 // First, call super
00070                 barrett::systems::SingleIO<T, T>::onExecutionManagerChanged();
00071 
00072                 executionManagerChanged = true;
00073         }
00074 
00075         T data;
00076 
00077 private:
00078         DISALLOW_COPY_AND_ASSIGN(ExposedIOSystem);
00079 };
00080 
00081 
00082 template<typename T>
00083 void checkConnected(barrett::systems::ManualExecutionManager& mem,
00084                                         ExposedIOSystem<T>* outSys,
00085                                         const ExposedIOSystem<T>& inSys,
00086                                         const T& value)
00087 {
00088         EXPECT_TRUE(inSys.input.isConnected());
00089         EXPECT_TRUE((outSys->output.isConnected()));
00090 
00091         outSys->operateCalled = false;
00092         outSys->setOutputValue(value);
00093         mem.runExecutionCycle();
00094 
00095         EXPECT_TRUE(inSys.inputValueDefined()) << "input value undefined";
00096         EXPECT_TRUE(outSys->operateCalled) << "System.operate() wasn't called";
00097         EXPECT_EQ(value, inSys.getInputValue()) << "input has the wrong value";
00098 }
00099 
00100 template<typename T>
00101 void checkNotConnected(barrett::systems::ManualExecutionManager& mem,
00102                                            ExposedIOSystem<T>* outSys,
00103                                            const ExposedIOSystem<T>& inSys,
00104                                            const T& value)
00105 {
00106         outSys->operateCalled = false;
00107         outSys->setOutputValue(value);
00108         mem.runExecutionCycle();
00109 
00110         EXPECT_FALSE(outSys->operateCalled) << "System.operate() was called";
00111 }
00112 
00113 template<typename T>
00114 void checkDisconnected(const ExposedIOSystem<T>& inSys)
00115 {
00116         EXPECT_FALSE(inSys.inputValueDefined()) << "input value defined";
00117         EXPECT_FALSE(inSys.input.isConnected()) << "input thinks it has an output";
00118 }
00119 
00120 
00121 
00122 #endif /* EXPOSEDIOSYSTEM_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines