From 57c76041f576d916d5300e1355e67c5829585645 Mon Sep 17 00:00:00 2001 From: Alex Lin Date: Fri, 18 Sep 2015 09:03:24 -0500 Subject: [PATCH] Get the value of a variable not parsed by SWIG in python Wrote a python routine called get_value_by_ref_name that takes a variable name string as an argument. The string is passed to ref_attributes to find the address and returns a REF2 structure. The REF2 structure is passed to ref_to_value and the type and value of the variable is returned in a structure. We call vval_ to extract the raw value from the structure and return that to python space. Made a couple of other bug fixes in the FieldVisior and cpp input file example found while investigating this issue. refs #123 --- include/trick/vval.h | 5 +--- share/trick/swig/shortcuts.py | 30 +++++++++++++++++++ .../SIM_Ball++_L1/RUN_cpp_input/input.cpp | 10 ++++++- .../Interface_Code_Gen/FieldVisitor.cpp | 3 ++ trick_source/trick_swig/sim_services.i | 1 + 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/include/trick/vval.h b/include/trick/vval.h index 1b82bf14..8fa928f0 100644 --- a/include/trick/vval.h +++ b/include/trick/vval.h @@ -20,12 +20,9 @@ float vval_float(V_DATA * V); double vval_double(V_DATA * V); char *vval_string(V_DATA * V); -#ifndef __Lynx__ +#ifndef SWIG wchar_t vval_wchar(V_DATA * V); wchar_t *vval_wstring(V_DATA * V); -#else -int vval_wchar(V_DATA * V); -int *vval_wstring(V_DATA * V); #endif FILE *vval_filep(V_DATA * V); diff --git a/share/trick/swig/shortcuts.py b/share/trick/swig/shortcuts.py index 1cb019d7..aba09940 100644 --- a/share/trick/swig/shortcuts.py +++ b/share/trick/swig/shortcuts.py @@ -100,3 +100,33 @@ if hasattr(top.cvar, 'trick_data_record'): drd_writer_thread = top.cvar.trick_data_record.drd.drd_writer_thread data_record_cpu_set = top.cvar.trick_data_record.drd.drd_writer_thread.cpu_set +# This routine will look up the reference name through ref_attributes. +# ref_to_value retrieves the value and places it into a V_DATA structure +# The return statement is an anonymous dictionary lookup that returns +# the value based on the type in val. The 6 argument is a default +# type to use +def get_value_by_ref_name(name): + ref = trick.ref_attributes(name) + ref.thisown = True + val = trick.V_DATA() + trick.ref_to_value(ref, val) + return { + '1': trick.vval_char(val), + '2': trick.vval_char(val), + '3': trick.vval_string(val), + '4': trick.vval_short(val), + '5': trick.vval_short(val), + '6': trick.vval_int(val), + '7': trick.vval_int(val), + '8': trick.vval_long(val), + '9': trick.vval_long(val), + '10': trick.vval_float(val), + '11': trick.vval_double(val), + '14': trick.vval_longlong(val), + '15': trick.vval_longlong(val), + '16': trick.vval_filep(val), + '17': trick.vval_char(val), + '20': trick.vval_voidp(val), + '21': trick.vval_int(val) + }.get(str(val.type),6) + diff --git a/trick_sims/SIM_Ball++_L1/RUN_cpp_input/input.cpp b/trick_sims/SIM_Ball++_L1/RUN_cpp_input/input.cpp index 99b55178..e43c0f8c 100644 --- a/trick_sims/SIM_Ball++_L1/RUN_cpp_input/input.cpp +++ b/trick_sims/SIM_Ball++_L1/RUN_cpp_input/input.cpp @@ -11,7 +11,9 @@ //#define protected public #include "../S_source.hh" -#include "sim_services/CheckPointRestart/include/CheckPointRestart_c_intf.hh" +#include "trick/CheckPointRestart_c_intf.hh" +#include "trick/realtimesync_proto.h" +#include "trick/external_application_c_intf.h" extern "C" int run_event_1() { message_publish(1, "event 1 fired!\n") ; @@ -81,6 +83,12 @@ extern "C" int run_me() { exec_set_terminate_time(200.0) ; +/* + real_time_enable() ; + trick_real_time.itimer.enable() ; + sim_control_panel_set_enabled(true) ; +*/ + return 0 ; } diff --git a/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp b/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp index adceb7ec..462e6ba8 100644 --- a/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp +++ b/trick_source/codegen/Interface_Code_Gen/FieldVisitor.cpp @@ -447,6 +447,9 @@ bool FieldVisitor::VisitTypedefType(clang::TypedefType *tt) { fdes->setHasType(true) ; } else if ( tt->isRecordType() ) { std::string type_name = tt->desugar().getAsString() ; + if ((pos = type_name.find("class ")) != std::string::npos ) { + type_name.erase(pos , 6) ; + } if ((pos = type_name.find("struct ")) != std::string::npos ) { type_name.erase(pos , 7) ; } diff --git a/trick_source/trick_swig/sim_services.i b/trick_source/trick_swig/sim_services.i index ae70fa64..fa480af6 100644 --- a/trick_source/trick_swig/sim_services.i +++ b/trick_source/trick_swig/sim_services.i @@ -129,6 +129,7 @@ #include "trick/trick_tests.h" #include "trick/VariableServer.hh" #include "trick/variable_server_proto.h" +#include "trick/vval.h" #include "trick/Flag.h" #include "trick/Unit.hh"