Objectizing C Code

R

Robby

Here's a brainteaser:

I have some legacy C code that I cannot modify for various reasons. I
would like to be able to wrap this code into a C++ class so that I can
create multiple objects to interact with.

To simplify, here's some code:

======================================================
foo.h (Can't modify):

#ifndef _FOO_H_
#define _FOO_H_

#if defined(TEST1) && defined(TEST2)
#error "Cannot define both TEST1 and TEST2"
#endif

#ifdef TEST1
#define OUTVAR 1
#elif defined(TEST2)
#define OUTVAR 2
#else
#error "Need TEST1 or TEST2"
#endif

extern int testInt;

void testFunc();

#endif

======================================================
======================================================
foo.c (Cannot modify):

#include "foo.h"
#include <stdio.h>

int testInt;

void testFunc() {
testInt = OUTVAR;
printf("%d\n", testInt);
}

======================================================

To complicate matters further, I would like to have two different
object types, one where TEST1 is defined, and one where TEST2 is
defined, and I would like for each object to have their own copy of
global variables (like testInt).

Thanks in advance for your help!
 
N

Noah Roberts

Robby said:
Here's a brainteaser:

I have some legacy C code that I cannot modify for various reasons. I
would like to be able to wrap this code into a C++ class so that I can
create multiple objects to interact with.

To simplify, here's some code:

======================================================
foo.h (Can't modify):

#ifndef _FOO_H_
#define _FOO_H_

#if defined(TEST1) && defined(TEST2)
#error "Cannot define both TEST1 and TEST2"
#endif

#ifdef TEST1
#define OUTVAR 1
#elif defined(TEST2)
#define OUTVAR 2
#else
#error "Need TEST1 or TEST2"
#endif

extern int testInt;

void testFunc();

#endif

======================================================
======================================================
foo.c (Cannot modify):

#include "foo.h"
#include <stdio.h>

int testInt;

void testFunc() {
testInt = OUTVAR;
printf("%d\n", testInt);
}

======================================================

To complicate matters further, I would like to have two different
object types, one where TEST1 is defined, and one where TEST2 is
defined, and I would like for each object to have their own copy of
global variables (like testInt).

Sorry, you're screwed.

It's worse than having globals you need to work with...you have defines.
These can't be changed. There is absolutely no way to do what you
want to do. If outvar was a variable there might be some chance and you
could set/override the global for each instance pre/post wrapper
call...but with it being a define there's no hope.

Sucks to be you.
 
R

Robby

Sorry, you're screwed.

It's worse than having globals you need to work with...you have defines.
These can't be changed. There is absolutely no way to do what you
want to do. If outvar was a variable there might be some chance and you
could set/override the global for each instance pre/post wrapper
call...but with it being a define there's no hope.

Sucks to be you.

Let's pretend the defines aren't there for a second then. I think I
can get around the defines with two header files that #define and
#undef what I need, and then put them in separate namespaces, like so:

======================================================
t1.h (can modify):
#ifndef _T1_H_
#define _T1_H_

#undef TEST2
#define TEST1

namespace T1 {
#include "foo.c"
}

#endif
======================================================
======================================================
t2.h (can modify):
#ifndef _T2_H_
#define _T2_H_

#undef TEST1
#define TEST2

namespace T2 {
#include "foo.c"
}

#endif
======================================================

I know this is very ugly, but I think it gets around the defines.
Now, how would I get around the global vars (especially with those
externs) and wrap all of this in an object? Or am I really screwed?

FYI - What I'm trying to do is take existing C firmware code, wrap it
up, and place several instances in a simulator. I have a hard time
believing this can't be done.
 
M

Michael DOUBEZ

Robby a écrit :
Let's pretend the defines aren't there for a second then. I think I
can get around the defines with two header files that #define and
#undef what I need, and then put them in separate namespaces, like so:

======================================================
t1.h (can modify):
#ifndef _T1_H_
#define _T1_H_

#undef TEST2
#define TEST1

namespace T1 {
#include "foo.c"
}

#endif
======================================================
======================================================
t2.h (can modify):
#ifndef _T2_H_
#define _T2_H_

#undef TEST1
#define TEST2

namespace T2 {
#include "foo.c"
}

#endif
======================================================

I know this is very ugly, but I think it gets around the defines.
Now, how would I get around the global vars (especially with those
externs) and wrap all of this in an object? Or am I really screwed?

FYI - What I'm trying to do is take existing C firmware code, wrap it
up, and place several instances in a simulator. I have a hard time
believing this can't be done.

Instead of putting cpp file inclision in header file, do it in code file
and then just declare the interface in separate header. That way globals
are compiled in different namespace.

Then: transform above files t1.h into t1.cpp, t2.h into t2.cpp then
declare test.h:
======================================================
namespace T1 {
void testFunc();
}

namespace T2 {
void testFunc();
}
======================================================

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top