473,626 Members | 3,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Purpose of #define inside struct definition?


Following is a snippet of a header file.
Here, #define are inside struct evConn{}

Any advantage of putting them inside struct block?
Looks like there is no diff in scoping of each identifier between
inside and outside struct block....

typedef struct evConn {
evConnFunc func;
void * uap;
int fd;
int flags;
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTE D 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */
evFileID file;
struct evConn * prev;
struct evConn * next;
} evConn;

May 8 '07 #1
12 4631
dj****@gmail.co m wrote:
Following is a snippet of a header file.
Here, #define are inside struct evConn{}

Any advantage of putting them inside struct block?
Probably written this way to show the flags are associated with the
flags member of the struct.
Looks like there is no diff in scoping of each identifier between
inside and outside struct block....

typedef struct evConn {
evConnFunc func;
void * uap;
int fd;
int flags;
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTE D 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */
evFileID file;
struct evConn * prev;
struct evConn * next;
} evConn;

--
Ian Collins.
May 8 '07 #2
dj****@gmail.co m wrote:
Any advantage of putting them inside struct block?
What Ian said...
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTE D 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */
Just from a style perspective, I might personally prefer these to be

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */

to clearly indicate that they are bitmask flags, but I imagine this
isn't your code anyway - just a thought. (And to see how silly an
idea others feel this is.)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
May 8 '07 #3
On 8 May, 15:45, Christopher Benson-Manica <a...@otaku.fre eshell.org>
wrote:
#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTE D 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */

Just from a style perspective, I might personally prefer these to be

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */

to clearly indicate that they are bitmask flags, but I imagine this
isn't your code anyway - just a thought. (And to see how silly an
idea others feel this is.)
Hmmm. I'm not convinced ...

If you've got enough experience to recognize bitmask flags, you
probably recognize that powers of 2 map to them...

I find your shift-base values look a little "clunky", but that's just
my 2p worth.

May 8 '07 #4
Christopher Benson-Manica wrote:
dj****@gmail.co m wrote:
[...]
>#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTE D 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */

Just from a style perspective, I might personally prefer these to be

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */

to clearly indicate that they are bitmask flags, but I imagine this
isn't your code anyway - just a thought. (And to see how silly an
idea others feel this is.)
Not silly at all! Very readable. Though, when I read "0x0004" I see
the binary value anyway, so it is less of a magic number for me. Must
have been all those base conversions I was forced to do by hand in school.

Since it is hidden behind a #define, one doesn't get the immediate sense
of "this bit over there" anyway, so I'd still have to read the
definition and remember that this define meant that bit pattern.
--
clvrmnky

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
May 8 '07 #5
On Tue, 8 May 2007 14:45:35 +0000 (UTC), Christopher Benson-Manica
<at***@otaku.fr eeshell.orgwrot e:
>dj****@gmail.c om wrote:
>Any advantage of putting them inside struct block?

What Ian said...
>#define EV_CONN_LISTEN 0x0001 /* Connection is a listener. */
#define EV_CONN_SELECTE D 0x0002 /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK 0x0004 /* Listener fd was blocking. */

Just from a style perspective, I might personally prefer these to be

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */

to clearly indicate that they are bitmask flags, but I imagine this
isn't your code anyway - just a thought. (And to see how silly an
idea others feel this is.)
I wouldn't call it silly, but I wouldn't do it. Anyone who works with
bitmasks has the patterns embedded in their brain anyway. I find your
version somewhat less readable.

--
Al Balmer
Sun City, AZ
May 8 '07 #6
Al Balmer <al******@att.n etwrote:
On Tue, 8 May 2007 14:45:35 +0000 (UTC), Christopher Benson-Manica

#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */
I wouldn't call it silly, but I wouldn't do it. Anyone who works with
bitmasks has the patterns embedded in their brain anyway. I find your
version somewhat less readable.
At a place I used to work, the left-shift-X metaphor was widespread,
so I suppose I simply got used to the style. Judging by the other
responses, it seems that style was more idiosyncratic than I realized
at the time.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
May 8 '07 #7
Christopher Benson-Manica <at***@otaku.fr eeshell.orgwrot e:
Al Balmer <al******@att.n etwrote:
On Tue, 8 May 2007 14:45:35 +0000 (UTC), Christopher Benson-Manica
>
>#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
>#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
>#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */
I wouldn't call it silly, but I wouldn't do it. Anyone who works with
bitmasks has the patterns embedded in their brain anyway. I find your
version somewhat less readable.
At a place I used to work, the left-shift-X metaphor was widespread,
so I suppose I simply got used to the style. Judging by the other
responses, it seems that style was more idiosyncratic than I realized
at the time.
But there are also cases were the left-shift-X way can help avoid
mistakes. If you have e.g. a 32-but wide hardware register which
says "Bit 23: set this bit to enable interrupts" then it tends to
be simpler to just use the number from the documentation and write

#define IRQ_ENABLE ( 1 << 23 )

than to calculate which hex number that corresponds to.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
May 8 '07 #8
In article <5a************ *@mid.uni-berlin.de>,
Jens Thoms Toerring <jt@toerring.de wrote:
>But there are also cases were the left-shift-X way can help avoid
mistakes. If you have e.g. a 32-but wide hardware register which
says "Bit 23: set this bit to enable interrupts" then it tends to
be simpler to just use the number from the documentation and write
>#define IRQ_ENABLE ( 1 << 23 )
>than to calculate which hex number that corresponds to.
True -- but still error prone if the bits are numbered the other way,
or there are padding bits to worry about.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
May 8 '07 #9
On May 8, 9:53 am, mark_blue...@po box.com wrote:
On 8 May, 15:45, Christopher Benson-Manica <a...@otaku.fre eshell.org>
wrote:
[...]
Just from a style perspective, I might personally prefer these to be
#define EV_CONN_LISTEN ( 0x0001 << 0 ) /* Connection is a listener. */
#define EV_CONN_SELECTE D ( 0x0001 << 1 ) /* evSelectFD(conn->file). */
#define EV_CONN_BLOCK ( 0x0001 << 2 ) /* Listener fd was blocking. */
to clearly indicate that they are bitmask flags, but I imagine this
isn't your code anyway - just a thought. (And to see how silly an
idea others feel this is.)

Hmmm. I'm not convinced ...

If you've got enough experience to recognize bitmask flags, you
probably recognize that powers of 2 map to them...

I find your shift-base values look a little "clunky", but that's just
my 2p worth.
I have a utility macro that I use in several projects that looks
something like this:

#define mBit(b) (1U<<(b))

So the above would become

#define EV_CONN_LISTEN mBit(0)) /* Connection is a
listener. */
#define EV_CONN_SELECTE D mBit(1)) /* evSelectFD(conn-
>file). */
#define EV_CONN_BLOCK mBit(2) /* Listener fd was
blocking. */

Fairly self-documenting, and a little less clunky-looking. IMHO,
anyway.

Regards,

-=Dave

May 8 '07 #10

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

Similar topics

2
5957
by: Grumble | last post by:
Hello all, I came across the following definition (or is it declaration? I never know): struct OFFSET_AND_SIZE { INT64 offset; UINT64 size; OFFSET_AND_SIZE (INT64 o, UINT64 s) : offset (o), size (s) {}
3
1602
by: james545 | last post by:
I saw some code written where member functions were written inside struct definition blocks and was wondering if this is an advisable or common style of programming in C/C++? The type of code I saw compiled under the g++ compiler and looked like this: typedef struct A { int x; int y;
42
5599
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
3
1323
by: Fernando Barsoba | last post by:
Hi all, I'm trying to reassign the address of a pointer inside a function and return it once the function has done its things... However, the address returned is not the address I wanted to return.. let me explain it better with the actual example: caller function: > struct authenHeader * AH;
10
1493
by: Ben | last post by:
Hello, Is it possible to define a struct that has a type of itself in it? So is it possible and if so how to do what I'm trying to do here?: typedef struct mystruct { int a; mystruct *b; } mystruct;
4
2429
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64 c1_syn, u64 c2_syn, u64 c_stat, u64 c_sts, int print) { char *sourcename = { "UNKNOWN", "UNKNOWN", "UNKNOWN", "MEMORY", "BCACHE", "DCACHE",
4
2202
by: _mario.lat | last post by:
for struct: struct in6_addr { uint8_t s6_addr; }; is provided a costant: #define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}} what does means {{{, and }}}?
2
1545
by: parag_paul | last post by:
Isnt is so that whenever the compiler reads an #def it will replace it with letter by letter on the place is see the macro, so what is the purpose of keeping #defs inside a structure, struct A{ #define A_list(e) (e)->list() }
1
2498
by: Lambda | last post by:
I'd like to define a std::map: map<struct term, vector<size_t. The definition of the structure is: struct term { string word; size_t freq; }; the freq is the size() of vector<size_t>.
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.