~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to src/common_cpp/API/PyWrapInputBaseEmitter.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:
 
1
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 
2
 *
 
3
 * MAUS is free software: you can redistribute it and/or modify
 
4
 * it under the terms of the GNU General Public License as published by
 
5
 * the Free Software Foundation, either version 3 of the License, or
 
6
 * (at your option) any later version.
 
7
 *
 
8
 * MAUS is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
15
 */
 
16
 
 
17
#ifndef _SRC_COMMON_CPP_API_PYWRAPINPUTBASEEMITTER_
 
18
#define _SRC_COMMON_CPP_API_PYWRAPINPUTBASEEMITTER_
 
19
 
 
20
#include <string>
 
21
 
 
22
// These ifdefs are required to avoid cpp compiler warning
 
23
#ifdef _POSIX_C_SOURCE
 
24
#undef _POSIX_C_SOURCE
 
25
#endif
 
26
 
 
27
#ifdef _XOPEN_SOURCE
 
28
#undef _XOPEN_SOURCE
 
29
#endif
 
30
 
 
31
#include "Python.h"
 
32
#include "structmember.h"
 
33
 
 
34
 
 
35
namespace MAUS {
 
36
 
 
37
template <class INPUTCLASS>
 
38
class PyWrapInputBaseEmitter {
 
39
 public:
 
40
  typedef struct {
 
41
    PyObject_HEAD;
 
42
    INPUTCLASS* input;
 
43
  } PyWrappedInputEmitter;
 
44
 
 
45
 private:
 
46
    static PyObject* Iter(PyObject *self);
 
47
    static PyObject* Next(PyObject *self);
 
48
    static PyTypeObject _class_type;
 
49
 
 
50
    static std::string _class_name;  // Class
 
51
    static std::string _path_name;  // module.Class
 
52
 
 
53
    // only PyWrapInputBase can initialise/destruct PyWrapInputBaseEmitter
 
54
    friend class PyWrapInputBase<INPUTCLASS>;
 
55
};
 
56
 
 
57
 
 
58
template <class INPUTCLASS>
 
59
PyObject* PyWrapInputBaseEmitter<INPUTCLASS>::Iter(PyObject *self) {
 
60
  Py_INCREF(self);
 
61
  return self;
 
62
}
 
63
 
 
64
template <class INPUTCLASS>
 
65
PyObject* PyWrapInputBaseEmitter<INPUTCLASS>::Next(PyObject *self) {
 
66
  PyWrappedInputEmitter *py_wrap_emitter =
 
67
                               reinterpret_cast<PyWrappedInputEmitter*>(self);
 
68
  if (py_wrap_emitter) {
 
69
      PyObject* out = py_wrap_emitter->input->emit_pyobj();
 
70
      if (out) {
 
71
          return out;
 
72
      } else {
 
73
          /* Raising of standard StopIteration exception with empty value. */
 
74
          PyErr_SetNone(PyExc_StopIteration);
 
75
          return NULL;
 
76
      }
 
77
  } else {
 
78
      PyErr_SetString(PyExc_RuntimeError, "Failed to get the emitter");
 
79
      return NULL;
 
80
  }
 
81
}
 
82
 
 
83
template <class INPUTCLASS>
 
84
PyTypeObject PyWrapInputBaseEmitter<INPUTCLASS>::_class_type = {
 
85
    PyObject_HEAD_INIT(NULL)
 
86
    0,                         /*ob_size*/
 
87
    "PyWrapInputBaseIterator",            /*tp_name*/
 
88
    sizeof(PyWrappedInputEmitter),       /*tp_basicsize*/
 
89
    0,                         /*tp_itemsize*/
 
90
    0,                         /*tp_dealloc*/
 
91
    0,                         /*tp_print*/
 
92
    0,                         /*tp_getattr*/
 
93
    0,                         /*tp_setattr*/
 
94
    0,                         /*tp_compare*/
 
95
    0,                         /*tp_repr*/
 
96
    0,                         /*tp_as_number*/
 
97
    0,                         /*tp_as_sequence*/
 
98
    0,                         /*tp_as_mapping*/
 
99
    0,                         /*tp_hash */
 
100
    0,                         /*tp_call*/
 
101
    0,                         /*tp_str*/
 
102
    0,                         /*tp_getattro*/
 
103
    0,                         /*tp_setattro*/
 
104
    0,                         /*tp_as_buffer*/
 
105
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_ITER,
 
106
      /* tp_flags: Py_TPFLAGS_HAVE_ITER tells python to
 
107
         use tp_iter and tp_iternext fields. */
 
108
    "PyWrapInputBase iterator object.",           /* tp_doc */
 
109
    0,  /* tp_traverse */
 
110
    0,  /* tp_clear */
 
111
    0,  /* tp_richcompare */
 
112
    0,  /* tp_weaklistoffset */
 
113
    PyWrapInputBaseEmitter<INPUTCLASS>::Iter,  /* tp_iter: __iter__() method */
 
114
    PyWrapInputBaseEmitter<INPUTCLASS>::Next  /* tp_iternext: next() method */
 
115
};
 
116
 
 
117
template <class INPUTCLASS>
 
118
std::string PyWrapInputBaseEmitter<INPUTCLASS>::_class_name = "";  // Class
 
119
template <class INPUTCLASS>
 
120
std::string PyWrapInputBaseEmitter<INPUTCLASS>::_path_name = "";  // module.Class
 
121
 
 
122
} // namespace MAUS
 
123
#endif
 
124