Avatar of sean-keys
sean-keys asked on

Assert unique values for a struct member during compile.

I have an embedded application where I need to guarantee that a structure member has a unique value in a table of those structures. I need to ensure that the startingPosition is always unique.

const dataLogDefinition dataLogDefinitions PPAGE_E5 = {
		version:  PROTOCOL_VERSION,
		sizeOfEntry:  sizeof(dataLogField),
		entries: {
				 {.startingPosition = offsetof(CoreVar, IAT), .size = bitSize(CoreVar, IAT), .name = "IAT", .description = "Variable 1", 0,0},
				 {.startingPosition = offsetof(CoreVar, CHT), .size = bitSize(CoreVar, CHT), .name = "CHT", .description = "Variable 2", 0,0},
		}
};

Open in new window


Any ideas would be appreciated.
CLinux OS Dev

Avatar of undefined
Last Comment
Kent Olsen

8/22/2022 - Mon
Kent Olsen

Hi Sean,

Is the table static?  How many items are in the table?  Is uniqueness enough or do they also need to be sequential, non-descending, random, etc.
ASKER
sean-keys

Hi,

Yes the table is static. The length will be variable but within reason 20-30 entries. They do not need to be sequential, I just need to make sure something does not end up in there twice.
ASKER
sean-keys

https://git.libreems.org/libreems-suite/libreems-firmware/blob/dataLogDefProtocol/src/main/dataLogDefinitions.c

needs to describe the data here:

https://git.libreems.org/libreems-suite/libreems-firmware/blob/dataLogDefProtocol/src/main/inc/containerTypes.h#L72


The definitions need to remain seperate because they reside in flash while the other resides in RAM, which I have much less of.

Thanks!
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Kent Olsen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
sean-keys

I guess the "right" way to do this would be to have a script generate both C source files. It seems enum, is as good as it gets :)
Kent Olsen

Well, for the specific application that you've shown, I don't think that a random/unique value is sufficient.  I believe that the value is also an index into the array of values returned from the PIC controller.  

You'll want to initialize the value to the correct index value.