By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Objectizing C Code

P: n/a
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!

Feb 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Robby wrote:
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.
Feb 26 '07 #2

P: n/a
>
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.

Feb 26 '07 #3

P: n/a
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
Feb 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.