#define a macro that #includes

R

.rhavin grobert

first of all: sorry for perhaps posting something already answered
anywhere, i couldnt find any useful info - define, macro, include and
so on are very poor keywords;-)

my problem:

i want to do something like...

INCLUDE_TYPES(#include);

....that resolves to ...

#include "file1.h"
#include "file2.h"
#include "file3.h"


.... and my try was a...

#define INCLUDE_TYPES(Q_L) \
Q_L "file1.h" \
Q_L "file2.h" \
Q_L "file3.h" \

....but my compiler complains about...

'#' : invalid character : possibly the result of a macro expansion

....so that way dont work. i cant do a simple...

#include "files.h"

....with all other includes in that file so im looking for some
different idea and would appreciate any useful help.

TIA, -.rhavin;-)
 
J

jxh

my problem:

i want to do something like...

INCLUDE_TYPES(#include);

...that resolves to ...

#include "file1.h"
#include "file2.h"
#include "file3.h"

... and my try was a...

#define INCLUDE_TYPES(Q_L) \
Q_L "file1.h" \
Q_L "file2.h" \
Q_L "file3.h" \

...but my compiler complains about...

 '#' : invalid character : possibly the result of a macro
expansion

...so that way dont work.

Right. You can't expand a preprocessor macro into a
preprocessor directive. You can use the presence of
a macro to determine whether or not to include your
header files by:

#ifdef INCLUDE_TYPES
# include "file1.h"
# include "file2.h"
# include "file3.h"
#endif
i cant do a simple...

#include "files.h"

...with all other includes in that file so im looking for some
different idea and would appreciate any useful help.

You need to explain why you can't do that.

-- James
 
H

Harald van Dijk

first of all: sorry for perhaps posting something already answered
anywhere, i couldnt find any useful info - define, macro, include and so
on are very poor keywords;-)

my problem:

i want to do something like...

INCLUDE_TYPES(#include);

...that resolves to ...

#include "file1.h"
#include "file2.h"
#include "file3.h"

That's not possible in C. Macro expansions cannot ever generate
preprocessing directives.
... and my try was a...

#define INCLUDE_TYPES(Q_L) \
Q_L "file1.h" \
Q_L "file2.h" \
Q_L "file3.h" \

...but my compiler complains about...

'#' : invalid character : possibly the result of a macro expansion

Right. Since #include doesn't appear at the start of a line in your code,
it's not a directive, and the # is left and causes syntax errors.
...so that way dont work. i cant do a simple...

#include "files.h"

...with all other includes in that file so im looking for some different
idea and would appreciate any useful help.

Why can't you do that? If you have a problem you want to solve, ask about
that problem. Your attempted solution doesn't work and can't be made to
work. Asking how to change that solution so that it will work won't get
you useful answers. Asking how to solve your original problem may.
 
R

.rhavin grobert

Right. You can't expand a preprocessor macro into a
preprocessor directive. You can use the presence of
a macro to determine whether or not to include your
header files by:
#ifdef INCLUDE_TYPES
# include "file1.h"
# include "file2.h"
# include "file3.h"
#endif
You need to explain why you can't do that.

i have a pack that consists of 3 different classes meant for
deriviation by the client (yes, thats c++)

they client may specify differnt additional classes and they shall be
included, so i created a file that is a headerfile and consists of
certain macros allowing the client to modify the package. and wantet
to find a solution to have it all in one configuration file, one part
of the config file looks for example like this:
____________________________________________


//-----------------------------------------------------------------------------
// list types' corresponding info-classes and file-classes
// usage: Q_T([FILETYPE]) Q_I([Header-Class]) Q_F([File-Class]) Q_B
// escape EOLs (lineends) to have a single #define-line
#define QFILEMANAGER_TYPECLASSES(Q_T, Q_I, Q_F, Q_B) \
Q_T(FZT_RECORD) Q_I(CInfRec) Q_F(CFZRecord) Q_B \
Q_T(FZT_POOL) Q_I(CInfPool) Q_F(CFZPool) Q_B \
Q_T(FZT_DEFN) Q_I(CInfDef) Q_F(CFZDef) Q_B \
Q_T(FZT_BASE) Q_I(CInfBase) Q_F(CFZBase) Q_B \
Q_T(FZT_LOCAL) Q_I(CInfLocal) Q_F(CFZLocal) Q_B \
Q_T(FZT_STUDY) Q_I(CInfStudy) Q_F(CFZStudy) Q_B \

____________________________________________

in my pack i have the following definitions

#define QFM_NOP(s)
#define QFM_EMPTY
#define QFM_CASE(s) case s:
#define QFM_NEW(s) new s; break;
#define QFM_DELETE(s) delete (s*)
#define QFM_BREAK ;break;

....by now 'calling' the above macro with the following code in my
class...

switch (type) {
QFILEMANAGER_TYPECLASSES(QFM_CASE, QFM_NOP, pFile = QFM_NEW,
QFM_EMPTY);
default:
//
}

....i get my allocator and by calling it that way ...

switch (pInfo->TypeGet()) {
QFILEMANAGER_TYPECLASSES(QFM_CASE, QFM_DELETE, QFM_NOP, pInfo
QFM_BREAK);
default:
return false;
}

....i delocate (correct word?) it. But that definition file is included
in many files in the pack where i dont want to have the #include-
sections, so im looking for a way to have a....

//-------------------------------------
// put yout file pathes here
MYMACRO(somewhat) \
"filepath" \
"filepath" \

....and decide in every file where i include the user-config-header if
i want to also include his specified files. the...

#ifdef INCLUDE_TYPES
# include "file1.h"
# include "file2.h"
# include "file3.h"
#endif

....seems to be a possibility, so'll try that, thx, .rhavin
 
B

Ben Bacarisse

.rhavin grobert said:
first of all: sorry for perhaps posting something already answered
anywhere, i couldnt find any useful info - define, macro, include and
so on are very poor keywords;-)

my problem:

i want to do something like...

INCLUDE_TYPES(#include);

...that resolves to ...

#include "file1.h"
#include "file2.h"
#include "file3.h"


... and my try was a...

#define INCLUDE_TYPES(Q_L) \
Q_L "file1.h" \
Q_L "file2.h" \
Q_L "file3.h" \

...but my compiler complains about...

'#' : invalid character : possibly the result of a macro expansion

The result of a macro expansion can't be a pre-processing directive --
no matter how much it looks like one. No macro can generate a
#include, #define etc. You can generate those tokens, but they are
not treated as directives.
...so that way dont work. i cant do a simple...

#include "files.h"

...with all other includes in that file so im looking for some
different idea and would appreciate any useful help.

Backup a bit. What problem were you trying to solve?
 
J

jxh

i have a pack that consists of 3 different classes meant for
deriviation by the client (yes, thats c++)

they client may specify differnt additional classes and they shall be
included, so i created a file that is a headerfile and consists of
certain macros allowing the client to modify the package.
[...]

It doesn't seem to me you are leveraging the full capabilities of C++.
If you describe your general architecture problem of accessing user
defined helper classes from your library's base class, I am sure you
would get good answers from comp.lang.c++.

-- James
 
P

Peter Nilsson

[email protected] (Walter Roberson) said:
No macro can expand to a preprocessor directive.

True, but preprocessing directives, including #include,
can expand to macros.

It's possible to do something like what the OP wants
(upto a static limit), but it's questionable as to
whether it's worth doing. Since the OP has admitted
to using C++, it's more likely there are better C++
paradigms to achieve what they really need to do.

But there is a Boost header that does many surprising
pre-processor tricks, and is both C and C++ compatible.
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top