473,770 Members | 6,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Globally unique enums or macro values?

Hi
Does anyone know if there is a way to define what is effectively a
single globally visible, enumerated list whilst actually defining the
entries across several different modules?
or somehow do a similar thing with macros.

Details:
I have a c project to fit into a small microprocessor and need to save
some ram. I have a significant number of flags all over the place that
currently use whole byte storage. I thought if I had a way to define a
list of numerically sequential flag names, I could assign a single
chunk of ram and a simple function to access the relevant flags as
single bits. I need to be able to define the different flag names
within their relevent modules because some modules may be removed at
compile time using pre-processor directives.
This code is subject to continuous updating and I just can't find a
bomb proof way of ensuring the values are unique and sequential with no
gaps.

Any ideas?
Thanks
Cliff

May 9 '06 #1
9 2789
us******@btconn ect.com wrote:
Hi
Does anyone know if there is a way to define what is effectively a
single globally visible, enumerated list whilst actually defining the
entries across several different modules?
or somehow do a similar thing with macros.

Details:
I have a c project to fit into a small microprocessor and need to save
some ram. I have a significant number of flags all over the place that
currently use whole byte storage. I thought if I had a way to define a
list of numerically sequential flag names, I could assign a single
chunk of ram and a simple function to access the relevant flags as
single bits. I need to be able to define the different flag names
within their relevent modules because some modules may be removed at
compile time using pre-processor directives.
This code is subject to continuous updating and I just can't find a
bomb proof way of ensuring the values are unique and sequential with no
gaps.

Could you use something like

struct flags
{
uint8_t flag1 : 1;
uint8_t flag2 : 1;
uint8_t flag3 : 1;
uint8_t flag4 : 1;
uint8_t flag5 : 1;
};

--
Ian Collins.
May 9 '06 #2
Thanks Ian. I thought of that, but the problem here is that as well as
knowing the flag name I also have to remember which structure i put it
in.
Thanks

May 9 '06 #3
Ian Collins <ia******@hotma il.com> writes:
us******@btconn ect.com wrote:
Does anyone know if there is a way to define what is effectively a
single globally visible, enumerated list whilst actually defining the
entries across several different modules?
or somehow do a similar thing with macros.

Details:
I have a c project to fit into a small microprocessor and need to save
some ram. I have a significant number of flags all over the place that
currently use whole byte storage. I thought if I had a way to define a
list of numerically sequential flag names, I could assign a single
chunk of ram and a simple function to access the relevant flags as
single bits. I need to be able to define the different flag names
within their relevent modules because some modules may be removed at
compile time using pre-processor directives.
This code is subject to continuous updating and I just can't find a
bomb proof way of ensuring the values are unique and sequential with no
gaps.

Could you use something like

struct flags
{
uint8_t flag1 : 1;
uint8_t flag2 : 1;
uint8_t flag3 : 1;
uint8_t flag4 : 1;
uint8_t flag5 : 1;
};


The only portable types for bit fields are int, signed int, unsigned
int, and (C99 only) _Bool.

Also, it's likely that accessing a bit field requires more code than
accessing a whole byte. Any savings in data space by packing multiple
flags int a byte might be lost in the increase in code size. (If code
and data are in separate address spaces, the tradeoff is more
complex.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 9 '06 #4
Code space (flash memory) is quite large at 64K, its the ram that's
precious, a meagre 1K. If I could clump the flags together in a single
block then the code to access them would be fairly trivial. Probably
do it as a macro something like (untested):

#define GetFlag(Flag) (FlagRam[(Flag>>3)] & (1<<(Flag&7)) ? 1 : 0

If only I could define those flags :-(

May 10 '06 #5
us******@btconn ect.com wrote:
Thanks Ian. I thought of that, but the problem here is that as well as
knowing the flag name I also have to remember which structure i put it
in.


You could put them all in the same one!

--
Ian Collins.
May 10 '06 #6
us******@btconn ect.com wrote:
Code space (flash memory) is quite large at 64K, its the ram that's
precious, a meagre 1K. If I could clump the flags together in a single
block then the code to access them would be fairly trivial. Probably
do it as a macro something like (untested):

#define GetFlag(Flag) (FlagRam[(Flag>>3)] & (1<<(Flag&7)) ? 1 : 0


You could use macros such as that or write access functions which would
be smaller, but slower.

First, some compilers, specifically for 8051, which supports bit access,
has a separate data type for a bit variable, which the linker will
allocate for all the linked modules. The 8051 is limited to 128 bits
for direct bit access. Other compiler/linker implementations probably
don't have this level of support for bit variable allocation.

You can use bitfields within each module. It will combine all the bits
within the module into a structure that is rounded up to the next full
increment used for structs, probably 8, 16, or 32 bits.

Here's another approach to save even more space:
Write a preprocessor that will collect declarations from the files that
you will include for a specific build and generate a header file that
defines a structure containing all the bit-fields.

[ Module 1 ]

#ifdef USEGLOBALBITS
#include "globalbits .h"
#else
struct {
int:1 m1flaga;
int:1 m1flagb;
...
} m1flags;
#endif
....
/* use the bit variables */
if (m1flags.m1flag a){
m1flags.m1flagb = 0;
}

[ globalbits.h ]
/* This file is automatically generated. Do not edit. */
struct {
/* declarations from module1.c */
#define m1flags globalflags
int:1 m1flaga;
int:1 m1flagb;
/* declarations from module3.c */
#define m2flags globalflags
int:1 m3flagx;
int:1 m2flagy;
...
} globalbittype;

extern globalbittype globalflags;

The preprocessor would take as parameters the names of the modules to be
included. It would then scan those modules looking for the specific
declarations for globalflags ("#ifndef GLOBALBITS" to "#endif"), picking
out the bit declarations. It defines a single struct containing all the
bitfields. One caveat is that the bitfield names must all be unique.
The strict identifier format used above is not required, so you can use
whatever names you want.

The reason for the #ifdef is that the modules will compile on their own
if USEGLOBALBITS is not defined, but bits from separate modules will not
be combined within a single bitfield. You can use that before you have
the preprocessor working.

When you have the preprocessor working have your script or makefile run
the preprocessor, specifying the modules to be included, then compile
and link the separate code files. Viola -- minimum RAM for bit variables.

The preprocessor doesn't need to be able to do general parsing -- only
recognizing the delimiters for the sections that hold the definitions.

--
Thad
May 10 '06 #7

<us******@btcon nect.com> wrote in message
news:11******** *************@j 33g2000cwa.goog legroups.com...
Hi
Does anyone know if there is a way to define what is effectively a
single globally visible, enumerated list whilst actually defining the
entries across several different modules?
or somehow do a similar thing with macros.


The usual method with macros is a single master header with OR'able and
AND'able values.

"master.h"
#define ASDF 0x01
#define ASDF 0x02
#define ASDF 0x04
#define ASDF 0x08

Macros can work but get really messy because if you can't #undef until after
the master header is included. This requires the use of alot of extra
defines to calculate the number of useful defines which were defined.

"master.h"
#define OFFSET 0
/* just comment out the one module define you don't need */
/* each central group of macros will be numbered sequentially */
//#define InclMod0 1
#define InclMod1 1
#define InclMod2 1

#ifdef InclMod0
#ifndef OFFSET
#define OFFSET 0
#endif
#define ASDFT (OFFSET)
#define ZXCVC (ASDFT+1)
#define QWERS (ZXCVC+1)
#define DFGHR (QWERS+1)
#ifndef MOD0
#define MOD0 (DFGHR-OFFSET)
#endif
#endif

#ifdef InclMod1
#ifndef OFFSET
#define OFFSET 0
#endif
#ifndef MOD0
#define MOD0 0
#endif
#define ASDF (OFFSET+MOD0+1)
#define ZXCV (ASDF+1)
#define QWER (ZXCV+1)
#define DFGH (QWER+1)
#ifndef MOD1
#define MOD1 (DFGH-OFFSET-MOD0-1)
#endif
#endif

#ifdef InclMod2
#ifndef OFFSET
#define OFFSET 0
#endif
#ifndef MOD0
#define MOD0 0
#endif
#ifndef MOD1
#define MOD1 0
#endif
#define FGH (OFFSET+MOD0+MO D1+1)
#define UIO (FGH+1)
#define QWE (UIO+1)
#define JKL (QWE+1)
#ifndef MOD2
#define MOD2 (JKL-OFFSET-MOD0-MOD1-1)
#endif
#endif

Of course, if I _actually_ needed to do that, I'd definately try to shorten
the names as much as possible.
Rod Pemberton
May 10 '06 #8

Thad, thanks for the suggestion, it looks interresting. I'll
investigate and let you know how I get on.

May 10 '06 #9
Thad. Thanks for your suggestion. I've not actually done it like you
said, but you prompted me in the right direction. The idea of a master
flag file wrapped in some pre-processor defs is the key. Using this I
can achieve my preferred goal of a single global enum list whilst
having the entries defined in separate module headers. This way any
module can see all the flags from all modules.

The only caveats being the names must be unique (as you suggested) and
I must pull in every header with flag definition into the master file.
This is no problem because the compiler won't let me get away with
either of them :-)

I tried it out in a simple dos app and it appears to work fine. I post
the code here for anyone interested.
There is the main code "Main.c", the master flag file
"MasterFlag s.h" file plus three modules M1, M2 & M3 (M2 & 3 don't
contain any code).

//--------- Main.c ---------
#include <stdio.h>
#include "M1.h"

#define GET_FLAG_ENUMS
#include "MasterFlag s.h"
#undef GET_FLAG_ENUMS

// Define ram to store flags in
unsigned char Flags[ (TOTAL_FLAG_COU NT / 8 ) + 1 ] = {0};

// Test the theory
unsigned char TestEnums[ TOTAL_FLAG_COUN T ];

int main(int argc, char* argv[])
{
int i;
printf("%d Flags Defined\n", TOTAL_FLAG_COUN T );

TestEnums[ 0 ] = M1_Flag_This ;
TestEnums[ 1 ] = M1_Flag_That ;
TestEnums[ 2 ] = M1_Flag_TheOthe r ;
TestEnums[ 3 ] = M2_Flag_Quick ;
TestEnums[ 4 ] = M2_Flag_Brown ;
TestEnums[ 5 ] = M2_Flag_Fox ;
TestEnums[ 6 ] = M3_Flag_Red ;
TestEnums[ 7 ] = M3_Flag_Green ;
TestEnums[ 8 ] = M3_Flag_Blue ;

for ( i = 0; i < TOTAL_FLAG_COUN T ; i++ )
{
printf( "%d, ", TestEnums[i] );
}
i = M1_Func();
printf( "\ni = %d", i );
scanf("%d",i);

return 0;
}
//---------- END ----------

//------ MasterFlags.h ------
#ifndef MASTERFLAGFILEH
#define MASTERFLAGFILEH

#define GET_FLAG_ENUMS

enum {
#include "M1.h"
#include "M2.h"
#include "M3.h"
TOTAL_FLAG_COUN T
};

#undef GET_FLAG_ENUMS

#endif // MASTERFLAGFILEH
//---------- END ----------

//---------- M1.c ----------

#include "M1.h"

#define GET_FLAG_ENUMS
#include "MasterFlag s.h"
#undef GET_FLAG_ENUMS

int M1_Func(void)
{
return M3_Flag_Red; // Can see the enum from M3 :-)
}

//---------- END ----------

//---------- M1.h ----------
#ifdef GET_FLAG_ENUMS
M1_Flag_This,
M1_Flag_That,
M1_Flag_TheOthe r,
#else

#ifndef M1H
#define M1H

int M1_Func(void); // Prototype

#endif // M1H
#endif // !GET_FLAG_ENUMS
//---------- END ----------

//---------- M2.h ----------
#ifdef GET_FLAG_ENUMS
M2_Flag_Quick,
M2_Flag_Brown,
M2_Flag_Fox,
#else

#ifndef M2H
#define M2H

// Stuff

#endif // M2H
#endif // !GET_FLAG_ENUMS
//---------- END ----------

//---------- M3.h ----------
#ifdef GET_FLAG_ENUMS
M3_Flag_Red,
M3_Flag_Green,
M3_Flag_Blue,
#else

#ifndef M3H
#define M3H

// Stuff

#endif // M3H
#endif // !GET_FLAG_ENUMS
//---------- END ----------

The following results are printed as expected:

9 Flags Defined
0, 1, 2, 3, 4, 5, 6, 7, 8,
i = 6

Regards
Cliff

May 10 '06 #10

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

Similar topics

13
1718
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out which one. if I do:
13
9554
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
4
1992
by: William Payne | last post by:
Hello, I am starting to steer away from the practice of using "using namespace std;" in my code. Instead I am qualifying each name in the source when I use them, for example: std::cout << "Hello"; Now to my question. Depending upon the status of my program, I return either EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these constants live in the std namespace, I tried: return std::EXIT_FAILURE; but my compiler said:...
27
2742
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template <typename Enum> class smart_enum { public: typedef Enum enum_type;
7
2652
by: narshe | last post by:
I haven't come across an elegant way to do this yet, and would like to know what other people have come up with. There are two tables: CREATE TABLE EmployeeStatus ( pkId int not null primary key identity, status varchar( 50 ) not null )
4
5592
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and when looking at the db we can't tell what the value in a field represents. The other problem is that our enums are currently all stored in a single class, which means that because of no visibility constraints the enums are often used out of context...
2
2883
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default, g++ seems to create enums of 4 bytes in length, unless I use the -fshort-enums option. I don't much want to use this all or nothing approach in case it breaks library code etc, and there's no guarantee that other compilers have a comparable...
3
14560
by: travis.downs | last post by:
Hi, I'm trying to use a macro to create a unique temporary variable name, such as #define TEMP_OBJ(string) MyType obj_ <some magic here(string); So something like TEMP_OBJ("foo")
37
2207
by: Ian Semmel | last post by:
If I have Enum en { i1, i2, i3 } I reckon I should be able to have
0
9595
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
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
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...
0
8891
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6682
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
5313
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...
1
3974
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
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.