forked from dhilip89/njs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnjs-extension-enum.h
More file actions
221 lines (176 loc) · 6.72 KB
/
njs-extension-enum.h
File metadata and controls
221 lines (176 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// [NJS-API]
// Native JavaScript API for Bindings.
//
// [License]
// Public Domain <http://unlicense.org>
// This header implements an enumeration extension. Enumeration is a map of
// sequential values into string representation and back. The `njs::Enum` type
// provided can be used to define enumeration and to act as parser or serializer.
#ifndef NJS_EXTENSION_ENUM_H
#define NJS_EXTENSION_ENUM_H
#include "./njs-api.h"
namespace njs {
/*TODO:
template<typename Enum_>
NJS_INLINE Value NewStringFromEnum(int value, const Enum_& enum_) noexcept {
return Internal::V8EnumToString(
*this, value, reinterpret_cast<const Enum&>(enum_));
}
*/
// ============================================================================
// [njs::Internal::EnumUtils]
// ============================================================================
namespace Internal {
namespace EnumUtils {
static const constexpr char kAltEnumMarker = '@';
static const constexpr unsigned int kEnumNotFound = ~static_cast<unsigned int>(0);
static NJS_INLINE bool isIgnorableChar(unsigned int c) noexcept {
return c == '-';
}
// Tries to find a string passed as `in` in `enumData` (see `Enum`). It returns
// the index of the value found. If the enumeration doesn't start at 0 then the
// index has to be adjusted accordingly. It returns `kEnumNotFound` on failure.
//
// Keep it inlined, it should expand only once if used properly.
template<typename CharType>
static NJS_INLINE unsigned int parse(const CharType* in, size_t size, const char* enumData) noexcept {
if (!size) return kEnumNotFound;
const char* pa = enumData;
const CharType* pbEnd = in + size;
unsigned int ca, cb;
unsigned int cbFirst = in[0];
for (unsigned int index = 0; ; index++) {
// NULL record indicates end of data.
ca = static_cast<uint8_t>(*pa++);
if (!ca) return kEnumNotFound;
// Enum can contain alternative strings that describe the same value,
// so make sure we won't increment if we just parsed the notation.
if (ca == kAltEnumMarker) {
ca = static_cast<uint8_t>(*pa++);
index--;
}
if (ca == cbFirst) {
const CharType* pb = in;
while (++pb != pbEnd) {
cb = *pb;
L_Repeat:
ca = static_cast<uint8_t>(*pa++);
if (!ca) goto L_End;
if (ca == cb) continue;
if (isIgnorableChar(ca))
goto L_Repeat;
else
goto L_NoMatch;
}
ca = static_cast<uint8_t>(*pa++);
if (!ca) return index;
}
L_NoMatch:
while (*pa++)
continue;
L_End:
;
}
}
// Keep it inlined, it should expand only once if used properly.
template<typename CharType>
static NJS_INLINE unsigned int stringify(CharType* data, unsigned int index, const char* enumData) noexcept {
unsigned int i = 0;
const char* p = enumData;
while (i != index) {
// NULL record indicates end of data.
if (!*p)
return kEnumNotFound;
// Only increment if this is not an alternative string.
i += *p != kAltEnumMarker;
// Skip the current string.
while (*++p)
continue;
// Skip the NULL terminator of this record.
p++;
}
CharType* pData = data;
for (;;) {
unsigned int c = static_cast<uint8_t>(*p++);
if (!c)
break;
*pData++ = static_cast<CharType>(c);
}
return static_cast<size_t>((intptr_t)(pData - data));
}
} // EnumUtils namespace
} // {Internal}
// ============================================================================
// [njs::Enum & NJS_ENUM]
// ============================================================================
struct Enum {
enum { kConceptType = Globals::kConceptSerializer };
// Returns enumeration representation defined by `EnumT<>`.
NJS_INLINE const char* data() const noexcept {
return reinterpret_cast<const char*>(this + 1);
}
template<typename T>
NJS_NOINLINE Result serialize(Context& ctx, T in, Value& out) const noexcept {
unsigned int index = static_cast<unsigned int>(static_cast<int>(in)) - static_cast<unsigned int>(_start);
uint16_t content[Globals::kMaxEnumSize];
unsigned int size = Internal::EnumUtils::stringify<uint16_t>(content, index, data());
if (size == Internal::EnumUtils::kEnumNotFound || size == 0) {
out = ctx.newString();
return Globals::kResultInvalidValue;
}
else {
out = ctx.newString(Utf16Ref(content, size));
return resultOf(out);
}
}
template<typename T>
NJS_NOINLINE Result deserialize(Context& ctx, const Value& in, T& out) const noexcept {
if (!in.isString())
return Globals::kResultInvalidValue;
uint16_t content[Globals::kMaxEnumSize];
int size = ctx.stringLength(in);
if (size <= 0 || size > int(Globals::kMaxEnumSize) || ctx.readUtf16(in, content, size) < size)
return Globals::kResultInvalidValue;
unsigned int index = Internal::EnumUtils::parse<uint16_t>(content, size, data());
if (index == Internal::EnumUtils::kEnumNotFound)
return Globals::kResultInvalidValue;
out = static_cast<T>(index + static_cast<unsigned int>(_start));
return Globals::kResultOk;
}
// --------------------------------------------------------------------------
// [Members]
// --------------------------------------------------------------------------
int _start;
int _end;
int _size;
int _flags;
};
// NOTE: This class cannot inherit from `Enum`, the intend is to have the
// instance of `EnumT<>` statically initialized and in read-only memory.
template<size_t Size>
struct EnumT {
enum { kConceptType = Globals::kConceptSerializer };
// Treat `EnumT` as `Enum`, if possible.
NJS_INLINE operator Enum&() noexcept { return _base; }
NJS_INLINE operator const Enum&() const noexcept { return _base; }
NJS_INLINE const Enum* operator&() const noexcept { return &_base; }
// Compatibility with `Enum`.
NJS_INLINE const char* data() const noexcept { return _base.data(); }
template<typename T>
NJS_NOINLINE Result serialize(Context& ctx, T in, Value& out) const noexcept {
return _base.serialize(ctx, in, out);
}
template<typename T>
NJS_INLINE Result deserialize(Context& ctx, const Value& in, T& out) const noexcept {
return _base.deserialize(ctx, in, out);
}
Enum _base;
char _data[(Size + 3) & ~static_cast<size_t>(3)];
};
#define NJS_ENUM(NAME, FIRST_VALUE, LAST_VALUE, DATA) \
static const ::njs::EnumT< sizeof(DATA) > NAME = { \
{ FIRST_VALUE, LAST_VALUE, static_cast<int>(sizeof(DATA) - 1), 0, }, \
DATA \
}
} // {njs}
#endif // NJS_EXTENSION_ENUM_H