diff --git a/include/trick/MonteCarlo.hh b/include/trick/MonteCarlo.hh index 87e6d167..c74aaf41 100644 --- a/include/trick/MonteCarlo.hh +++ b/include/trick/MonteCarlo.hh @@ -14,6 +14,11 @@ #include "trick/RemoteShell.hh" #include "trick/tc.h" +#ifdef SWIG +// This instructs SWIG to use dynamic_cast and return one of the derived type for the get_variable function. +%factory(Trick::MonteVar * Trick::MonteCarlo::get_variable, Trick::MonteVarCalculated, Trick::MonteVarFile, Trick::MonteVarFixed, Trick::MonteVarRandom) ; +#endif + namespace Trick { /** @@ -622,6 +627,13 @@ namespace Trick { */ void add_variable(Trick::MonteVar *variable); + /** + * Gets the specified variable. + * + * @param variable name to get + */ + Trick::MonteVar * get_variable(std::string variable_name); + /** * Adds a new slave with the specified machine name. * diff --git a/include/trick/MonteVarCalculated.hh b/include/trick/MonteVarCalculated.hh index fb7ceb90..93ea07d8 100644 --- a/include/trick/MonteVarCalculated.hh +++ b/include/trick/MonteVarCalculated.hh @@ -13,6 +13,20 @@ #include "trick/MonteVar.hh" #include "trick/reference.h" +// This block of code disowns the pointer on the python side so you can reassign +// python variables without freeing the C++ class underneath +#ifdef SWIG +%feature("compactdefaultargs","0") ; +%feature("shadow") Trick::MonteVarCalculated::MonteVarCalculated(std::string name) %{ + def __init__(self, *args): + this = $action(*args) + try: self.this.append(this) + except: self.this = this + this.own(0) + self.this.own(0) +%} +#endif + namespace Trick { /** diff --git a/include/trick/MonteVarFile.hh b/include/trick/MonteVarFile.hh index db07638c..444b1ff8 100644 --- a/include/trick/MonteVarFile.hh +++ b/include/trick/MonteVarFile.hh @@ -14,6 +14,20 @@ #include "trick/MonteVar.hh" +// This block of code disowns the pointer on the python side so you can reassign +// python variables without freeing the C++ class underneath +#ifdef SWIG +%feature("compactdefaultargs","0") ; +%feature("shadow") Trick::MonteVarFile::MonteVarFile(std::string name, std::string file_name, unsigned int column) %{ + def __init__(self, *args): + this = $action(*args) + try: self.this.append(this) + except: self.this = this + this.own(0) + self.this.own(0) +%} +#endif + namespace Trick { /** diff --git a/include/trick/MonteVarFixed.hh b/include/trick/MonteVarFixed.hh index d9a1f089..a8857adb 100644 --- a/include/trick/MonteVarFixed.hh +++ b/include/trick/MonteVarFixed.hh @@ -12,6 +12,20 @@ #include "trick/MonteVar.hh" +// This block of code disowns the pointer on the python side so you can reassign +// python variables without freeing the C++ class underneath +#ifdef SWIG +%feature("compactdefaultargs","0") ; +%feature("shadow") Trick::MonteVarFixed::MonteVarFixed(std::string name, double value) %{ + def __init__(self, *args): + this = $action(*args) + try: self.this.append(this) + except: self.this = this + this.own(0) + self.this.own(0) +%} +#endif + namespace Trick { /** diff --git a/include/trick/MonteVarRandom.hh b/include/trick/MonteVarRandom.hh index 42844a75..87fb4995 100644 --- a/include/trick/MonteVarRandom.hh +++ b/include/trick/MonteVarRandom.hh @@ -14,6 +14,20 @@ #include "trick/rand_generator.h" #include "trick/StlRandomGenerator.hh" +// This block of code disowns the pointer on the python side so you can reassign +// python variables without freeing the C++ class underneath +#ifdef SWIG +%feature("compactdefaultargs","0") ; +%feature("shadow") Trick::MonteVarRandom::MonteVarRandom(std::string name, Distribution distribution) %{ + def __init__(self, *args): + this = $action(*args) + try: self.this.append(this) + except: self.this = this + this.own(0) + self.this.own(0) +%} +#endif + namespace Trick { /** diff --git a/include/trick/swig/trick_swig.i b/include/trick/swig/trick_swig.i index 96746cd4..97901753 100644 --- a/include/trick/swig/trick_swig.i +++ b/include/trick/swig/trick_swig.i @@ -4,6 +4,7 @@ %include "std_map.i" %include "std_string.i" %include "std_vector.i" +%include "factory.i" %include "trick/swig/swig_extend_str.i" %include "trick/swig/swig_class_typedef.i" diff --git a/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp b/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp index 136e9358..9be07160 100644 --- a/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp +++ b/trick_source/sim_services/MonteCarlo/MonteCarlo_funcs.cpp @@ -153,9 +153,31 @@ void Trick::MonteCarlo::get_ranges(std::vector &ranges) { } void Trick::MonteCarlo::add_variable(Trick::MonteVar *variable) { + for (std::vector::const_iterator i = variables.begin(); i != variables.end(); ++i) { + if ( (*i)->name.compare(variable->name) == 0 ) { + message_publish(MSG_WARNING, "Monte WARNING: Cannot add new MonteVar \"%s\", variable of that name already exists.\n", + variable->name.c_str() ); + return; + } + } variables.push_back(variable); } +/** + * @par Detailed Design: + * Get a pointer to a MonteVar by name. Note this is used in conjunction with + * %factory so that swig produces methods returning all derived types + */ +Trick::MonteVar * Trick::MonteCarlo::get_variable(std::string variable_name) { + + for (std::vector::const_iterator i = variables.begin(); i != variables.end(); ++i) { + if ( (*i) and (*i)->name.compare(variable_name) == 0 ) { + return (*i); + } + } + return (NULL); +} + void Trick::MonteCarlo::add_slave(std::string in_machine_name) { add_slave(new MonteSlave(in_machine_name)); }