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

Commit

Permalink
added generation of c++ enum serializer functions.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ctin committed Oct 23, 2019
1 parent 3aa304c commit 24ac1fc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/source/CppGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
refs.hpp.add("#include <functional>") // needed for std::hash
}

val stringExpr = MExpr(MString, Seq.empty[MExpr])
refs.find(stringExpr, false)

val flagsType = "unsigned"
val enumType = "int"
val underlyingType = if(e.flags) flagsType else enumType

val stringRetType = marshal.typename(stringExpr)
val toStringFunctionName = idCpp.method(s"${self}_to_string")

writeHppFile(ident, origin, refs.hpp, refs.hppFwds, w => {
w.w(s"enum class $self : $underlyingType").bracedSemi {
writeEnumOptionNone(w, e, idCpp.enum)
Expand All @@ -92,6 +98,9 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
w.wl(s"return static_cast<$self>(~static_cast<$flagsType>(x));")
}
}
if(spec.cppEnumSerializers) {
w.wl.wl(s"$stringRetType $toStringFunctionName(${withCppNs(self)} arg);")
}
},
w => {
// std::hash specialization has to go *outside* of the wrapNs
Expand All @@ -110,6 +119,21 @@ class CppGenerator(spec: Spec) extends Generator(spec) {
)
}
})
if(spec.cppEnumSerializers) {
writeCppFile(ident, origin, refs.cpp,
w => {
w.w(s"$stringRetType $toStringFunctionName(${withCppNs(self)} arg)").braced {
w.w("switch(arg)").braced {
for (o <- normalEnumOptions(e)) {
val enumItem = withCppNs(s"$self::${idCpp.enum(o.ident.name)}")
w.wl(s"case $enumItem: return ${q(enumItem)};")
}
val convertValToString = (if (spec.cppUseWideStrings) "std::to_wstring" else "std::to_string") + s"(static_cast<${underlyingType}>(arg))"
w.wl(s"default: return $stringRetType(${q(s"$toStringFunctionName: not supported enum value: ")}) + $convertValToString;")
}
}
})
}
}

def shouldConstexpr(c: Const) = {
Expand Down
3 changes: 3 additions & 0 deletions src/source/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ object Main {
var cppHeaderOutFolderOptional: Option[File] = None
var cppExt: String = "cpp"
var cppHeaderExt: String = "hpp"
var cppEnumSerializers = false
var javaIdentStyle = IdentStyle.javaDefault
var cppIdentStyle = IdentStyle.cppDefault
var cppTypeEnumIdentStyle: IdentConverter = null
Expand Down Expand Up @@ -136,6 +137,7 @@ object Main {
.text("The filename extension for C++ files (default: \"cpp\").")
opt[String]("hpp-ext").valueName("<ext>").foreach(cppHeaderExt = _)
.text("The filename extension for C++ header files (default: \"hpp\").")
opt[Boolean]("cpp-enum-serializers").valueName("<true/false>").foreach(x => cppEnumSerializers = x)
opt[String]("cpp-optional-template").valueName("<template>").foreach(x => cppOptionalTemplate = x)
.text("The template to use for optional values (default: \"std::optional\")")
opt[String]("cpp-optional-header").valueName("<header>").foreach(x => cppOptionalHeader = x)
Expand Down Expand Up @@ -339,6 +341,7 @@ object Main {
jniBaseLibIncludePrefix,
cppExt,
cppHeaderExt,
cppEnumSerializers,
objcOutFolder,
objcppOutFolder,
objcIdentStyle,
Expand Down
1 change: 1 addition & 0 deletions src/source/generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ package object generatorTools {
jniBaseLibIncludePrefix: String,
cppExt: String,
cppHeaderExt: String,
cppEnumSerializers: Boolean,
objcOutFolder: Option[File],
objcppOutFolder: Option[File],
objcIdentStyle: ObjcIdentStyle,
Expand Down
2 changes: 1 addition & 1 deletion test-suite/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ djinni:
./run_djinni.sh

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

clean_objc:
cd objc && xcodebuild -sdk iphonesimulator -project DjinniObjcTest.xcodeproj -scheme DjinniObjcTest clean
Expand Down
1 change: 1 addition & 0 deletions test-suite/run_djinni.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ fi
--cpp-optional-template "std::experimental::optional" \
--cpp-optional-header "\"../../handwritten-src/cpp/optional.hpp\"" \
--cpp-extended-record-include-prefix "../../handwritten-src/cpp/" \
--cpp-enum-serializers true \
\
--jni-out "$temp_out_relative/jni" \
--ident-jni-class NativeFooBar \
Expand Down

0 comments on commit 24ac1fc

Please sign in to comment.