-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add enums to the
HybridObject
(#7)
Adds support for enums to `HybridObject`: When declaring custom enums there's two steps you need to do: 1. Create your enum (e.g. in `TestEnum.h`) ```cpp enum TestEnum { FIRST, SECOND, THIRD }; ``` 2. Add the enum to the `EnumMapper` by editing `cpp/jsi/EnumMapper.h`: ```cpp template<> struct EnumMapper<TestEnum> { public: static constexpr TestEnum fromJSUnion(const std::string& jsUnion) { if (jsUnion == "first") return FIRST; if (jsUnion == "second") return SECOND; if (jsUnion == "third") return THIRD; throw invalidUnion(jsUnion); } static std::string toJSUnion(TestEnum value) { switch (value) { case FIRST: return "first"; case SECOND: return "second"; case THIRD: return "third"; } throw invalidEnum(value); } }; ``` This way compile-time safety is guaranteed. All other approaches to parse enums from C++ to JS are not compile-time but rather run-time, so currently there's no way around editing `EnumMapper.h` unless we do runtime type checking.
- Loading branch information
Showing
7 changed files
with
106 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Created by Marc Rousavy on 22.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "test/TestEnum.h" | ||
#include <unordered_map> | ||
|
||
namespace margelo { | ||
|
||
using namespace facebook; | ||
|
||
static std::runtime_error invalidUnion(const std::string jsUnion) { | ||
return std::runtime_error("Cannot convert JS Value to Enum: Invalid Union value passed! (\"" + jsUnion + "\")"); | ||
} | ||
template <typename Enum> static std::runtime_error invalidEnum(Enum passedEnum) { | ||
return std::runtime_error("Cannot convert Enum to JS Value: Invalid Enum passed! (Value #" + std::to_string(passedEnum) + | ||
" does not exist in " + typeid(Enum).name() + ")"); | ||
} | ||
|
||
template <typename Enum> struct EnumMapper { | ||
static Enum fromJSUnion(const std::string&) { | ||
static_assert(always_false<Enum>::value, "This type is not supported by the EnumMapper!"); | ||
return Enum(); | ||
} | ||
static std::string toJSUnion(Enum) { | ||
static_assert(always_false<Enum>::value, "This type is not supported by the EnumMapper!"); | ||
return std::string(); | ||
} | ||
|
||
private: | ||
template <typename> struct always_false : std::false_type {}; | ||
}; | ||
|
||
template <> struct EnumMapper<TestEnum> { | ||
public: | ||
static constexpr TestEnum fromJSUnion(const std::string& jsUnion) { | ||
if (jsUnion == "first") | ||
return FIRST; | ||
if (jsUnion == "second") | ||
return SECOND; | ||
if (jsUnion == "third") | ||
return THIRD; | ||
throw invalidUnion(jsUnion); | ||
} | ||
static std::string toJSUnion(TestEnum value) { | ||
switch (value) { | ||
case FIRST: | ||
return "first"; | ||
case SECOND: | ||
return "second"; | ||
case THIRD: | ||
return "third"; | ||
} | ||
throw invalidEnum(value); | ||
} | ||
}; | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Created by Marc Rousavy on 22.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "jsi/EnumMapper.h" | ||
|
||
namespace margelo { | ||
|
||
enum TestEnum { FIRST, SECOND, THIRD }; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters