[C++-sig] Automatic (implicit ?) type conversion for a data member of a struct.

Gavin Doughtie gdoughtie at anim.dreamworks.com
Sat May 24 02:04:03 CEST 2003


Well, I'm trying to do the same thing, but I think I'm not understanding 
something basic. The PyObject* parameter passed to my Extractor's 
"execute" method is "0x3" or "0x4" or something like that.

I'd love some help on this.

Here's the relevant code:

// CString is an internally-developed string class that I want to do
// implicit conversions for

// Converter
struct cstring_to_str
{
     static PyObject* convert(cdm_base::CString const& inputStr)
     {
       return PyString_FromString(inputStr.ptr());
     }
};

struct CStringExtractor
{
   static CString foo;
   static CString& execute(PyObject *pyString)
   {
      handle<> strHandle(pyString);
      str strObj(strHandle);
      char *theString = extract< char* >(strObj);

	// Not very thread safe...
      foo = theString;
      return foo;
    }
};

cdm_base::CString CStringExtractor::foo = "Uninitialized";


// Module 
======================================================================
BOOST_PYTHON_MODULE(cdm_python)
{
	// this works fine
     to_python_converter<cdm_base::CString, cstring_to_str>();

	// THIS FAILS MISERABLY
     lvalue_from_pytype<CStringExtractor, &PyString_Type>();

//etc...
}
pj wrote:
> Greetings.
> 
> Generally how would I enable automatic conversion for a given C++ type ?
> 
> To be specific.
> 
> I know how to expose this class to Python and exposing a function returning
> this struct.
> Its pretty easy due to the power of boost-python.
> 
> class MyStruct
> {
>     std::string name;
> }
> 
> MyStruct fuc1();
> 
> 
> What I cant figure out is how would I do this if the 'name' is not a type
> understood by boost-python.
> say ..
> 
> class MyString;
> 
> class MyStruct
> {
>     MyString name;
> }
> 
> MyStruct fuc1();
> 
> I _dont_ want to wrap the whole 'MyString' class to Python.
> 
> I would like to expose 'MyStruct' to python so that the 'name' data member
> of
> MyStruct is _automatically_  _exposed_ as
> a python string ?
> 
> I have tried the basic documentation and couldnt find the information I
> need.
> 
> Appreciate your time.
> 
> cheers
> pj
> 
> 
> 
> 
> 
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 

-- 
Gavin Doughtie
DreamWorks SKG
(818) 695-3821





More information about the Cplusplus-sig mailing list