Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
Enum.h File Reference

Named enumerations. More...

Go to the source code of this file.

Data Structures

struct  EnumName
 The EnumName type. More...
 

Macros

#define MakeEnumAlias(en, al)
 Creates an EnumName alias for en for use with valueof.
 
#define MakeEnumName(en)
 Creates an EnumName for en for use with valueof.
 
#define MakeEnumNames(...)
 Creates a null-terminated array of EnumNames.
 

Functions

OBJECTIVELY_EXPORT Stringnameof (const EnumName *names, int value)
 Marshal value, returning a String of OR'ed EnumNames for the given enum value.
 
OBJECTIVELY_EXPORT int valueof (const EnumName *names, const char *chars)
 Parse string, returning the corresponding enum value.
 

Detailed Description

Named enumerations.

Definition in file Enum.h.

Macro Definition Documentation

◆ MakeEnumAlias

#define MakeEnumAlias (   en,
  al 
)
Value:
{ \
.name = #en, \
.alias = #al, \
.value = en, \
}

Creates an EnumName alias for en for use with valueof.

See also
valueof(constEnumName *, const char *, int)

Definition at line 58 of file Enum.h.

59 { \
60 .name = #en, \
61 .alias = #al, \
62 .value = en, \
63 }

◆ MakeEnumName

#define MakeEnumName (   en)
Value:
{ \
.name = #en, \
.alias = NULL, \
.value = en, \
}

Creates an EnumName for en for use with valueof.

See also
valueof(const EnumName *, const char *, int)

Definition at line 47 of file Enum.h.

48 { \
49 .name = #en, \
50 .alias = NULL, \
51 .value = en, \
52 }

◆ MakeEnumNames

#define MakeEnumNames (   ...)
Value:
{ \
__VA_ARGS__, \
{ \
.name = NULL, \
.value = 0 \
} \
}

Creates a null-terminated array of EnumNames.

Definition at line 68 of file Enum.h.

69 { \
70 __VA_ARGS__, \
71 { \
72 .name = NULL, \
73 .value = 0 \
74 } \
75 }

Function Documentation

◆ nameof()

OBJECTIVELY_EXPORT String * nameof ( const EnumName names,
int  value 
)

Marshal value, returning a String of OR'ed EnumNames for the given enum value.

Parameters
namesA null-terminated array of EnumNames.
valueThe enum value, which may be a bitwise OR of multiple enum values.
Returns
A String of EnumNames matching value, or NULL.

Definition at line 31 of file Enum.c.

31 {
32
33 for (const EnumName *name = names; name->name; name++) {
34 if (name->value == value) {
35 return $$(String, stringWithCharacters, name->name);
36 }
37 }
38
39 String *string = NULL;
40
41 for (const EnumName *name = names; name->name; name++) {
42 if (name->value) {
43 if ((name->value & value) == name->value) {
44 if (string == NULL) {
45 string = $$(String, string);
46 } else {
47 $(string, appendCharacters, " | ");
48 }
49 $(string, appendCharacters, name->name);
50 }
51 }
52 }
53
54 return (String *) string;
55}
static String * string(void)
Definition String.c:905
static void appendCharacters(String *self, const char *chars)
Definition String.c:506
static String * stringWithCharacters(const char *chars)
Definition String.c:349
The EnumName type.
Definition Enum.h:37
const char * name
Definition Enum.h:38
UTF-8 strings.
Definition String.h:69

◆ valueof()

OBJECTIVELY_EXPORT int valueof ( const EnumName names,
const char *  chars 
)

Parse string, returning the corresponding enum value.

Parameters
namesA null-terminated array of EnumNames.
charsA null-terminated C string.
Returns
The value of the EnumName matching string, or -1.

Definition at line 57 of file Enum.c.

57 {
58
59 int value = 0;
60
61 const char *c = chars;
62 while (*c) {
63 const size_t size = strcspn(c, " |");
64 if (size) {
65 const EnumName *en;
66 for (en = names; en->name; en++) {
67 if (strncmp(en->name, c, size) == 0) {
68 value |= en->value;
69 break;
70 }
71 if (en->alias && strncmp(en->alias, c, size) == 0) {
72 value |= en->value;
73 break;
74 }
75 }
76 if (en->name == NULL) {
77 fprintf(stderr, "%s: Unknown character sequence: %s\n", __func__, c);
78 }
79 }
80 c += size;
81 c += strspn(c, " |");
82 }
83
84 return value;
85}
int value
Definition Enum.h:40
const char * alias
Definition Enum.h:39