473,385 Members | 1,312 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,385 software developers and data experts.

Pre-Processing loop

Hi all ,

Suppose i have structurs defines as follows :
struct S1
{
int n1 ;
int n2 ;
int n3 ;
};

struct S2
{
int i1 ;
int i2 ;
int i3 ;
};

i need to copy each member from S1 to it's corresponding in S2 something like:

s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;

is there a way to create those line in a macro ???
what i need is something like :

#define COPY(n)
#if ( n != 0 )
copy n to i\
COPY(n-1) \ reccursive call
#endif

BTW : i'm using vc++ compiler (and preprocessor).

Thanks.
Jul 19 '05 #1
9 12290
"Itay" <it***@nur.com> wrote...
Suppose i have structurs defines as follows :
struct S1
{
int n1 ;
int n2 ;
int n3 ;
};

struct S2
{
int i1 ;
int i2 ;
int i3 ;
};

i need to copy each member from S1 to it's corresponding in S2 something like:
s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;

is there a way to create those line in a macro ???
what i need is something like :

#define COPY(n)
#if ( n != 0 )
copy n to i\
COPY(n-1) \ reccursive call
#endif


If the layouts of the two structures are really so much the same,
why don't you use some kind of low-end memcpy routine? If the
layouts are not the same (as to prohibit the use of memcpy), why
not have a simple function that would do the copying for you

void copyints(S1& s1, S2 const& s2) {
s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;
}

? Why do you need to involve a preprocessor at all?

And, no, AFAIK, there are no means in C++ preprocessor to make
recursive macro expansions that could stop based on its argument
value.

Victor
Jul 19 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ettrb.111902$275.317876@attbi_s53...
"Itay" <it***@nur.com> wrote...
Suppose i have structurs defines as follows :
struct S1
{
int n1 ;
int n2 ;
int n3 ;
};

struct S2
{
int i1 ;
int i2 ;
int i3 ;
};

i need to copy each member from S1 to it's corresponding in S2 something

like:

s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;

is there a way to create those line in a macro ???
what i need is something like :

#define COPY(n)
#if ( n != 0 )
copy n to i\
COPY(n-1) \ reccursive call
#endif


If the layouts of the two structures are really so much the same,
why don't you use some kind of low-end memcpy routine? If the
layouts are not the same (as to prohibit the use of memcpy), why
not have a simple function that would do the copying for you

void copyints(S1& s1, S2 const& s2) {
s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;
}

? Why do you need to involve a preprocessor at all?

And, no, AFAIK, there are no means in C++ preprocessor to make
recursive macro expansions that could stop based on its argument
value.


See the Boost Preprocessor Library at www.boost.org.

Jeff

Jul 19 '05 #3
"Jeff F" <Po**@ThisNewsGroup.com> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Ettrb.111902$275.317876@attbi_s53...
"Itay" <it***@nur.com> wrote...
Suppose i have structurs defines as follows :
struct S1
{
int n1 ;
int n2 ;
int n3 ;
};

struct S2
{
int i1 ;
int i2 ;
int i3 ;
};

i need to copy each member from S1 to it's corresponding in S2
something like:

s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;

is there a way to create those line in a macro ???
what i need is something like :

#define COPY(n)
#if ( n != 0 )
copy n to i\
COPY(n-1) \ reccursive call
#endif


If the layouts of the two structures are really so much the same,
why don't you use some kind of low-end memcpy routine? If the
layouts are not the same (as to prohibit the use of memcpy), why
not have a simple function that would do the copying for you

void copyints(S1& s1, S2 const& s2) {
s1.n1 = s2.i1 ;
s1.n2 = s2.i2 ;
s1.n3 = s2.i3 ;
}

? Why do you need to involve a preprocessor at all?

And, no, AFAIK, there are no means in C++ preprocessor to make
recursive macro expansions that could stop based on its argument
value.


See the Boost Preprocessor Library at www.boost.org.


So, what's the solution? Post it, don't tease.
Jul 19 '05 #4
Victor Bazarov wrote:
"Jeff F" <Po**@ThisNewsGroup.com> wrote...


[...]
And, no, AFAIK, there are no means in C++ preprocessor to make
recursive macro expansions that could stop based on its argument
value.


See the Boost Preprocessor Library at www.boost.org.


So, what's the solution? Post it, don't tease.


#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

#define COPY_M(z, v, _) \
s1.BOOST_PP_CAT(n, BOOST_PP_INC(v)) \
= s2.BOOST_PP_CAT(i, BOOST_PP_INC(v)); \
/**/
#define COPY(n) BOOST_PP_REPEAT(n, COPY_M, ~)

COPY(3)

Regards,
Paul Mensonides
Jul 19 '05 #5
"Paul Mensonides" <le******@comcast.net> wrote...
Victor Bazarov wrote:
"Jeff F" <Po**@ThisNewsGroup.com> wrote...


[...]
And, no, AFAIK, there are no means in C++ preprocessor to make
recursive macro expansions that could stop based on its argument
value.

See the Boost Preprocessor Library at www.boost.org.


So, what's the solution? Post it, don't tease.


#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

#define COPY_M(z, v, _) \
s1.BOOST_PP_CAT(n, BOOST_PP_INC(v)) \
= s2.BOOST_PP_CAT(i, BOOST_PP_INC(v)); \
/**/
#define COPY(n) BOOST_PP_REPEAT(n, COPY_M, ~)

COPY(3)

Interesting. Only I can't use it. My compiler doesn't come with
those headers. Any solution in terms of standard C++?

Thanks.

Jul 19 '05 #6
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:5iNrb.119681$9E1.586934@attbi_s52...
So, what's the solution? Post it, don't tease.


#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

#define COPY_M(z, v, _) \
s1.BOOST_PP_CAT(n, BOOST_PP_INC(v)) \
= s2.BOOST_PP_CAT(i, BOOST_PP_INC(v)); \
/**/
#define COPY(n) BOOST_PP_REPEAT(n, COPY_M, ~)

COPY(3)

Interesting. Only I can't use it. My compiler doesn't come with
those headers. Any solution in terms of standard C++?


Certainly. The following implements the scaffolding required to implement a
generic repetition construct, a generic repetititon construct, and then the use
of that construct to generate the assignments.

This is "standard C++" which means that it won't work on most preprocessors
(e.g. not VC, not EDG, not Sun, not IBM, not Borland, and not Metrowerks).
Preprocessors that are compliant enough include Wave, GCC, Borland's free
command line preprocessor (the non-integrated one), and probably Metrowerks' new
preprocessor available with their version 9 Macintosh compiler.

The following implementation can only repeat something nine times. In order to
increase that limit, you need to increase the number of DEC_x macros and the
number of EXPR_x macros such that they go as high as necessary. However, the
construct is multidimensional, and the invocations of the external macros are
trampolined. In other words, the first dimension can repeat nine elements, the
second dimension can repeat eight elements, the third dimension can repeat seven
elements, and so on.

// concatenation
#define CAT(a, b) PRIMITIVE_CAT(a, b)
#define PRIMITIVE_CAT(a, b) a ## b

// binary intermediate split
#define SPLIT(i, im) PRIMITIVE_CAT(SPLIT_, i)(im)
#define SPLIT_0(a, b) a
#define SPLIT_1(a, b) b

// saturating increment and decrement
#define DEC(x) SPLIT(0, PRIMITIVE_CAT(DEC_, x))
#define INC(x) SPLIT(1, PRIMITIVE_CAT(DEC_, x))

#define DEC_0 0, 1
#define DEC_1 0, 2
#define DEC_2 1, 3
#define DEC_3 2, 4
#define DEC_4 3, 5
#define DEC_5 4, 6
#define DEC_6 5, 7
#define DEC_7 6, 8
#define DEC_8 7, 9
#define DEC_9 8, 9

// bit complement
#define COMPL(bit) PRIMITIVE_CAT(COMPL_, bit)
#define COMPL_0 1
#define COMPL_1 0

// nullary parentheses detection
#define IS_NULLARY(x) SPLIT(0, CAT(IS_NULLARY_R_, IS_NULLARY_C x))
#define IS_NULLARY_C() 1
#define IS_NULLARY_R_1 1, ~
#define IS_NULLARY_R_IS_NULLARY_C 0, ~

// boolean conversion
#define BOOL(x) COMPL(IS_NULLARY(PRIMITIVE_CAT(BOOL_, x)))
#define BOOL_0 ()

// recursion backend
#define EXPR(s) PRIMITIVE_CAT(EXPR_, s)
#define EXPR_0(x) x
#define EXPR_1(x) x
#define EXPR_2(x) x
#define EXPR_3(x) x
#define EXPR_4(x) x
#define EXPR_5(x) x
#define EXPR_6(x) x
#define EXPR_7(x) x
#define EXPR_8(x) x
#define EXPR_9(x) x

// bit-oriented if control structure
#define IIF(bit) PRIMITIVE_CAT(IIF_, bit)
#define IIF_0(t, f) f
#define IIF_1(t, f) t

// number-oriented if control structure
#define IF(cond) IIF(BOOL(cond))

// emptiness abstraction
#define EMPTY()

// 1x and 2x deferral macros
#define DEFER(macro) macro EMPTY()
#define OBSTRUCT() DEFER(EMPTY)()

// argument list eater
#define EAT(size) PRIMITIVE_CAT(EAT_, size)
#define EAT_0()
#define EAT_1(a)
#define EAT_2(a, b)
#define EAT_3(a, b, c)
#define EAT_4(a, b, c, d)
#define EAT_5(a, b, c, d, e)
#define EAT_6(a, b, c, d, e, f)

// comma abstractions
#define COMMA() ,
#define COMMA_IF(n) IF(n)(COMMA, EMPTY)()

// repetition construct
#define REPEAT(s, count, macro, data) \
EXPR(s)(REPEAT_I(INC(s), INC(s), count, macro, data)) \
/**/
#define REPEAT_INDIRECT() REPEAT_I
#define REPEAT_I(s, o, count, macro, data) \
IF(count)(REPEAT_II, EAT(6))(OBSTRUCT(), s, o, DEC(count), macro, data) \
/**/
#define REPEAT_II(_, s, o, count, macro, data) \
EXPR(s) _(REPEAT_INDIRECT _()( \
INC(s), o, count, macro, data \
)) \
EXPR OBSTRUCT()(o)(macro OBSTRUCT()(o, count, data)) \
/**/

// original example
#define COPY_M(s, v, _) s1.CAT(n, INC(v)) = s2.CAT(i, INC(v));
#define COPY(n) EXPR(0)(REPEAT(0, n, COPY_M, ~))

COPY(3)

// multidimensional example (template template parameters)
#define FIXED(s, n, text) COMMA_IF(n) text
#define TTP(s, n, _) \
COMMA_IF(n) template<REPEAT(s, INC(n), FIXED, class)> class T ## n \
/**/
#define TEMPLATE_TEMPLATE(n) EXPR(0)(REPEAT(0, n, TTP, ~))

TEMPLATE_TEMPLATE(3)

Boost.Preprocessor cannot get away with this level of abstraction because it
requires a strictly conforming preprocessor. This is similar to the way Chaos
generalizes recursion. Most of the above are general purpose library facilities
(rather than the means to implement the trivial example in question). Hence, it
is better to just get Boost (though I realize that you were just being strictly
topical).

Regards,
Paul Mensonides
Jul 19 '05 #7
Whhoow ,
Thanks for the replies.
Actually i was looking for a much simplier solution.
I'm not familar with the boost libraries though i heard it good.
Can someone apply a simple solution to the simple sample i posted ???

Thanks.
Jul 19 '05 #8
it***@nur.com (Itay) wrote in message news:<31**************************@posting.google. com>...
Whhoow ,
Thanks for the replies.
Actually i was looking for a much simplier solution.
I'm not familar with the boost libraries though i heard it good.
Can someone apply a simple solution to the simple sample i posted ???

Thanks.


Victor posted a simple solution.
Jul 19 '05 #9
Dan Cernat wrote:
it***@nur.com (Itay) wrote in message
news:<31**************************@posting.google. com>...
Whhoow ,
Thanks for the replies.
Actually i was looking for a much simplier solution.
I'm not familar with the boost libraries though i heard it good.
Can someone apply a simple solution to the simple sample i posted ???

Thanks.


Victor posted a simple solution.


Yes, Victor's solution is the way to go in this case.

Regards,
Paul Mensonides
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
7
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
2
by: Buck Turgidson | last post by:
I want to have a css with 2 PRE styles, one bold with large font, and another non-bold and smaller font. I am new to CSS (and not exactly an expert in HTML, for that matter). Is there a way to...
5
by: Michael Shell | last post by:
Greetings, Consider the XHTML document attached at the end of this post. When viewed under Firefox 1.0.5 on Linux, highlighting and pasting (into a text editor) the <pre> tag listing will...
8
by: Jarno Suni not | last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
9
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
14
by: Schraalhans Keukenmeester | last post by:
I am building a default sheet for my linux-related pages. Since many linux users still rely on/prefer viewing textmode and unstyled content I try to stick to the correct html tags to pertain good...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.