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

Stefan Seefeld stefan at synopsis.fresco.org
Sat Jan 10 22:50:36 UTC 2004


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

Modified Files:
	Dict.hh List.hh Module.hh Object.hh Tuple.hh 
Log Message:
more work on C++ API

Index: Dict.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/Dict.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- Dict.hh	9 Jan 2004 20:03:25 -0000	1.1
+++ Dict.hh	10 Jan 2004 22:50:34 -0000	1.2
@@ -23,6 +23,8 @@ public:
   friend class iterator;
    
   Dict() : Object(PyDict_New()) {}
+  Dict(Object o) throw(TypeError) : Object(o) 
+  { if (!PyDict_Check(o.my_impl)) throw TypeError("object not a dict");}
   void set(Object k, Object v);
   Object get(Object k) const;
   bool del(Object k);
@@ -31,14 +33,12 @@ public:
   iterator end();
    
   void clear() { PyDict_Clear(my_impl);}
-  Dict copy() const { return PyDict_Copy(my_impl);}
+  Dict copy() const { return Object(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
 };
@@ -68,13 +68,6 @@ private:
   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)
 {

Index: List.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/List.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- List.hh	9 Jan 2004 20:03:25 -0000	1.1
+++ List.hh	10 Jan 2004 22:50:34 -0000	1.2
@@ -18,7 +18,7 @@ class List : public Object
 {
 public:
   List(size_t i = 0) : Object(PyList_New(i)) {}
-  List(const Object &) throw(TypeError);
+  List(Object) throw(TypeError);
   size_t size() const { return PyList_GET_SIZE(my_impl);}
   void set(int i, Object o);
   Object get(int i) const;
@@ -29,13 +29,7 @@ public:
   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)
+List::List(Object o) throw(TypeError)
   : Object(o)
 {
   if (!PyList_Check(o.my_impl)) throw TypeError("object not a list");

Index: Module.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/Module.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- Module.hh	9 Jan 2004 20:03:25 -0000	1.1
+++ Module.hh	10 Jan 2004 22:50:34 -0000	1.2
@@ -11,6 +11,7 @@
 
 #include <Synopsis/Object.hh>
 #include <Synopsis/Dict.hh>
+#include <Synopsis/Callable.hh>
 
 namespace Synopsis
 {
@@ -34,10 +35,9 @@ inline Dict Module::dict() const
 {
   PyObject *d = PyModule_GetDict(my_impl);
   Py_INCREF(d);
-  return d;
+  return Object(d);
 }
 
-
 }
 
 #endif

Index: Object.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/Object.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- Object.hh	9 Jan 2004 20:03:26 -0000	1.1
+++ Object.hh	10 Jan 2004 22:50:34 -0000	1.2
@@ -31,17 +31,17 @@ class Object
 public:
   struct TypeError : std::invalid_argument
   {
-    TypeError(const char *msg = "") : std::invalid_argument(msg) {}
+    TypeError(const std::string &msg = "") : std::invalid_argument(msg) {}
   };
 
   struct AttributeError : std::invalid_argument
   {
-    AttributeError(const char *msg = "") : std::invalid_argument(msg) {}
+    AttributeError(const std::string &msg = "") : std::invalid_argument(msg) {}
   };
 
   struct ImportError : std::invalid_argument
   {
-    ImportError(const char *msg = "") : std::invalid_argument(msg) {}
+    ImportError(const std::string &msg = "") : std::invalid_argument(msg) {}
   };
 
   Object() : my_impl(Py_None) { Py_INCREF(Py_None);}
@@ -62,6 +62,9 @@ public:
   Object repr() const { return PyObject_Repr(my_impl);}
   Object str() const { return PyObject_Str(my_impl);}
   bool is_instance(Object) const;
+  void assert_type(const char *module,
+		   const char *type) const
+    throw(TypeError);
 
   template <typename T>
   static T narrow(Object) throw(TypeError);
@@ -124,6 +127,8 @@ public:
               Object,
               Object,
               Object);
+
+  PyObject *ref() { Py_INCREF(my_impl); return my_impl;}
 private:
    PyObject *my_impl;
 };
@@ -150,6 +155,21 @@ inline bool Object::is_instance(Object o
   return PyObject_IsInstance(my_impl, o.my_impl) == 1;
 }
 
+inline void Object::assert_type(const char *module_name,
+				const char *type_name) const
+  throw(TypeError)
+{
+  Object module = Object::import(module_name);
+  if (!this->is_instance(module.attr(type_name)))
+  {
+    std::string msg = "object not a ";
+    msg += module_name;
+    msg += ".";
+    msg += type_name;
+    throw TypeError(msg);
+  }
+}
+
 inline Object Object::import(const char *name)
 {
   PyObject *retn = PyImport_ImportModule(const_cast<char *>(name));
@@ -313,6 +333,12 @@ inline Object Object::call(const char *n
                              o9.my_impl);
 }
 
+template <typename T>
+inline T Object::narrow(Object o) throw(Object::TypeError)
+{
+  return T(o.my_impl);
+}
+
 template <>
 inline char Object::narrow(Object o) throw(Object::TypeError)
 {

Index: Tuple.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/Tuple.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- Tuple.hh	9 Jan 2004 20:03:26 -0000	1.1
+++ Tuple.hh	10 Jan 2004 22:50:34 -0000	1.2
@@ -20,23 +20,17 @@ 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(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);
+  Tuple(Object, 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)
 {
@@ -118,6 +112,27 @@ Tuple::Tuple(Object o1, Object o2, Objec
    Py_INCREF(o6.my_impl);
 }
 
+Tuple::Tuple(Object o1, Object o2, Object o3,
+             Object o4, Object o5, Object o6,
+	     Object o7)
+  : Object(PyTuple_New(7))
+{
+   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);
+   PyTuple_SET_ITEM(my_impl, 1, o7.my_impl);
+   Py_INCREF(o7.my_impl);
+}
+
 }
 
 #endif





More information about the Synopsis-changes mailing list