/usr/include/polybori/embed.h is in libpolybori-dev 0.8.3-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | // -*- c++ -*-
//*****************************************************************************
/** @file embed.h
*
* @author Alexander Dreyer
* @date 2012-06-15
*
* This file is a header-only framework for supporting PolyBoRi embedding.
* It also incorporates Python (via boost_python).
*
* @par Copyright:
* (c) 2012 by The PolyBoRi Team
*
**/
//*****************************************************************************
#ifndef polybori_embed_h_
#define polybori_embed_h_
// include basic definitions
#include <polybori/polybori.h>
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <set>
#ifndef PBORI_PYTHONPATH
#define PBORI_PYTHONPATH "."
#endif
BEGIN_NAMESPACE_PBORI
/// objects from namespace boost::python;
using boost::python::str;
using boost::python::import;
using boost::python::handle;
using boost::python::borrowed;
using boost::python::extract;
using boost::python::stl_input_iterator;
using boost::python::error_already_set;
/// convenience aliases
typedef BoolePolyRing Ring;
typedef BoolePolynomial Polynomial;
typedef BooleVariable Variable;
typedef BooleMonomial Monomial;
/// copy enums for convenience
enum order_codes {
lp = PBORI::COrderEnums::lp,
dlex = PBORI::COrderEnums::dlex,
dp_asc = PBORI::COrderEnums::dp_asc,
block_dlex = PBORI::COrderEnums::block_dlex,
block_dp_asc = PBORI::COrderEnums::block_dp_asc
};
/** @class Interpreter
* This class initializes and finalized the python interpreter.
*
* It also imports polybori and stores the main context.
* @note If the polybori python module is not at default location you may set
* the macro PBORI_PYTHONPATH accordingly (before including this header).
**/
class Interpreter {
public:
/// Destructor
~Interpreter() { if(m_owns_python) Py_Finalize(); }
/// Initialize unique (singleton) python interpreter instance,
/// and set Singular type identifier
static void init() { instance(); }
/// acces corresponding context
static boost::python::object& globals() { return instance().m_globals; }
private:
/// Singleton: Only instance() is allowed to construct an instance
Interpreter():
m_owns_python(false), m_globals() {
if (!Py_IsInitialized()) init_python();
set_python_defaults();
}
/// Static initialization -
/// safely takes care of destruction on program termination
static Interpreter& instance() {
static Interpreter init_interpreter;
return init_interpreter;
}
/// start the python interpreter, it not done yet
void init_python() {
Py_Initialize();
m_owns_python = true;
}
/// @note Always keep this routine in header to allow custom PBORI_PYTHONPATH
void set_python_defaults() {
// Sone python modules needs argc, argv set for some reason
const char* argv = "";
PySys_SetArgv(1, (char**)&argv);
import("sys").attr("path").attr("insert")(0, PBORI_PYTHONPATH);
PyRun_SimpleString("from polybori.frontend import *");
m_globals = import("__main__").attr("__dict__");
boost::python::object start = boost::python::eval("polybori_start", m_globals, m_globals);
start(m_globals);
}
/// store whether this instance had initialized python
bool m_owns_python;
/// the active global context
boost::python::object m_globals;
};
template <class Type>
class DerefProxy {
typedef DerefProxy self;
public:
DerefProxy(const Type& val): m_val(val) {}
DerefProxy<self> operator*() {
return *this;
}
const Type& get() const{ return m_val; }
private:
const Type& m_val;
};
/** @class dict
* This class extends boost::python::tuple by a Python-style **-operator
**/
class dict:
public boost::python::dict {
typedef dict self;
typedef boost::python::dict base;
public:
/// Unary constructors
template <class Type>
dict(const Type& obj): base(obj) {}
/// Default constructor
dict(): base() {}
//// Mimicking Pythons **-operator
DerefProxy<self> operator*() {
return *this;
}
};
/** @class tuple
* This class extends boost::python::tuple by a Python-style *-operator
**/
class tuple:
public boost::python::tuple {
typedef tuple self;
typedef boost::python::tuple base;
public:
/// Unary constructors
template <class Type>
tuple(const Type& obj): base(obj) {}
/// Default constructor
tuple(): base() {}
//// Mimicking Pythons *-operator
DerefProxy<self> operator*() {
return *this;
}
};
/** @class object
* This class extends boost::python::object by nice conversion operators
**/
class object:
public boost::python::object {
typedef object self;
typedef boost::python::object base;
public:
/// Unary constructors
template <class Type>
object(const Type& obj): base(obj) {}
/// Default constructor
object(): base() {}
/// Generic call operation
self
operator()(const DerefProxy<tuple>& args,
const DerefProxy<DerefProxy<dict> >& kwds) const {
return object(handle<>(borrowed(PyObject_Call(base::ptr(), args.get().ptr(),
kwds.get().get().ptr()))));
}
/// @name Call with plain arguments
//@{
template <class Type>
self operator()(const Type& arg) const {
return static_cast<const boost::python::object&>(*this)(arg);
}
template <class Type1, class Type2>
self operator()(const Type1& arg1, const Type2& arg2) const {
return static_cast<const boost::python::object&>(*this)(arg1, arg2);
}
template <class Type1, class Type2, class Type3>
self operator()(const Type1& arg1, const Type2& arg2, const Type3& arg3) const {
return static_cast<const boost::python::object&>(*this)(arg1, arg2, arg3);
}
template <class Type1, class Type2, class Type3, class Type4>
self operator()(const Type1& arg1, const Type2& arg2, const Type3& arg3,
const Type4& arg4) const {
return static_cast<const boost::python::object&>(*this)(arg1, arg2, arg3,
arg4);
}
//@}
/// @name Call with keyword arguments
//@{
template <class Type>
self operator()(const DerefProxy<DerefProxy<dict> >& kwds) const {
return self::operator()(DerefProxy<tuple>(boost::python::make_tuple()), kwds);
}
template <class Type>
self operator()(const Type& arg,
const DerefProxy<DerefProxy<dict> >& kwds) const {
return self::operator()(DerefProxy<tuple>(boost::python::make_tuple(arg)), kwds);
}
template <class Type1, class Type2>
self operator()(const Type1& arg1, const Type2& arg2,
const DerefProxy<DerefProxy<dict> >& kwds) const {
return self::operator()(DerefProxy<tuple>(boost::python::make_tuple(arg1, arg2)), kwds);
}
template <class Type1, class Type2, class Type3>
self operator()(const Type1& arg1, const Type2& arg2, const Type3& arg3,
const DerefProxy<DerefProxy<dict> >& kwds) const {
return self::operator()(DerefProxy<tuple>(boost::python::make_tuple(arg1, arg2, arg3)), kwds);
}
template <class Type1, class Type2, class Type3, class Type4>
self operator()(const Type1& arg1, const Type2& arg2, const Type3& arg3,
const Type4& arg4, const DerefProxy<DerefProxy<dict> >& kwds) const {
return self::operator()(DerefProxy<tuple>(boost::python::make_tuple(arg1, arg2, arg3,
arg4)), kwds);
}
//@}
/// @name Convertig to PolyBoRi types and some built-ins
//@{
operator bool() const {
return extract<bool>(*this);
}
operator int() const {
return extract<int>(*this);
}
operator long() const {
return extract<long>(*this);
}
operator std::string() const {
return extract<std::string>(*this);
}
operator const Ring&() const {
return extract<const Ring&>(*this);
}
operator Polynomial() const {
return extract<Polynomial>(*this);
}
operator Monomial() const {
return extract<Monomial>(*this);
}
operator const Variable&() const {
return extract<const Variable&>(*this);
}
operator BooleSet() const {
return extract<BooleSet>(*this);
}
template <class Type>
operator std::vector<Type>() const {
stl_input_iterator<self> begin(*this), end;
return std::vector<Type>(begin, end);
}
template <class Type>
operator std::list<Type>() const {
stl_input_iterator<self> begin(*this), end;
return std::list<Type>(begin, end);
}
template <class Type>
operator std::set<Type>() const {
stl_input_iterator<self> begin(*this), end;
return std::set<Type>(begin, end);
}
//@}
};
inline object
eval(str expression) {
try {
return boost::python::eval(expression,
Interpreter::globals(),
Interpreter::globals());
}
catch( error_already_set ) {
PyErr_Print();
}
return object();
}
inline object
exec(str code) {
try {
return boost::python::exec(code,
Interpreter::globals(),
Interpreter::globals());
}
catch( error_already_set ) {
PyErr_Print();
}
return object();
}
inline object
exec_file(str filename) {
try {
return boost::python::exec_file(filename,
Interpreter::globals(),
Interpreter::globals());
}
catch( error_already_set ) {
PyErr_Print();
}
return object();
}
inline void
run (const char* code) {
PyRun_SimpleString(code);
}
END_NAMESPACE_PBORI
namespace boost { namespace python { namespace converter {
template <>
struct object_manager_traits<PBORI::dict>:
object_manager_traits<boost::python::dict> {};
template <>
struct object_manager_traits<PBORI::tuple>:
object_manager_traits<boost::python::tuple> {};
template <>
struct object_manager_traits<PBORI::object>:
object_manager_traits<boost::python::object> {};
}}}
#define BEGIN_PBORI_EMBED() try { USING_NAMESPACE_PBORI; while(0)
#define END_PBORI_EMBED() } catch(PBORI::error_already_set) { PyErr_Print(); } while(0)
#endif /* polybori_embed_h_ */
|