[C++-sig] Re: boost::variant in Python interface

Colin Irwin colin_irwin at hotmail.com
Mon Mar 29 03:02:13 CEST 2004


Sorry for the incomplete send - let me try this again:


I have a C++ interface that I would like to make available in a
corresponding Python module.  The interface contains a member function that
returns a vector of boost::variants, with the variant being one of  three
possible types.  I've used the boost::python::vector_indexing_suite class to
wrap the vector up, so far so good.  However, the core of the problem is how
to wrap the variant type up.

To provide a more concrete example, I have provided a sample implementation
below:

class BaseData { ... };
class DataType1 : public BaseData { ... };
class DataType2 : public BaseData { ... };
class DataType3 : public BaseData { ... };

class DataProducer
{
    public:
        typedef boost::variant<DataType1, DataType2, DataType3> DataVariant;
        typedef std::vector<DataVariant> DataVector;

        void ReceiveData(DataVector& data_array)
        {
            // Return data in the user specified data_array parameter.
        }

        ...
}

I'm thinking it would be nice (and possible?) to somehow have the fact
hidden that there is a variant type involved.  By this I mean that the
various member data and member functions would simply either be available,
if the variant actually contained a type where these were available, or not
available, if the variant contained a type where these were not available.

What's the best approach to deal with this type of problem?  Has anyone
tried to use a boost::variant type through a Python interface?  Would it be
possible to wrap up the variant type is some way as to hide the
implementation, but still make functionality available?

Thanks in advance.

Colin







More information about the Cplusplus-sig mailing list