~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to src/common_cpp/API/MapBase-inl.hh

  • Committer: Durga Rajaram
  • Date: 2014-07-16 15:13:05 UTC
  • mfrom: (659.1.92 cand)
  • Revision ID: durga@fnal.gov-20140716151305-q27rv1y9p03v9lks
Tags: MAUS-v0.9, MAUS-v0.9.0
MAUS-v0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
#define _SRC_COMMON_CPP_API_MAPBASE_INL_
19
19
 
20
20
#include <string>
 
21
#include "Interface/Squeak.hh"
21
22
#include "src/common_cpp/API/APIExceptions.hh"
22
23
#include "src/common_cpp/Utils/PyObjectWrapper.hh"
23
24
#include "src/common_cpp/Utils/Exception.hh"
40
41
  template<typename TYPE>
41
42
  PyObject* MapBase<TYPE>::process_pyobj(PyObject* py_input) const {
42
43
    // this function owns cpp_data; py_input is still owned by caller
43
 
    TYPE* cpp_data = PyObjectWrapper::unwrap<TYPE>(py_input);
 
44
    TYPE* cpp_data = NULL;
44
45
    try {
 
46
        cpp_data = PyObjectWrapper::unwrap<TYPE>(py_input);
45
47
        _process(cpp_data);
46
 
    }
47
 
    catch (Exception& s) {
48
 
        CppErrorHandler::getInstance()->HandleExceptionNoJson(s, _classname);
49
 
    } catch (std::exception& e) {
50
 
        CppErrorHandler::getInstance()->HandleStdExcNoJson(e, _classname);
 
48
    } catch (MAUS::Exception& exc) {
 
49
        Squeak::mout(Squeak::debug) << "Stack trace:" << exc.GetStackTrace()
 
50
                                                                   << std::endl;
 
51
        HandleException(&cpp_data, &exc, _classname);
 
52
    } catch (std::exception& exc) {
 
53
        HandleException(&cpp_data, &exc, _classname);
51
54
    } catch (...) {
52
55
        throw Exception(Exception::recoverable,
53
56
                        _classname+" threw an unhandled exception",
54
57
                        "MapBase::process_pyobj");
55
58
    }
56
 
 
57
59
    PyObject* py_output = PyObjectWrapper::wrap(cpp_data);
58
60
    // py_output now owns cpp_data
59
61
    return py_output;
60
62
  }
61
63
 
62
 
 
 
64
  // Need some wrapper code for the exception handler as the interface is
 
65
  // assumed to be json. As conversion is error prone, need to be a bit careful
 
66
  // and do the conversion here.
 
67
  template <typename TYPE>
 
68
  void MapBase<TYPE>::HandleException(TYPE** data,
 
69
                               std::exception* exc,
 
70
                               std::string class_name) const {
 
71
      if (!data) {
 
72
          throw *exc;
 
73
      }
 
74
      Json::Value* val = NULL;
 
75
      try {
 
76
          try {
 
77
              val = ConverterFactory().convert<TYPE, Json::Value>(*data);
 
78
          } catch (...) {
 
79
              // do nothing; catch data == NULL or failed conversion to json
 
80
          }
 
81
          delete *data;  // we don't need it any more
 
82
          if (val == NULL) {  // conversion failed, try to build from scratch
 
83
              MAUS::Data data_temp;
 
84
              data_temp.SetSpill(new Spill());
 
85
              val = ConverterFactory().convert<MAUS::Data, Json::Value>(&data_temp);
 
86
          }
 
87
          *val = CppErrorHandler::getInstance()->HandleStdExc(*val, *exc, class_name);
 
88
          *data = ConverterFactory().convert<Json::Value, TYPE>(val);
 
89
          delete val;
 
90
      } catch (...) {
 
91
          std::cerr << "Unhandled exception" << std::endl;
 
92
          throw;
 
93
      }
 
94
  }
63
95
 
64
96
}// end of namespace
65
97
#endif