I have a data structure like this:
struct foo { int id; int route; int backup_route; int current_route; }
and a
The most elegant way would be to update the struct fields directly, without having to use the update()
function - but maybe there are good reasons for using it that don't come across in the question.
struct foo* bar = get_foo_ptr();
foo_ref.id = 42;
foo_ref.current_route = new_route;
Or you can, like Pukku suggested, create separate access functions for each field of the struct.
Otherwise the best solution I can think of is treating a value of '0' in a struct field as a 'do not update' flag - so you just create a funciton to return a zeroed out struct, and then use this to update.
struct foo empty_foo(void)
{
struct foo bar;
bzero(&bar, sizeof (struct bar));
return bar;
}
struct foo bar = empty_foo();
bar.id=42;
bar.current_route = new_route;
update(&bar);
However, this might not be very feasible, if 0 is a valid value for fields in the struct.
You could address the problem with an X-Macro
You would change your struct definition into:
#define LIST_OF_foo_MEMBERS \
X(int,id) \
X(int,route) \
X(int,backup_route) \
X(int,current_route)
#define X(type,name) type name;
struct foo {
LIST_OF_foo_MEMBERS
};
#undef X
And then you would be able to easily define a flexible function that sets all fields to dont_care
.
#define X(type,name) in->name = dont_care;
void setFooToDontCare(struct foo* in) {
LIST_OF_foo_MEMBERS
}
#undef X
Following the discussion here, one could also define a default value in this way:
#define X(name) dont_care,
const struct foo foo_DONT_CARE = { LIST_OF_STRUCT_MEMBERS_foo };
#undef X
Which translates into:
const struct foo foo_DONT_CARE = {dont_care, dont_care, dont_care, dont_care,};
And use it as in hlovdal answer, with the advantage that here maintenance is easier, i.e. changing the number of struct members will automatically update foo_DONT_CARE
. Note that the last "spurious" comma is acceptable.
I first learned the concept of X-Macros when I had to address this problem.
It is extremely flexible to new fields being added to the struct. If you have different data types, you could define different dont_care
values depending on the data type: from here, you could take inspiration from the function used to print the values in the second example.
If you are ok with an all int
struct, then you could omit the data type from LIST_OF_foo_MEMBERS
and simply change the X function of the struct definition into #define X(name) int name;
While macros and/or functions (as already suggested) will work (and might have other positive effects (i.e. debug hooks)), they are more complex than needed. The simplest and possibly most elegant solution is to just define a constant that you use for variable initialisation:
const struct foo FOO_DONT_CARE = { // or maybe FOO_DEFAULT or something
dont_care, dont_care, dont_care, dont_care
};
...
struct foo bar = FOO_DONT_CARE;
bar.id = 42;
bar.current_route = new_route;
update(&bar);
This code has virtually no mental overhead of understanding the indirection, and it is very clear which fields in bar
you set explicitly while (safely) ignoring those you do not set.
One pattern gobject uses is a variadic function, and enumerated values for each property. The interface looks something like:
update (ID, 1,
BACKUP_ROUTE, 4,
-1); /* -1 terminates the parameter list */
Writing a varargs function is easy -- see http://www.eskimo.com/~scs/cclass/int/sx11b.html. Just match up key -> value pairs and set the appropriate structure attributes.