aboutsummaryrefslogtreecommitdiff
path: root/puzzles.h
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2017-10-01 13:38:35 +0100
committerSimon Tatham <anakin@pobox.com>2017-10-01 16:34:41 +0100
commitde67801b0fd3dfa11777c1ef86cd617baf376b7b (patch)
treef632daee458c8fefcfc90ff809c44672adeedaa4 /puzzles.h
parenteeb2db283de9115f7256fa4cc49597d63e06b0ab (diff)
downloadpuzzles-de67801b0fd3dfa11777c1ef86cd617baf376b7b.zip
puzzles-de67801b0fd3dfa11777c1ef86cd617baf376b7b.tar.gz
puzzles-de67801b0fd3dfa11777c1ef86cd617baf376b7b.tar.bz2
puzzles-de67801b0fd3dfa11777c1ef86cd617baf376b7b.tar.xz
Use a proper union in struct config_item.
This allows me to use different types for the mutable, dynamically allocated string value in a C_STRING control and the fixed constant list of option names in a C_CHOICES.
Diffstat (limited to 'puzzles.h')
-rw-r--r--puzzles.h52
1 files changed, 29 insertions, 23 deletions
diff --git a/puzzles.h b/puzzles.h
index f43ee83..0a91e31 100644
--- a/puzzles.h
+++ b/puzzles.h
@@ -137,30 +137,36 @@ typedef struct psdata psdata;
*/
enum { C_STRING, C_CHOICES, C_BOOLEAN, C_END };
struct config_item {
- /*
- * `name' is never dynamically allocated.
- */
- char *name;
- /*
- * `type' contains one of the above values.
- */
+ /* Not dynamically allocated */
+ const char *name;
+ /* Value from the above C_* enum */
int type;
- /*
- * For C_STRING, `sval' is always dynamically allocated and
- * non-NULL. For C_BOOLEAN and C_END, `sval' is always NULL.
- * For C_CHOICES, `sval' is non-NULL, _not_ dynamically
- * allocated, and contains a set of option strings separated by
- * a delimiter. The delimeter is also the first character in
- * the string, so for example ":Foo:Bar:Baz" gives three
- * options `Foo', `Bar' and `Baz'.
- */
- char *sval;
- /*
- * For C_BOOLEAN, this is TRUE or FALSE. For C_CHOICES, it
- * indicates the chosen index from the `sval' list. In the
- * above example, 0==Foo, 1==Bar and 2==Baz.
- */
- int ival;
+ union {
+ struct { /* if type == C_STRING */
+ /* Always dynamically allocated and non-NULL */
+ char *sval;
+ } string;
+ struct { /* if type == C_CHOICES */
+ /*
+ * choicenames is non-NULL, not dynamically allocated, and
+ * contains a set of option strings separated by a
+ * delimiter. The delimiter is also the first character in
+ * the string, so for example ":Foo:Bar:Baz" gives three
+ * options `Foo', `Bar' and `Baz'.
+ */
+ const char *choicenames;
+ /*
+ * Indicates the chosen index from the options in
+ * choicenames. In the above example, 0==Foo, 1==Bar and
+ * 2==Baz.
+ */
+ int selected;
+ } choices;
+ struct {
+ /* just TRUE or FALSE */
+ int bval;
+ } boolean;
+ } u;
};
/*