473,414 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Objectizing C Code

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
3 1292
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
16
by: Dario de Judicibus | last post by:
I'm getting crazy. Look at this code: #include <string.h> #include <stdio.h> #include <iostream.h> using namespace std ; char ini_code = {0xFF, 0xFE} ; char line_sep = {0x20, 0x28} ;
109
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
37
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.