Skip to content
This repository was archived by the owner on Mar 26, 2020. It is now read-only.

Commit 24ac1fc

Browse files
committed
added generation of c++ enum serializer functions.
Added flag cpp-enum-serializers to enable serializing enums in c++; When this flag is turned on - serialize function declaration will be generated in enum's header and c++ function definition will be generated to similar cpp file.
1 parent 3aa304c commit 24ac1fc

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

src/source/CppGenerator.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
6363
refs.hpp.add("#include <functional>") // needed for std::hash
6464
}
6565

66+
val stringExpr = MExpr(MString, Seq.empty[MExpr])
67+
refs.find(stringExpr, false)
68+
6669
val flagsType = "unsigned"
6770
val enumType = "int"
6871
val underlyingType = if(e.flags) flagsType else enumType
6972

73+
val stringRetType = marshal.typename(stringExpr)
74+
val toStringFunctionName = idCpp.method(s"${self}_to_string")
75+
7076
writeHppFile(ident, origin, refs.hpp, refs.hppFwds, w => {
7177
w.w(s"enum class $self : $underlyingType").bracedSemi {
7278
writeEnumOptionNone(w, e, idCpp.enum)
@@ -92,6 +98,9 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
9298
w.wl(s"return static_cast<$self>(~static_cast<$flagsType>(x));")
9399
}
94100
}
101+
if(spec.cppEnumSerializers) {
102+
w.wl.wl(s"$stringRetType $toStringFunctionName(${withCppNs(self)} arg);")
103+
}
95104
},
96105
w => {
97106
// std::hash specialization has to go *outside* of the wrapNs
@@ -110,6 +119,21 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
110119
)
111120
}
112121
})
122+
if(spec.cppEnumSerializers) {
123+
writeCppFile(ident, origin, refs.cpp,
124+
w => {
125+
w.w(s"$stringRetType $toStringFunctionName(${withCppNs(self)} arg)").braced {
126+
w.w("switch(arg)").braced {
127+
for (o <- normalEnumOptions(e)) {
128+
val enumItem = withCppNs(s"$self::${idCpp.enum(o.ident.name)}")
129+
w.wl(s"case $enumItem: return ${q(enumItem)};")
130+
}
131+
val convertValToString = (if (spec.cppUseWideStrings) "std::to_wstring" else "std::to_string") + s"(static_cast<${underlyingType}>(arg))"
132+
w.wl(s"default: return $stringRetType(${q(s"$toStringFunctionName: not supported enum value: ")}) + $convertValToString;")
133+
}
134+
}
135+
})
136+
}
113137
}
114138

115139
def shouldConstexpr(c: Const) = {

src/source/Main.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ object Main {
5959
var cppHeaderOutFolderOptional: Option[File] = None
6060
var cppExt: String = "cpp"
6161
var cppHeaderExt: String = "hpp"
62+
var cppEnumSerializers = false
6263
var javaIdentStyle = IdentStyle.javaDefault
6364
var cppIdentStyle = IdentStyle.cppDefault
6465
var cppTypeEnumIdentStyle: IdentConverter = null
@@ -136,6 +137,7 @@ object Main {
136137
.text("The filename extension for C++ files (default: \"cpp\").")
137138
opt[String]("hpp-ext").valueName("<ext>").foreach(cppHeaderExt = _)
138139
.text("The filename extension for C++ header files (default: \"hpp\").")
140+
opt[Boolean]("cpp-enum-serializers").valueName("<true/false>").foreach(x => cppEnumSerializers = x)
139141
opt[String]("cpp-optional-template").valueName("<template>").foreach(x => cppOptionalTemplate = x)
140142
.text("The template to use for optional values (default: \"std::optional\")")
141143
opt[String]("cpp-optional-header").valueName("<header>").foreach(x => cppOptionalHeader = x)
@@ -339,6 +341,7 @@ object Main {
339341
jniBaseLibIncludePrefix,
340342
cppExt,
341343
cppHeaderExt,
344+
cppEnumSerializers,
342345
objcOutFolder,
343346
objcppOutFolder,
344347
objcIdentStyle,

src/source/generator.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ package object generatorTools {
6464
jniBaseLibIncludePrefix: String,
6565
cppExt: String,
6666
cppHeaderExt: String,
67+
cppEnumSerializers: Boolean,
6768
objcOutFolder: Option[File],
6869
objcppOutFolder: Option[File],
6970
objcIdentStyle: ObjcIdentStyle,

test-suite/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ djinni:
66
./run_djinni.sh
77

88
objc: djinni
9-
cd objc; xcodebuild -sdk iphonesimulator -project DjinniObjcTest.xcodeproj -scheme DjinniObjcTest test -destination 'platform=iOS Simulator,name=iPhone 7,OS=latest'
9+
cd objc; xcodebuild -sdk iphonesimulator -project DjinniObjcTest.xcodeproj -scheme DjinniObjcTest test -destination 'platform=iOS Simulator,name=iPhone 8,OS=latest'
1010

1111
clean_objc:
1212
cd objc && xcodebuild -sdk iphonesimulator -project DjinniObjcTest.xcodeproj -scheme DjinniObjcTest clean

test-suite/run_djinni.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ fi
110110
--cpp-optional-template "std::experimental::optional" \
111111
--cpp-optional-header "\"../../handwritten-src/cpp/optional.hpp\"" \
112112
--cpp-extended-record-include-prefix "../../handwritten-src/cpp/" \
113+
--cpp-enum-serializers true \
113114
\
114115
--jni-out "$temp_out_relative/jni" \
115116
--ident-jni-class NativeFooBar \

0 commit comments

Comments
 (0)