[Synopsis-changes] Synopsis/Synopsis/include/Synopsis Object.hh,1.3,1.4 List.hh,1.3,1.4
Stefan Seefeld stefan at synopsis.fresco.orgWed Jan 14 04:03:50 UTC 2004
- Previous message: [Synopsis-changes] Synopsis/Synopsis/tests/Parsers/Cpp/input macros.cc,NONE,1.1
- Next message: [Synopsis-changes] Synopsis/Synopsis/tests/Cxx-API/Python/expected List.out,1.1,1.2
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Update of /cvs/synopsis/Synopsis/include/Synopsis
In directory frida:/tmp/cvs-serv17056/include/Synopsis
Modified Files:
Object.hh List.hh
Log Message:
add List iterator
Index: Object.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/Object.hh,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -p -d -r1.3 -r1.4
--- Object.hh 11 Jan 2004 19:46:29 -0000 1.3
+++ Object.hh 14 Jan 2004 04:03:48 -0000 1.4
@@ -55,7 +55,7 @@ public:
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);
+ Object &operator = (const Object &o);
int hash() const { return PyObject_Hash(my_impl);}
operator bool () const { return my_impl != Py_None;}
@@ -67,8 +67,12 @@ public:
const char *type) const
throw(TypeError);
+ //. try to downcast to T, throw on failure
template <typename T>
static T narrow(Object) throw(TypeError);
+ //. more relaxed form of downcast, return None on failure
+ template <typename T>
+ static T try_narrow(Object);
static Object import(const char *name);
Object attr(const char *name) const;
@@ -88,7 +92,7 @@ inline Object::Object(PyObject *o)
}
}
-inline Object &Object::operator = (Object o)
+inline Object &Object::operator = (const Object &o)
{
Py_DECREF(my_impl);
my_impl = o.my_impl;
@@ -135,6 +139,13 @@ inline T Object::narrow(Object o) throw(
return T(o.my_impl);
}
+template <typename T>
+inline T Object::try_narrow(Object o)
+{
+ try { return T(o.my_impl);}
+ catch (const TypeError &) { return T();}
+}
+
template <>
inline char Object::narrow(Object o) throw(Object::TypeError)
{
Index: List.hh
===================================================================
RCS file: /cvs/synopsis/Synopsis/include/Synopsis/List.hh,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -p -d -r1.3 -r1.4
--- List.hh 13 Jan 2004 07:42:09 -0000 1.3
+++ List.hh 14 Jan 2004 04:03:48 -0000 1.4
@@ -18,6 +18,8 @@ namespace Synopsis
class List : public Object
{
public:
+ class iterator;
+
List(size_t i = 0) : Object(PyList_New(i)) {}
List(Object) throw(TypeError);
Tuple tuple() const { return Tuple(PyList_AsTuple(my_impl));}
@@ -26,11 +28,63 @@ public:
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);}
+
+ iterator begin();
+ iterator end();
+
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;}
+private:
+ PyObject *impl() { return my_impl;} // extend friendship
+};
+
+class List::iterator
+{
+ friend class List;
+public:
+ iterator(const iterator &i) : my_list(i.my_list), my_pos(i.my_pos) {}
+ iterator &operator = (const iterator &);
+
+ bool operator == (iterator i);
+ bool operator != (iterator i) { return !operator==(i);}
+
+ const Object &operator *() { return my_current;}
+ const Object *operator ->() { return &(operator *());}
+ iterator operator ++(int) { incr(); return *this;}
+ iterator operator ++() { iterator tmp = *this; incr(); return tmp;}
+
+private:
+ iterator(List l, int i) : my_list(l), my_pos(i)
+ { if (my_pos >= 0) my_current = my_list.get(my_pos);}
+ void incr();
+
+ List my_list;
+ int my_pos;
+
+ Object my_current;
};
+inline List::iterator &List::iterator::operator = (const List::iterator &i)
+{
+ my_list = i.my_list;
+ my_pos = i.my_pos;
+ my_current = i.my_current;
+}
+
+inline bool List::iterator::operator == (List::iterator i)
+{
+ return i.my_list.impl() == my_list.impl() && i.my_pos == my_pos;
+}
+
+inline void List::iterator::incr()
+{
+ if (++my_pos < my_list.size())
+ my_current = my_list.get(my_pos);
+ else
+ my_pos = -1;
+}
+
List::List(Object o) throw(TypeError)
: Object(o)
{
@@ -50,6 +104,16 @@ inline Object List::get(int i) const
return Object(retn);
}
+inline List::iterator List::begin()
+{
+ return iterator(*this, 0);
+}
+
+inline List::iterator List::end()
+{
+ return iterator(*this, -1);
+}
+
}
#endif
- Previous message: [Synopsis-changes] Synopsis/Synopsis/tests/Parsers/Cpp/input macros.cc,NONE,1.1
- Next message: [Synopsis-changes] Synopsis/Synopsis/tests/Cxx-API/Python/expected List.out,1.1,1.2
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Synopsis-changes mailing list