[Synopsis-changes] Synopsis/Synopsis/include/Synopsis Callable.hh,NONE,1.1 Dict.hh,NONE,1.1 Interpreter.hh,NONE,1.1 List.hh,NONE,1.1 Module.hh,NONE,1.1 Object.hh,NONE,1.1 Tuple.hh,NONE,1.1

Stefan Seefeld stefan at synopsis.fresco.org
Fri Jan 9 20:03:28 UTC 2004


Update of /cvs/synopsis/Synopsis/include/Synopsis
In directory frida:/tmp/cvs-serv10107/include/Synopsis

Added Files:
	Callable.hh Dict.hh Interpreter.hh List.hh Module.hh Object.hh 
	Tuple.hh 
Log Message:
new C++ interface

--- NEW FILE: Callable.hh ---
// $Id: Callable.hh,v 1.1 2004/01/09 20:03:25 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_Callable_hh
#define _Synopsis_Callable_hh

#include <Synopsis/Object.hh>
#include <Synopsis/Tuple.hh>
#include <Synopsis/Dict.hh>

namespace Synopsis
{

class Callable : public Object
{
public:
   Callable(Object);

   Object call();
   Object call(Tuple);
   Object call(Tuple, Dict);
};

template <>
Callable Object::narrow(Object o) throw(Object::TypeError)
{
  return Callable(o);
}

inline Callable::Callable(Object o)
  : Object(o)
{
  if (!PyCallable_Check(o.my_impl)) throw TypeError("object not a callable");
}

inline Object Callable::call()
{
  return PyObject_CallObject(my_impl, 0);
}

inline Object Callable::call(Tuple args)
{
  return PyObject_Call(my_impl, args.my_impl, 0);
}

inline Object Callable::call(Tuple args, Dict kwds)
{
  return PyObject_Call(my_impl, args.my_impl, kwds.my_impl);
}

}

#endif

--- NEW FILE: Dict.hh ---
// $Id: Dict.hh,v 1.1 2004/01/09 20:03:25 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_Dict_hh
#define _Synopsis_Dict_hh

#include <Synopsis/Object.hh>
#include <Synopsis/List.hh>
#include <Synopsis/Tuple.hh>

namespace Synopsis
{

class Dict : public Object
{
public:
  class iterator;
  friend class iterator;
   
  Dict() : Object(PyDict_New()) {}
  void set(Object k, Object v);
  Object get(Object k) const;
  bool del(Object k);

  iterator begin();
  iterator end();
   
  void clear() { PyDict_Clear(my_impl);}
  Dict copy() const { return PyDict_Copy(my_impl);}
   
  List keys() const { return List(Object(PyDict_Keys(my_impl)));}
  List values() const { return List(Object(PyDict_Values(my_impl)));}
  List items() const { return List(Object(PyDict_Items(my_impl)));}

  Dict(PyObject *o) throw(TypeError)
    : Object(o) { if (!PyDict_Check(o)) throw TypeError("object not a dict");}
private:
  PyObject *impl() { return my_impl;} // extend friendship
};

class Dict::iterator
{
  friend class Dict;
public:
  iterator(const iterator &i) : my_dict(i.my_dict), my_pos(i.my_pos) {}
  iterator &operator = (const iterator &);

  bool operator == (iterator i);
  bool operator != (iterator i) { return !operator==(i);}

  const Tuple &operator *() { return my_current;}
  const Tuple *operator ->() { return &(operator *());}
  iterator operator ++(int) { incr(); return *this;}
  iterator operator ++() { iterator tmp = *this; incr(); return tmp;}

private:
  iterator(Dict, int);
  void incr();

  Dict my_dict;
  int my_pos;

  Tuple my_current;
};

template <>
inline Dict Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyDict_Check(o.my_impl)) throw TypeError("object not a dict");
  return Dict(o.my_impl);
}

inline Dict::iterator::iterator(Dict dict, int pos)
  : my_dict(dict), my_pos(pos)
{
  if (pos != -1) incr();
}

inline Dict::iterator &Dict::iterator::operator = (const Dict::iterator &i)
{
  my_dict = i.my_dict;
  my_pos = i.my_pos;
  my_current = i.my_current;
}

inline void Dict::iterator::incr()
{
  PyObject *key = 0, *value = 0;
  bool valid = PyDict_Next(my_dict.impl(), &my_pos, &key, &value);
  if (!valid) my_pos = -1;
  else
  {
     Py_INCREF(key);
     Py_INCREF(value);   
     my_current = Tuple(key, value);
  }
}

inline bool Dict::iterator::operator == (Dict::iterator i)
{
  return i.my_dict.impl() == my_dict.impl() && i.my_pos == my_pos;
}

inline void Dict::set(Object k, Object v)
{
//   Py_INCREF(k.my_impl); // ?
//   Py_INCREF(v.my_impl); // ?
  PyDict_SetItem(my_impl, k.my_impl, v.my_impl);
}

inline Object Dict::get(Object k) const
{
  PyObject *retn = PyDict_GetItem(my_impl, k.my_impl);
  if (retn) Py_INCREF(retn);
  return retn;
}

inline bool Dict::del(Object k)
{
  return PyDict_DelItem(my_impl, k.my_impl) == 0;
}

inline Dict::iterator Dict::begin()
{
  return iterator(*this, 0);
}

inline Dict::iterator Dict::end()
{
  return iterator(*this, -1);
}

}

#endif

--- NEW FILE: Interpreter.hh ---
// $Id: Interpreter.hh,v 1.1 2004/01/09 20:03:25 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_Interpreter_hh
#define _Synopsis_Interpreter_hh

#include <Synopsis/Object.hh>
#include <string>

namespace Synopsis
{

class Interpreter
{
public:

   enum Mode { EVAL = Py_eval_input,
               FILE = Py_file_input,
               SINGLE = Py_single_input};
   
   Interpreter() {}
   ~Interpreter() {}

   Object run_string(const std::string &, Mode, Object, Object);
   Object run_file(const std::string &, Mode, Object, Object);
private:
};

inline Object Interpreter::run_string(const std::string &code,
                                      Mode m, Object globals, Object locals)
{
   Object retn = PyRun_String(const_cast<char *>(code.c_str()), m,
                              globals.my_impl, locals.my_impl);
   if (!retn) PyErr_Print();
   return retn;
}

inline Object Interpreter::run_file(const std::string &script,
                                    Mode m, Object globals, Object locals)
{
   ::FILE *file = fopen(script.c_str(), "r");
   Object retn = PyRun_File(file, const_cast<char *>(script.c_str()),
                            m, globals.my_impl, locals.my_impl);
   fclose(file);
   if (!retn) PyErr_Print();
   return retn;
}

}

#endif

--- NEW FILE: List.hh ---
// $Id: List.hh,v 1.1 2004/01/09 20:03:25 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_List_hh
#define _Synopsis_List_hh

#include <Synopsis/Object.hh>

namespace Synopsis
{

class List : public Object
{
public:
  List(size_t i = 0) : Object(PyList_New(i)) {}
  List(const Object &) throw(TypeError);
  size_t size() const { return PyList_GET_SIZE(my_impl);}
  void set(int i, Object o);
  Object get(int i) const;
  void append(Object o) { PyList_Append(my_impl, o.my_impl);}
  void insert(int i, Object o) { PyList_Insert(my_impl, i, o.my_impl);}
  List get_slice(int low, int high) const { return List(PyList_GetSlice(my_impl, low, high));}
  bool sort() { return PyList_Sort(my_impl) == 0;}
  bool reverse() { return PyList_Reverse(my_impl) == 0;}
};

template <>
List Object::narrow(Object o) throw(Object::TypeError)
{
  return List(o);
}

List::List(const Object &o) throw(TypeError)
  : Object(o)
{
  if (!PyList_Check(o.my_impl)) throw TypeError("object not a list");
}

inline void List::set(int i, Object o)
{
  Py_INCREF(o.my_impl);
  PyList_SetItem(my_impl, i, o.my_impl);
}

inline Object List::get(int i) const
{
  PyObject *retn = PyList_GetItem(my_impl, i);
  Py_INCREF(retn);
  return Object(retn);
}

}

#endif

--- NEW FILE: Module.hh ---
// $Id: Module.hh,v 1.1 2004/01/09 20:03:25 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_Module_hh
#define _Synopsis_Module_hh

#include <Synopsis/Object.hh>
#include <Synopsis/Dict.hh>

namespace Synopsis
{

class Module : public Object
{
public:
  Module(const std::string &);
  std::string name() const { return PyModule_GetName(my_impl);}
  std::string filename() const { return PyModule_GetFilename(my_impl);}
  Dict dict() const;
};

inline Module::Module(const std::string &name)
  : Object(PyImport_AddModule(const_cast<char *>(name.c_str())))
{
  Py_INCREF(my_impl);
}

inline Dict Module::dict() const
{
  PyObject *d = PyModule_GetDict(my_impl);
  Py_INCREF(d);
  return d;
}


}

#endif

--- NEW FILE: Object.hh ---
// $Id: Object.hh,v 1.1 2004/01/09 20:03:26 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_Object_hh
#define _Synopsis_Object_hh

#include <Python.h>
#include <string>
#include <stdexcept>

namespace Synopsis
{

//. Object provides basic
//. functionality to access the underlaying
//. python object, to be used in subclasses and
//. generic accessors such as (python) lists and tuples
class Object
{
  friend class List;
  friend class Tuple;
  friend class Dict;
  friend class Callable;
  friend class Module;
  friend class Interpreter;
public:
  struct TypeError : std::invalid_argument
  {
    TypeError(const char *msg = "") : std::invalid_argument(msg) {}
  };

  struct AttributeError : std::invalid_argument
  {
    AttributeError(const char *msg = "") : std::invalid_argument(msg) {}
  };

  struct ImportError : std::invalid_argument
  {
    ImportError(const char *msg = "") : std::invalid_argument(msg) {}
  };

  Object() : my_impl(Py_None) { Py_INCREF(Py_None);}
  Object(PyObject *);
  Object(const Object &o) : my_impl(o.my_impl) { Py_INCREF(my_impl);}
  Object(const std::string &value) : my_impl(PyString_FromString(value.c_str())) {}
  Object(const char *value) : my_impl(PyString_FromString(value)) {}
  Object(char value) : my_impl(PyString_FromStringAndSize(&value, 1)) {}
  Object(double value) : my_impl(PyFloat_FromDouble(value)) {}
  Object(int value) : my_impl(PyInt_FromLong(value)) {}
  Object(long value) : my_impl(PyInt_FromLong(value)) {}
  Object(bool value) : my_impl(PyInt_FromLong(value)) {}
  virtual ~Object() { Py_DECREF(my_impl);}
  Object &operator = (Object o);
   
  int hash() const { return PyObject_Hash(my_impl);}
  operator bool () const { return PyObject_IsTrue(my_impl);}
  Object repr() const { return PyObject_Repr(my_impl);}
  Object str() const { return PyObject_Str(my_impl);}
  bool is_instance(Object) const;

  template <typename T>
  static T narrow(Object) throw(TypeError);
  static Object import(const char *name);

  Object attr(const char *name) const;

  Object call(const char *name);
  Object call(const char *name,
              Object);
  Object call(const char *name,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object);
  Object call(const char *name,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object,
              Object);
private:
   PyObject *my_impl;
};

inline Object::Object(PyObject *o)
  : my_impl(o)
{
  if (!my_impl)
  {
    my_impl = Py_None;
    Py_INCREF(Py_None);
  }
}

inline Object &Object::operator = (Object o)
{
   Py_DECREF(my_impl);
   my_impl = o.my_impl;
   Py_INCREF(my_impl);
}

inline bool Object::is_instance(Object o) const
{
  return PyObject_IsInstance(my_impl, o.my_impl) == 1;
}

inline Object Object::import(const char *name)
{
  PyObject *retn = PyImport_ImportModule(const_cast<char *>(name));
  if (!retn) throw ImportError(name);
  else return Object(retn);
}

inline Object Object::attr(const char *name) const
{
  PyObject *retn = PyObject_GetAttrString(my_impl, const_cast<char *>(name));
  if (!retn) throw AttributeError(name);
  else return Object(retn);
}

inline Object Object::call(const char *name)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name), 0);
}

inline Object Object::call(const char *name,
                           Object o1)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "O",
                             o1.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OO",
                             o1.my_impl,
                             o2.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3,
                           Object o4)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl,
                             o4.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3,
                           Object o4,
                           Object o5)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOOOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl,
                             o4.my_impl,
                             o5.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3,
                           Object o4,
                           Object o5,
                           Object o6)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOOOOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl,
                             o4.my_impl,
                             o5.my_impl,
                             o6.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3,
                           Object o4,
                           Object o5,
                           Object o6,
                           Object o7)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOOOOOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl,
                             o4.my_impl,
                             o5.my_impl,
                             o6.my_impl,
                             o7.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3,
                           Object o4,
                           Object o5,
                           Object o6,
                           Object o7,
                           Object o8)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOOOOOOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl,
                             o4.my_impl,
                             o5.my_impl,
                             o6.my_impl,
                             o7.my_impl,
                             o8.my_impl);
}

inline Object Object::call(const char *name,
                           Object o1,
                           Object o2,
                           Object o3,
                           Object o4,
                           Object o5,
                           Object o6,
                           Object o7,
                           Object o8,
                           Object o9)
{
  return PyObject_CallMethod(my_impl, const_cast<char *>(name),
                             "OOOOOOOOO",
                             o1.my_impl,
                             o2.my_impl,
                             o3.my_impl,
                             o4.my_impl,
                             o5.my_impl,
                             o6.my_impl,
                             o7.my_impl,
                             o8.my_impl,
                             o9.my_impl);
}

template <>
inline char Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyString_Check(o.my_impl)
      || PyString_GET_SIZE(o.my_impl) != 1) throw TypeError("object not a character");
  char *value;
  int length;
  PyString_AsStringAndSize(o.my_impl, &value, &length);
  return value[0];
}

template <>
inline const char *Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyString_Check(o.my_impl)) throw TypeError("object not a string");
  return PyString_AS_STRING(o.my_impl);
}

template <>
inline std::string Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyString_Check(o.my_impl)) throw TypeError("object not a string");
  return PyString_AS_STRING(o.my_impl);
}

template <>
inline double Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyFloat_Check(o.my_impl)) throw TypeError("object not a float");
  return PyFloat_AsDouble(o.my_impl);
}

template <>
inline long Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyInt_Check(o.my_impl)) throw TypeError("object not an integer");
  return PyInt_AsLong(o.my_impl);
}

template <>
inline bool Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyInt_Check(o.my_impl)) throw TypeError("object not an integer");
  return PyInt_AsLong(o.my_impl);
}

}

#endif

--- NEW FILE: Tuple.hh ---
// $Id: Tuple.hh,v 1.1 2004/01/09 20:03:26 stefan Exp $
//
// Copyright (C) 2004 Stefan Seefeld
// All rights reserved.
// Licensed to the public under the terms of the GNU LGPL (>= 2),
// see the file COPYING for details.
//

#ifndef _Synopsis_Tuple_hh
#define _Synopsis_Tuple_hh

#include <Synopsis/Object.hh>
#include <Synopsis/List.hh>

namespace Synopsis
{

class Tuple : public Object
{
public:
  Tuple(size_t i = 0) : Object(PyTuple_New(i)) {}
  Tuple(PyObject *);
  Tuple(const List &l) : Object(PyList_AsTuple(l.my_impl)) {}
  Tuple(Object);
  Tuple(Object, Object);
  Tuple(Object, Object, Object);
  Tuple(Object, Object, Object, Object);
  Tuple(Object, Object, Object, Object, Object);
  Tuple(Object, Object, Object, Object, Object, Object);
  Object get(size_t i) const { return PyTuple_GetItem(my_impl, i); }
};

template <>
Tuple Object::narrow(Object o) throw(Object::TypeError)
{
  if (!PyTuple_Check(o.my_impl)) throw TypeError("object not a tuple");
  return Tuple(o.my_impl);
}

Tuple::Tuple(PyObject *o)
  : Object(o)
{
  if (!PyTuple_Check(o)) throw TypeError("object not a tuple");
}

Tuple::Tuple(Object o)
  : Object(PyTuple_New(1))
{
   PyTuple_SET_ITEM(my_impl, 0, o.my_impl);
   Py_INCREF(o.my_impl);
}

Tuple::Tuple(Object o1, Object o2)
  : Object(PyTuple_New(2))
{
   PyTuple_SET_ITEM(my_impl, 0, o1.my_impl);
   Py_INCREF(o1.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o2.my_impl);
   Py_INCREF(o2.my_impl);
}

Tuple::Tuple(Object o1, Object o2, Object o3)
  : Object(PyTuple_New(3))
{
   PyTuple_SET_ITEM(my_impl, 0, o1.my_impl);
   Py_INCREF(o1.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o2.my_impl);
   Py_INCREF(o2.my_impl);
   PyTuple_SET_ITEM(my_impl, 2, o3.my_impl);
   Py_INCREF(o3.my_impl);
}

Tuple::Tuple(Object o1, Object o2, Object o3,
             Object o4)
  : Object(PyTuple_New(4))
{
   PyTuple_SET_ITEM(my_impl, 0, o1.my_impl);
   Py_INCREF(o1.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o2.my_impl);
   Py_INCREF(o2.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o3.my_impl);
   Py_INCREF(o3.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o4.my_impl);
   Py_INCREF(o4.my_impl);
}

Tuple::Tuple(Object o1, Object o2, Object o3,
             Object o4, Object o5)
  : Object(PyTuple_New(5))
{
   PyTuple_SET_ITEM(my_impl, 0, o1.my_impl);
   Py_INCREF(o1.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o2.my_impl);
   Py_INCREF(o2.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o3.my_impl);
   Py_INCREF(o3.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o4.my_impl);
   Py_INCREF(o4.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o5.my_impl);
   Py_INCREF(o5.my_impl);
}

Tuple::Tuple(Object o1, Object o2, Object o3,
             Object o4, Object o5, Object o6)
  : Object(PyTuple_New(6))
{
   PyTuple_SET_ITEM(my_impl, 0, o1.my_impl);
   Py_INCREF(o1.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o2.my_impl);
   Py_INCREF(o2.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o3.my_impl);
   Py_INCREF(o3.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o4.my_impl);
   Py_INCREF(o4.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o5.my_impl);
   Py_INCREF(o5.my_impl);
   PyTuple_SET_ITEM(my_impl, 1, o6.my_impl);
   Py_INCREF(o6.my_impl);
}

}

#endif





More information about the Synopsis-changes mailing list