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

added generated function enum_to_string #452

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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