|
4 | 4 |
|
5 | 5 | #pragma once
|
6 | 6 |
|
7 |
| -#include "test/TestEnum.h" |
8 |
| -#include <unordered_map> |
| 7 | +#include <stdexcept> |
| 8 | +#include <string> |
9 | 9 |
|
10 | 10 | namespace margelo {
|
11 | 11 |
|
12 |
| -using namespace facebook; |
| 12 | +namespace EnumMapper { |
| 13 | + // Add these two methods in namespace "EnumMapper" to allow parsing a custom enum: |
| 14 | + // 1. `static void convertJSUnionToEnum(const std::string& inUnion, Enum* outEnum)` |
| 15 | + // 2. `static void convertEnumToJSUnion(Enum inEnum, std::string* outUnion)` |
13 | 16 |
|
14 |
| -static std::runtime_error invalidUnion(const std::string jsUnion) { |
15 |
| - return std::runtime_error("Cannot convert JS Value to Enum: Invalid Union value passed! (\"" + jsUnion + "\")"); |
16 |
| -} |
17 |
| -template <typename Enum> static std::runtime_error invalidEnum(Enum passedEnum) { |
18 |
| - return std::runtime_error("Cannot convert Enum to JS Value: Invalid Enum passed! (Value #" + std::to_string(passedEnum) + |
19 |
| - " does not exist in " + typeid(Enum).name() + ")"); |
20 |
| -} |
21 |
| - |
22 |
| -template <typename Enum> struct EnumMapper { |
23 |
| - static Enum fromJSUnion(const std::string&) { |
24 |
| - static_assert(always_false<Enum>::value, "This type is not supported by the EnumMapper!"); |
25 |
| - return Enum(); |
| 17 | + static std::runtime_error invalidUnion(const std::string& passedUnion) { |
| 18 | + return std::runtime_error("Cannot convert JS Value to Enum: Invalid Union value passed! (\"" + std::string(passedUnion) + "\")"); |
26 | 19 | }
|
27 |
| - static std::string toJSUnion(Enum) { |
28 |
| - static_assert(always_false<Enum>::value, "This type is not supported by the EnumMapper!"); |
29 |
| - return std::string(); |
| 20 | + static std::runtime_error invalidEnum(int passedEnum) { |
| 21 | + return std::runtime_error("Cannot convert Enum to JS Value: Invalid Enum passed! (Value #" + std::to_string(passedEnum) + ")"); |
30 | 22 | }
|
31 | 23 |
|
32 |
| -private: |
33 |
| - template <typename> struct always_false : std::false_type {}; |
34 |
| -}; |
35 |
| - |
36 |
| -template <> struct EnumMapper<TestEnum> { |
37 |
| -public: |
38 |
| - static constexpr TestEnum fromJSUnion(const std::string& jsUnion) { |
39 |
| - if (jsUnion == "first") |
40 |
| - return FIRST; |
41 |
| - if (jsUnion == "second") |
42 |
| - return SECOND; |
43 |
| - if (jsUnion == "third") |
44 |
| - return THIRD; |
45 |
| - throw invalidUnion(jsUnion); |
| 24 | + static void convertJSUnionToEnum(const std::string& inUnion, int*) { |
| 25 | + throw invalidUnion(inUnion); |
46 | 26 | }
|
47 |
| - static std::string toJSUnion(TestEnum value) { |
48 |
| - switch (value) { |
49 |
| - case FIRST: |
50 |
| - return "first"; |
51 |
| - case SECOND: |
52 |
| - return "second"; |
53 |
| - case THIRD: |
54 |
| - return "third"; |
55 |
| - } |
56 |
| - throw invalidEnum(value); |
| 27 | + |
| 28 | + static void convertEnumToJSUnion(int inEnum, std::string*) { |
| 29 | + throw invalidEnum(inEnum); |
57 | 30 | }
|
58 |
| -}; |
| 31 | +} // namespace EnumMapper |
59 | 32 |
|
60 | 33 | } // namespace margelo
|
0 commit comments