00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00038 #ifndef _LOOPCONTROL_IMPL_H
00039 #define _LOOPCONTROL_IMPL_H
00040
00041 #include <simthetic/loopcontrol.h>
00042 #include <simthetic/exceptions.h>
00043
00044 namespace simth
00045 {
00046 template<class T>
00047 LinearLoopControl<T>::~LinearLoopControl() {}
00048
00049 template<class T>
00050 LinearLoopControl<T>::LinearLoopControl(const std::string& namePar, T start, T end, T increment)
00051 : name_(namePar), start_(start), end_(end), incr(increment), value_(start)
00052 {
00053 if(incr == T(0)) {
00054 throw std::runtime_error("Increment value of LinerLoopControl "+name()+" is zero!");
00055 }
00056 if((value_ < end_) && (incr < 0)) {
00057 throw std::runtime_error("LinearLoopControl "+name()+" covers improper range");
00058 }
00059 if((value_ > end_) && (incr > 0)) {
00060 throw std::runtime_error("LinearLoopControl "+name()+" covers improper range");
00061 }
00062 }
00063
00064 template<class T>
00065 inline std::string LinearLoopControl<T>::currentIteration() const
00066 {
00067 std::ostringstream os;
00068 os<<value_;
00069 return os.str();
00070 }
00071
00072 template<class T>
00073 inline std::string LinearLoopControl<T>::lastIteration() const
00074 {
00075 std::ostringstream os;
00076 os<<end_;
00077 return os.str();
00078 }
00079
00080
00081 template<class T>
00082 inline bool LinearLoopControl<T>::nextIteration()
00083 {
00084 value_ += incr;
00085 if(!reachedEnd()) {
00086 for(unsigned int i=0;i<interfaces.size();++i) {
00087 interfaces[i]->callFunc(value_);
00088 }
00089 return true;
00090 }
00091 else {
00092 return false;
00093 }
00094 }
00095
00096
00097
00098 template<class T>
00099 inline void LinearLoopControl<T>::connectControlInterface(ControlInterface* interface)
00100 {
00101 ControlInterfaceT<T>* interPtr = dynamic_cast<ControlInterfaceT<T> * >(interface);
00102 if(interPtr == NULL)
00103 {
00104 throw simth::type_mismatch_connection("control interface and loop_control have different types");
00105 }
00106 interPtr->callFunc(value_);
00107 interfaces.push_back(interPtr);
00108 }
00109
00110
00111
00112 template<class T>
00113 inline bool LinearLoopControl<T>::reachedEnd()
00114 {
00115
00116 return (incr > T(0)) ?
00117
00118 (value_ > end_) :
00119
00120 (value_ <= end_);
00121 }
00122
00123 template<class T>
00124 inline void LinearLoopControl<T>::reset()
00125 {
00126 value_ = start_;
00127 for(unsigned int i=0;i<interfaces.size();++i) {
00128 interfaces[i]->callFunc(value_);
00129 }
00130 }
00131
00132
00133 }
00134
00135 #endif // _LOOPCONTROL_IMPL_H
00136