00001
00002
00003
00004
00005
00006
00007
00008 #ifndef VARIABLEARRAYBASEDMODIFIER_H_
00009 #define VARIABLEARRAYBASEDMODIFIER_H_
00010
00011 #include "EvSimObject.h"
00012 #include "EvSimNetwork.h"
00013 #include "SharedArray.h"
00014
00015 #include <stdexcept>
00016
00017 #ifndef SWIG
00018
00019 #include <iostream>
00020 using std::cerr; using std::endl;
00021
00022 #endif
00023
00024
00025
00026
00027 template<typename T>
00028 class VariableArrayBasedModifier : public EvSimObject {
00029
00030 public:
00031
00032 VariableArrayBasedModifier() : values_arr(0), is_cyclic(false)
00033 {}
00034
00035
00036 VariableArrayBasedModifier(const SharedArray<T> & values, bool cyclic=false)
00037 : values_arr(values.get()), is_cyclic(cyclic)
00038 {}
00039
00040 virtual ~VariableArrayBasedModifier() {};
00041
00042 void setValuesArray(const SharedArray<T> & values) {
00043 values_arr = values.get();
00044 }
00045
00046 virtual void eventHit(SimContext & sim_info, Time time, port_id_t port) {
00047 if (port == 1) {
00048 curr_idx=0;
00049 }
00050 if (port == 0 || port == 1) {
00051 (*mod_var_ptr) = (*values_arr)[curr_idx];
00052 if (curr_idx < values_arr->size() - 1)
00053 curr_idx++;
00054 else if (is_cyclic)
00055 curr_idx=0;
00056 }
00057 }
00058
00059 virtual void reset(SimContext &sim_ctxt)
00060 {
00061 curr_idx = 0;
00062 }
00063
00064 virtual unsigned numInputPorts() const
00065 {
00066 return 2;
00067 };
00068
00069 void setModifiedVariable(EvSimNetwork &net, EvSimObject::ID self, EvSimObject::ID obj_id, const string &var_name, update_id_t upd_id = -1) {
00070 EvSimObject *obj = net.getObject(obj_id);
00071 std::cout << "I am here!" << std::endl;
00072 setModifiedVariable(obj, var_name);
00073 if (upd_id >= 0)
00074 net.causalUpdateLink(self, obj_id, upd_id);
00075 }
00076
00077 void setModifiedVariable(EvSimObject *obj, const string &var_name) {
00078 mod_var_ptr = (T *)obj->getVariablePtr(var_name);
00079 if (!mod_var_ptr)
00080 {
00081 throw( std::invalid_argument(std::string("VariableArrayBasedModifier::setModifiedVariable: Object has no variable named '")+var_name+"'." ) );
00082 }
00083 if (obj->getVariableType(var_name) != typeid(T))
00084 {
00085 throw( std::invalid_argument(std::string("VariableArrayBasedModifier::setModifiedVariable: Modified variable and modifier must be of same type. Source type: ")+
00086 typeid(T).name()+" destination type ('"+var_name+"'): "+obj->getVariableType(var_name).name()+"." ) );
00087 }
00088 }
00089
00090 void setCyclic(bool cyclic) {
00091 is_cyclic = cyclic;
00092 }
00093
00094 protected:
00095 T *mod_var_ptr;
00096 unsigned curr_idx;
00097 const vector<T> * values_arr;
00098 bool is_cyclic;
00099 };
00100
00101 #endif