From fc5155e30dceb32d1a70395b3f00f8cd941c5e1c Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sun, 12 Nov 2000 20:58:42 +0000 Subject: [PATCH] Fixed version number check for __MWERKS__ changed to use new conversion namespace macros Added tests for enums and non-method class attributes Fixed Ullrich's formatting Made it compile with MSVC [SVN r8183] --- extclass_demo.cpp | 69 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/extclass_demo.cpp b/extclass_demo.cpp index d719d3e1..94edc6ae 100644 --- a/extclass_demo.cpp +++ b/extclass_demo.cpp @@ -153,7 +153,7 @@ int IntPairPythonClass::getattr(const IntPair& p, const std::string& s) PyErr_SetString(PyExc_AttributeError, s.c_str()); throw py::ErrorAlreadySet(); } -#if defined(__MWERKS__) && __MWERKS__ <= 0x6000 +#if defined(__MWERKS__) && __MWERKS__ <= 0x2400 return 0; #endif } @@ -163,7 +163,7 @@ void throw_key_error_if_end(const StringMap& m, StringMap::const_iterator p, std { if (p == m.end()) { - PyErr_SetObject(PyExc_KeyError, py::converters::to_python(key)); + PyErr_SetObject(PyExc_KeyError, PY_CONVERSION::to_python(key)); throw py::ErrorAlreadySet(); } } @@ -668,16 +668,49 @@ template class py::ExtensionClass; // explicitly instantiate } // namespace extclass_demo -#ifndef PY_NO_INLINE_FRIENDS_IN_NAMESPACE -namespace py { -#endif +PY_BEGIN_CONVERSION_NAMESPACE inline PyObject* to_python(const extclass_demo::Record* p) { return to_python(*p); } -#ifndef PY_NO_INLINE_FRIENDS_IN_NAMESPACE +PY_END_CONVERSION_NAMESPACE + +/************************************************************/ +/* */ +/* Enums and non-method class attributes */ +/* */ +/************************************************************/ + +namespace extclass_demo { + +struct EnumOwner +{ + public: + enum enum_type { one = 1, two = 2, three = 3 }; + + EnumOwner(enum_type a1, const enum_type& a2) + : m_first(a1), m_second(a2) {} + + void set_first(const enum_type& x) { m_first = x; } + void set_second(const enum_type& x) { m_second = x; } + + enum_type first() { return m_first; } + enum_type second() { return m_second; } + private: + enum_type m_first, m_second; +}; + } -#endif + +namespace py { +template class enum_as_int_converters; +} + +// This is just a way of getting the converters instantiated +//struct EnumOwner_enum_type_Converters +// : py::py_enum_as_int_converters +//{ +//}; namespace extclass_demo { /************************************************************/ @@ -850,9 +883,19 @@ void init_module(py::Module& m) m.def_raw(&raw, "raw"); m.def_raw(&raw1, "raw1"); m.def_raw(&raw2, "raw2"); + + py::ClassWrapper enum_owner(m, "EnumOwner"); + enum_owner.def(py::Constructor()); + enum_owner.def(&EnumOwner::set_first, "__setattr__first__"); + enum_owner.def(&EnumOwner::set_second, "__setattr__second__"); + enum_owner.def(&EnumOwner::first, "__getattr__first__"); + enum_owner.def(&EnumOwner::second, "__getattr__second__"); + enum_owner.add(PyInt_FromLong(EnumOwner::one), "one"); + enum_owner.add(PyInt_FromLong(EnumOwner::two), "two"); + enum_owner.add(PyInt_FromLong(EnumOwner::three), "three"); } -PyObject * raw(py::Tuple const & args, py::Dict const & keywords) +PyObject* raw(py::Tuple const& args, py::Dict const& keywords) { if(args.size() != 2 || keywords.size() != 2) { @@ -860,13 +903,13 @@ PyObject * raw(py::Tuple const & args, py::Dict const & keywords) throw py::ArgumentError(); } - RawTest * first = from_python(args[0].get(), py::Type()); - int second = from_python(args[1].get(), py::Type()); + RawTest* first = PY_CONVERSION::from_python(args[0].get(), py::Type()); + int second = PY_CONVERSION::from_python(args[1].get(), py::Type()); - int third = from_python(keywords[py::String("third")].get(), py::Type()); - int fourth = from_python(keywords[py::String("fourth")].get(), py::Type()); + int third = PY_CONVERSION::from_python(keywords[py::String("third")].get(), py::Type()); + int fourth = PY_CONVERSION::from_python(keywords[py::String("fourth")].get(), py::Type()); - return to_python(first->i_ + second + third + fourth); + return PY_CONVERSION::to_python(first->i_ + second + third + fourth); } void init_module()