You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:param bool asString: *Experimental.* If true, the enum values are represented by plain strings in JavaScript, which is handy for basic operations like comparison and serialization.
:returns: A reference to the current object. This allows chaining of multiple enum values in the :cpp:func:`EMSCRIPTEN_BINDINGS` block.
822
821
823
822
823
+
.. cpp:class:: string_enum_
824
+
825
+
.. code-block:: cpp
826
+
827
+
//prototype
828
+
template<typename EnumType>
829
+
class string_enum_
830
+
831
+
Registers an enum (just like :cpp:class:`enum_`), but the values are represented as plain strings in JavaScript. This is closer to how typescript implements enums, and is handier for basic operations like comparison and serialization. See :ref:`embind-enums` for more information.
832
+
833
+
834
+
.. cpp:type:: enum_type
835
+
836
+
A typedef of ``EnumType`` (a typename for the class).
Copy file name to clipboardExpand all lines: site/source/docs/porting/connecting_cpp_and_javascript/embind.rst
+7-3Lines changed: 7 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -825,12 +825,12 @@ type.
825
825
Module.OldStyle.ONE;
826
826
Module.NewStyle.TWO;
827
827
828
-
If you set the `asString` parameter to `true` when registering the enum, the enum values will be represented as plain strings in JavaScript.
828
+
You can alternatively use the :cpp:class:`string_enum_` class to register your enum values. In this case, they will be represented as plain strings in JavaScript. This is closer to how typescript implements enums, and is handier for basic operations like comparison and serialization.
829
829
830
830
.. code:: cpp
831
831
832
-
EMSCRIPTEN_BINDINGS(my_enum_example) {
833
-
enum_<MyEnum>("MyEnum", true)
832
+
EMSCRIPTEN_BINDINGS(my_string_enum_example) {
833
+
string_enum_<MyEnum>("MyEnum")
834
834
.value("ONE", MyEnum::ONE)
835
835
.value("TWO", MyEnum::TWO)
836
836
;
@@ -840,6 +840,10 @@ If you set the `asString` parameter to `true` when registering the enum, the enu
0 commit comments