Click here to Skip to main content
15,903,012 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: It's horrible Pin
jamie55010-Jul-08 4:57
jamie55010-Jul-08 4:57 
GeneralRe: It's horrible Pin
BillW332-Sep-08 11:22
professionalBillW332-Sep-08 11:22 
GeneralRe: It's horrible Pin
Dan Neely10-Jul-08 5:43
Dan Neely10-Jul-08 5:43 
GeneralRe: It's horrible Pin
BadKarma10-Jul-08 21:20
BadKarma10-Jul-08 21:20 
GeneralRe: It's horrible Pin
Dan Neely11-Jul-08 4:22
Dan Neely11-Jul-08 4:22 
GeneralRe: It's horrible Pin
Rage11-Jul-08 4:59
professionalRage11-Jul-08 4:59 
GeneralRe: It's horrible Pin
Philippe Lhoste11-Jul-08 11:08
Philippe Lhoste11-Jul-08 11:08 
QuestionC++ wrappers for C struct api Pin
Kishore Jonnalagadda9-Jul-08 6:22
Kishore Jonnalagadda9-Jul-08 6:22 
Hi all,

I am new to this forum and generally any programming help forum for that matter.

I have a case where i want to maintain both C and C++ api library. So therefore i choose to make my C++ support a thin wrapper around the C code. However, i do not know what is the most effective way to achieve this without unnecessary memory and performance overhead. Below is an example case.

/*Begin Code Sample*/

/*Start time.h*/

struct Time
{
int seconds,
int nanoseconds
};

void timeAdd(struct Time* time, struct Time* delta);
void timeSub(struct Time* time, struct Time* delta);

/*End time.h*/

/*Start timer.h*/

struct Timer
{
struct Time time;
void (*callback)(struct Timer *timer);
};

void timerSetCallback(struct Timer *timer, void (*callback)(struct Timer *timer));
void timerStart(struct Timer *timer);
void timerStop(struct Timer *timer);

/*End timer.h*/

/*Start time.hxx*/

namespace C
{
#include "time.h"
}

class Time
{
public:
Time()
{
m_time.seconds = 0;
m_time.nanoseconds = 0;
};
~Time(){};
int seconds(){return m_time.seconds;}
int nanoseconds(){return m_time.nanoseconds;}
void setSeconds(int seconds){m_time.seconds = seconds};
void setNanoseconds(int nanoseconds){m_time.nanoseconds = nanoseconds};
void add(Time &time){C::timeAdd(&m_time, &time.m_time);};
void sub(Time &time){C::timeSub(&m_time, &time.m_time);};
protected:
C::Time m_time;
};

/*End time.hxx*/

/*Start timer.hxx*/
#include "timer.h"

class Timer : public Time
{
public:
Timer():Time(){m_timer.callback = &Timer::callback;};
~Timer(){C::timerStop(&m_timer);};

start(){m_timer.time = m_time; C::timerStart(&m_timer);};
stop(){C::timerStop(&m_timer);};
protected:
static void callback(struct Timer *timer);
C::Timer m_timer;
};


/*End Code Sample*/

Now while this works for this simple case, it has a few drawbacks like the additional memory used to allocate m_time in class Time when all of struct Time api's would have even worked for struct Timer and hence struct Time m_time could have been "replaced" by struct Timer m_time but instead both exist in class Timer.

It also is the simpler of the cases where the struct sizes are relatively small and the call to Timer::start() was easily managed by a copy of the struct.

So now, my question is how can c++ wrappers be written for such cases without the additional memory overhead? Given their similarity, I definitely want class Timer to be a subclass of class Time!

However, i do have the flexibility to modify both the C and C++ code but i need both the C and C++ code to be equally capable, clean and efficient. How can i do it?
AnswerRe: C++ wrappers for C struct api PinPopular
MidwestLimey9-Jul-08 6:26
professionalMidwestLimey9-Jul-08 6:26 
AnswerRe: C++ wrappers for C struct api Pin
Dave Kreskowiak9-Jul-08 7:09
mveDave Kreskowiak9-Jul-08 7:09 
JokeRe: C++ wrappers for C struct api Pin
jayart10-Jul-08 0:24
jayart10-Jul-08 0:24 
AnswerRe: C++ wrappers for C struct api Pin
Paul Conrad9-Jul-08 18:56
professionalPaul Conrad9-Jul-08 18:56 
AnswerRe: C++ wrappers for C struct api Pin
Kishore Jonnalagadda9-Jul-08 19:05
Kishore Jonnalagadda9-Jul-08 19:05 
GeneralA Coding Horror from a subcontracted project... Pin
CARPETBURNER3-Jul-08 22:47
CARPETBURNER3-Jul-08 22:47 
GeneralRe: A Coding Horror from a subcontracted project... Pin
Rob Grainger4-Jul-08 1:34
Rob Grainger4-Jul-08 1:34 
GeneralRe: A Coding Horror from a subcontracted project... [modified] PinPopular
CPallini4-Jul-08 1:55
mveCPallini4-Jul-08 1:55 
GeneralRe: A Coding Horror from a subcontracted project... PinPopular
PIEBALDconsult4-Jul-08 4:03
mvePIEBALDconsult4-Jul-08 4:03 
GeneralRe: A Coding Horror from a subcontracted project... PinPopular
CARPETBURNER4-Jul-08 10:17
CARPETBURNER4-Jul-08 10:17 
GeneralRe: A Coding Horror from a subcontracted project... PinPopular
PIEBALDconsult5-Jul-08 6:13
mvePIEBALDconsult5-Jul-08 6:13 
GeneralRe: A Coding Horror from a subcontracted project... Pin
Stephen Hewitt6-Jul-08 16:12
Stephen Hewitt6-Jul-08 16:12 
GeneralRe: A Coding Horror from a subcontracted project... Pin
PIEBALDconsult7-Jul-08 13:38
mvePIEBALDconsult7-Jul-08 13:38 
GeneralRe: A Coding Horror from a subcontracted project... Pin
GuyThiebaut8-Jul-08 9:12
professionalGuyThiebaut8-Jul-08 9:12 
GeneralRe: A Coding Horror from a subcontracted project... Pin
Rob Grainger9-Jul-08 23:48
Rob Grainger9-Jul-08 23:48 
GeneralRe: A Coding Horror from a subcontracted project... Pin
GuyThiebaut10-Jul-08 0:17
professionalGuyThiebaut10-Jul-08 0:17 
GeneralRe: A Coding Horror from a subcontracted project... Pin
sucram10-Jul-08 0:34
sucram10-Jul-08 0:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.