Using different header files and selecting conditionally

I

Ishmagel

I have two header files with almost identical information. These
header files represents different versions of a data feed

When i am processing a feed, i need to choose in runtime which structs
from which header file to use. I have been thinking of using
namespaces:

**file ver1 and ver2**
struct mystruct {
int i;
} t;
**EOF**

namespace ver_1
{
#include "ver1.hpp";
}

namespace ver_2
{
#include "ver2.hpp";
}

void test()
{
if(useNew)
using namespace ver_1;
else
using namespace ver_2;

t.i = 5;
}

This wont work, since conditional namespace is not valid. What is the
best way of doing this? Bear in mind that in the future, new versions
may appear, so creating a test() for each is not an option

Hope you can help

Kind Regards

-Kenneth
 
R

Rolf Magnus

Ishmagel said:
I have two header files with almost identical information. These
header files represents different versions of a data feed

When i am processing a feed, i need to choose in runtime which structs
from which header file to use. I have been thinking of using
namespaces:

**file ver1 and ver2**
struct mystruct {
int i;
} t;
**EOF**

namespace ver_1
{
#include "ver1.hpp";
}

namespace ver_2
{
#include "ver2.hpp";
}

void test()
{
if(useNew)
using namespace ver_1;
else
using namespace ver_2;

t.i = 5;
}

This wont work, since conditional namespace is not valid.

namespaces are a compile time concept. They don't exist at runtime.
What is the best way of doing this?

Polymorphism.
 
J

James Kanze

I have two header files with almost identical information. These
header files represents different versions of a data feed
When i am processing a feed, i need to choose in runtime which
structs from which header file to use. I have been thinking of
using namespaces:
**file ver1 and ver2**
struct mystruct {
int i;} t;

namespace ver_1
{
#include "ver1.hpp";
}
namespace ver_2
{
#include "ver2.hpp";
}
void test()
{
if(useNew)
using namespace ver_1;
else
using namespace ver_2;
t.i = 5;
}
This wont work, since conditional namespace is not valid. What
is the best way of doing this? Bear in mind that in the
future, new versions may appear, so creating a test() for each
is not an option

First, you should define an interface (an abstract class) to the
data, which is common to all versions. Then implement it for
each actual version, putting the implementation in a dynamically
linked object (.dll or .so, depending on the system). At
runtime, test which one to use, and load the appropriate object
(dlopen under Unix, LoadLibrary under Windows).
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top