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?

Tyranicangel
  • 189
  • 2
  • 16
  • Post this in the question itself – asheeshr Nov 29 '12 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 http://www.cython.org/ could help you? – Andrei Sosnin Dec 11 '12 at 07: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 – Tyranicangel Dec 12 '12 at 09:42

2 Answers2

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.

Nathan Binkert
  • 8,744
  • 1
  • 29
  • 37
  • Currently in the Py script i need only the function getElapsedtime().Is there any possibility to retrieve it without completely converting the timer Object – Tyranicangel Dec 14 '12 at 11:26
  • If you only need the one function, you can create a CObject http://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). – Nathan Binkert Dec 14 '12 at 17:53
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

Community
  • 1
  • 1
Amit
  • 677
  • 8
  • 15