File: PycRecord.cc

package info (click to toggle)
casacore 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,912 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (97 lines) | stat: -rw-r--r-- 3,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//# PycRecord.cc: Class to convert a Record to/from Python
//# Copyright (C) 2006
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: casa-feedback@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA

#include <casacore/python/Converters/PycRecord.h>
#include <casacore/python/Converters/PycValueHolder.h>
#include <casacore/casa/Utilities/Assert.h>
#include <boost/python/object.hpp>

namespace casacore { namespace python {

  boost::python::dict casa_record_to_python::makeobject
  (Record const& rec)
  {
    boost::python::dict d;
    // Copy over the record field by field
    uInt nf = rec.nfields();
    for (uInt i=0; i<nf; i++) {
      d.setdefault ((std::string const&)(rec.name(i)),
		    casa_value_to_python::makeobject(rec.asValueHolder(i)));
    }
    return d;
  }


  void* casa_record_from_python::convertible(PyObject* obj_ptr)
  {
    if (!(PyDict_Check(obj_ptr))) return 0;
    return obj_ptr;
  }

  void casa_record_from_python::construct
  (PyObject* obj_ptr,
   boost::python::converter::rvalue_from_python_stage1_data* data)
  {
    using namespace boost::python;
    void* storage = ((converter::rvalue_from_python_storage<Record>*)
		     data)->storage.bytes;
    new (storage) Record();
    data->convertible = storage;
    Record& result = *((Record*)storage);
    result = makeRecord (obj_ptr);
  }

  Record casa_record_from_python::makeRecord (PyObject* obj_ptr)
  {
    using namespace boost::python;
    AlwaysAssert (PyDict_Check(obj_ptr), AipsError);
    dict d = extract<dict>(obj_ptr)();
    list keys = d.keys();
    Record result;
    handle<> obj_iter(PyObject_GetIter(keys.ptr()));
    std::size_t i=0;
    for(;;i++) {
      handle<> py_elem_hdl(allow_null(PyIter_Next(obj_iter.get())));
      if (PyErr_Occurred()) throw_error_already_set();
      if (!py_elem_hdl.get()) break;             // end of iteration
      object py_elem_key(py_elem_hdl);
      result.defineFromValueHolder (extract<String>(py_elem_key)(),
           casa_value_from_python::makeValueHolder(d.get(py_elem_key).ptr()));
    }
    return result;
  }


  bool convert_casa_record::_done = false;
  void convert_casa_record::reg()
  {
    if (! _done) {
      _done = true;
      boost::python::to_python_converter<Record, casa_record_to_python>();
      casa_record_from_python();
    }
  }

}}