4

I have a two c++ objects

    int queueIndex
    Timer timer_obj

and here is the definition of the timer class

class Timer
{

private:
    struct timeval tStart; /**< @brief Stores the system time when the timer is started */
    struct timeval tFinish; /**< @brief Stores the system time when the timer is stopped */

protected:
    float getElapsedTime(struct timeval, struct timeval);

public:
    Timer();
    string currentTime();
    float currentElapsedTime();
    void restart();
    float stop();
    int high_pres_usleep_untill(unsigned long long int);
    string getStringStartTimer();
    void setStartTimerFromString(string);
    void sleep(long int);
    unsigned long long int get_clock();

I need to convert these two objects into PyObj*.I was using boost::python for this purpose.I now wish to do it without boost or swig. I successfully converted Index but im clueless with the timer_obj

    PyObject* pyo=PyInt_FromLong(queueIndex)

I would be thankful if someone could help me with this.I just need an example you dont need to give me the complete code.Also the functions defined in the class are a bit complicated so I did not give them here.Is it possible to convert the timer_obj and then assign a new reference or do i have to obtain new reference for all the structs and functions in the class and create a new class with these references?

3
  • Post this in the question itself Commented Nov 29, 2012 at 12:49
  • What exactly are you trying to accomplish by 'converting the C++ objects'? boost::python is meant for interoperability, so I assume you want to make a C++ application "extensible" with Python code, right? Maybe cython.org could help you? Commented Dec 11, 2012 at 7:29
  • Well i am working on client to service platform written in c++ and the logic the service has to implement in written in python.Until now i have been using boost but recently i have been asked to change the interpreter method where i can't use boost anymore Commented Dec 12, 2012 at 9:42

2 Answers 2

2

What you need to do is create an extension type. Unfortunately, the process is relatively involved, and will take a couple hundred lines of code. That's the whole python of boost::python and SWIG, to remove all the boilerplate.

Sign up to request clarification or add additional context in comments.

2 Comments

Currently in the Py script i need only the function getElapsedtime().Is there any possibility to retrieve it without completely converting the timer Object
If you only need the one function, you can create a CObject docs.python.org/2/c-api/cobject.html. That will basically let you pass an opaque void pointer to python. You can then expose getElapsedTime() as a function that takes your CObject as a parameter. (You'll also need to create a function that generates those CObjects somehow so you can pass them to python).
1

As Nathan said, extension type is relatively involved. You would like to explore Cython for this as syntax is easier and it takes care of generating boilerplate internally. http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html

You also can take a look at example posted by Niklas in https://stackoverflow.com/a/8933819/2156678

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.