Skip to content
Draft
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
17 changes: 12 additions & 5 deletions src/source/CppMarshal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class CppMarshal(spec: Spec) extends Marshal(spec) {
case "i8" | "i16" | "i32" | "i64" => List(ImportRef("<cstdint>"))
case _ => List()
}
case MString => List(ImportRef("<string>"))
case MString =>
val includes = List(ImportRef("<string>"))
if (spec.cppUseStringView) ImportRef("<string_view>") :: includes else includes
case MDate => List(ImportRef("<chrono>"))
case MBinary => List(ImportRef("<vector>"), ImportRef("<cstdint>"))
case MOptional => List(ImportRef(spec.cppOptionalHeader))
Expand Down Expand Up @@ -258,10 +260,15 @@ class CppMarshal(spec: Spec) extends Marshal(spec) {

// this can be used in c++ generation to know whether a const& should be applied to the parameter or not
private def toCppParamType(tm: MExpr, namespace: Option[String] = None, scopeSymbols: Seq[String] = Seq()): String = {
val cppType = toCppType(tm, namespace, scopeSymbols)
val refType = "const " + cppType + " &"
val valueType = cppType
if(byValue(tm)) valueType else refType
tm.base match {
case MString if spec.cppUseStringView =>
if (spec.cppUseWideStrings) "std::wstring_view" else "std::string_view"
case _ =>
val cppType = toCppType(tm, namespace, scopeSymbols)
val refType = "const " + cppType + " &"
val valueType = cppType
if(byValue(tm)) valueType else refType
}
}

private def moveOnly(tm: MExpr): Boolean = tm.base match {
Expand Down
4 changes: 4 additions & 0 deletions src/source/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object Main {
var cppNnCheckExpression: Option[String] = None
var cppUseWideStrings: Boolean = false
var cppLegacyRecords: Boolean = false
var cppUseStringView: Boolean = false
var javaOutFolder: Option[File] = None
var javaPackage: Option[String] = None
var javaClassAccessModifier: JavaAccessModifier.Value = JavaAccessModifier.Public
Expand Down Expand Up @@ -191,6 +192,8 @@ object Main {
.text("Use wide strings in C++ code (default: false)")
opt[Boolean]( "cpp-legacy-records").valueName("<true/false>").foreach(x => cppLegacyRecords = x)
.text("Use legacy record behavior for C++ code (default: false)")
opt[Boolean]( "cpp-use-string-view").valueName("<true/false>").foreach(x => cppUseStringView = x)
.text("Use std::string_view for string parameters in C++ code (default: false)")
note("")
opt[File]("jni-out").valueName("<out-folder>").foreach(x => jniOutFolder = Some(x))
.text("The folder for the JNI C++ output files (Generator disabled if unspecified).")
Expand Down Expand Up @@ -438,6 +441,7 @@ object Main {
cppNnCheckExpression,
cppUseWideStrings,
cppLegacyRecords,
cppUseStringView,
jniOutFolder,
jniHeaderOutFolder,
jniIncludePrefix,
Expand Down
1 change: 1 addition & 0 deletions src/source/generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ package object generatorTools {
cppNnCheckExpression: Option[String],
cppUseWideStrings: Boolean,
cppLegacyRecords: Boolean,
cppUseStringView: Boolean,
jniOutFolder: Option[File],
jniHeaderOutFolder: Option[File],
jniIncludePrefix: String,
Expand Down
1 change: 1 addition & 0 deletions test-suite/djinni/all.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
@import "enum_flags.djinni"
@import "constant_enum.djinni"
@import "data_ref_view.djinni"
@import "string_view_test.djinni"

@import "vendor/third-party/date.djinni"
@import "third-party/duration.djinni"
Expand Down
6 changes: 6 additions & 0 deletions test-suite/djinni/string_view_test.djinni
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @flag "--cpp-use-string-view true"

test_interface_with_string = interface +c {
# Method with string parameter to test string_view generation
process_string(message: string): string;
}
19 changes: 19 additions & 0 deletions test-suite/generated-src/cpp/test_interface_with_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

#pragma once

#include <string>

namespace testsuite {

/** @flag "--cpp-use-string-view true" */
class TestInterfaceWithString {
public:
virtual ~TestInterfaceWithString() = default;

/** Method with string parameter to test string_view generation */
virtual std::string process_string(const std::string & message) = 0;
};

} // namespace testsuite
1 change: 1 addition & 0 deletions test-suite/generated-src/inFileList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ djinni/constant_enum.djinni
djinni/data_ref_view.djinni
../support-lib/dataref.yaml
../support-lib/dataview.yaml
djinni/string_view_test.djinni
djinni/vendor/third-party/date.djinni
djinni/vendor/third-party/date.yaml
djinni/vendor/third-party/duration.djinni
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

package com.dropbox.djinni.test;

import com.snapchat.djinni.NativeObjectManager;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/** @flag "--cpp-use-string-view true" */
public abstract class TestInterfaceWithString {
/** Method with string parameter to test string_view generation */
@Nonnull
public abstract String processString(@Nonnull String message);

public static final class CppProxy extends TestInterfaceWithString
{
private final long nativeRef;
private final AtomicBoolean destroyed = new AtomicBoolean(false);

private CppProxy(long nativeRef)
{
if (nativeRef == 0) throw new RuntimeException("nativeRef is zero");
this.nativeRef = nativeRef;
NativeObjectManager.register(this, nativeRef);
}
public static native void nativeDestroy(long nativeRef);

@Override
public String processString(String message)
{
assert !this.destroyed.get() : "trying to use a destroyed object";
return native_processString(this.nativeRef, message);
}
private native String native_processString(long _nativeRef, String message);
}
}
30 changes: 30 additions & 0 deletions test-suite/generated-src/jni/NativeTestInterfaceWithString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

#include "NativeTestInterfaceWithString.hpp" // my header
#include "Marshal.hpp"

namespace djinni_generated {

NativeTestInterfaceWithString::NativeTestInterfaceWithString() : ::djinni::JniInterface<::testsuite::TestInterfaceWithString, NativeTestInterfaceWithString>("com/dropbox/djinni/test/TestInterfaceWithString$CppProxy") {}

NativeTestInterfaceWithString::~NativeTestInterfaceWithString() = default;


CJNIEXPORT void JNICALL Java_com_dropbox_djinni_test_TestInterfaceWithString_00024CppProxy_nativeDestroy(JNIEnv* jniEnv, jobject /*this*/, jlong nativeRef)
{
try {
delete reinterpret_cast<::djinni::CppProxyHandle<::testsuite::TestInterfaceWithString>*>(nativeRef);
} JNI_TRANSLATE_EXCEPTIONS_RETURN(jniEnv, )
}

CJNIEXPORT jstring JNICALL Java_com_dropbox_djinni_test_TestInterfaceWithString_00024CppProxy_native_1processString(JNIEnv* jniEnv, jobject /*this*/, jlong nativeRef, jstring j_message)
{
try {
const auto& ref = ::djinni::objectFromHandleAddress<::testsuite::TestInterfaceWithString>(nativeRef);
auto r = ref->process_string(::djinni::String::toCpp(jniEnv, j_message));
return ::djinni::release(::djinni::String::fromCpp(jniEnv, r));
} JNI_TRANSLATE_EXCEPTIONS_RETURN(jniEnv, 0 /* value doesn't matter */)
}

} // namespace djinni_generated
32 changes: 32 additions & 0 deletions test-suite/generated-src/jni/NativeTestInterfaceWithString.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

#pragma once

#include "djinni_support.hpp"
#include "test_interface_with_string.hpp"

namespace djinni_generated {

class NativeTestInterfaceWithString final : ::djinni::JniInterface<::testsuite::TestInterfaceWithString, NativeTestInterfaceWithString> {
public:
using CppType = std::shared_ptr<::testsuite::TestInterfaceWithString>;
using CppOptType = std::shared_ptr<::testsuite::TestInterfaceWithString>;
using JniType = jobject;

using Boxed = NativeTestInterfaceWithString;

~NativeTestInterfaceWithString();

static CppType toCpp(JNIEnv* jniEnv, JniType j) { return ::djinni::JniClass<NativeTestInterfaceWithString>::get()._fromJava(jniEnv, j); }
static ::djinni::LocalRef<JniType> fromCppOpt(JNIEnv* jniEnv, const CppOptType& c) { return {jniEnv, ::djinni::JniClass<NativeTestInterfaceWithString>::get()._toJava(jniEnv, c)}; }
static ::djinni::LocalRef<JniType> fromCpp(JNIEnv* jniEnv, const CppType& c) { return fromCppOpt(jniEnv, c); }

private:
NativeTestInterfaceWithString();
friend ::djinni::JniClass<NativeTestInterfaceWithString>;
friend ::djinni::JniInterface<::testsuite::TestInterfaceWithString, NativeTestInterfaceWithString>;

};

} // namespace djinni_generated
31 changes: 31 additions & 0 deletions test-suite/generated-src/objc/DBTestInterfaceWithString+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

#include "test_interface_with_string.hpp"
#include <memory>

static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file");

@class DBTestInterfaceWithString;

namespace djinni_generated {

class TestInterfaceWithString
{
public:
using CppType = std::shared_ptr<::testsuite::TestInterfaceWithString>;
using CppOptType = std::shared_ptr<::testsuite::TestInterfaceWithString>;
using ObjcType = DBTestInterfaceWithString*;

using Boxed = TestInterfaceWithString;

static CppType toCpp(ObjcType objc);
static ObjcType fromCppOpt(const CppOptType& cpp);
static ObjcType fromCpp(const CppType& cpp) { return fromCppOpt(cpp); }

private:
class ObjcProxy;
};

} // namespace djinni_generated

60 changes: 60 additions & 0 deletions test-suite/generated-src/objc/DBTestInterfaceWithString+Private.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

#import "DBTestInterfaceWithString+Private.h"
#import "DBTestInterfaceWithString.h"
#import "DJICppWrapperCache+Private.h"
#import "DJIError.h"
#import "DJIMarshal+Private.h"
#include <exception>
#include <stdexcept>
#include <utility>

static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file");

@interface DBTestInterfaceWithString ()

- (id)initWithCpp:(const std::shared_ptr<::testsuite::TestInterfaceWithString>&)cppRef;

@end

@implementation DBTestInterfaceWithString {
::djinni::CppProxyCache::Handle<std::shared_ptr<::testsuite::TestInterfaceWithString>> _cppRefHandle;
}

- (id)initWithCpp:(const std::shared_ptr<::testsuite::TestInterfaceWithString>&)cppRef
{
if (self = [super init]) {
_cppRefHandle.assign(cppRef);
}
return self;
}

- (nonnull NSString *)processString:(nonnull NSString *)message {
try {
auto objcpp_result_ = _cppRefHandle.get()->process_string(::djinni::String::toCpp(message));
return ::djinni::String::fromCpp(objcpp_result_);
} DJINNI_TRANSLATE_EXCEPTIONS()
}

namespace djinni_generated {

auto TestInterfaceWithString::toCpp(ObjcType objc) -> CppType
{
if (!objc) {
return nullptr;
}
return objc->_cppRefHandle.get();
}

auto TestInterfaceWithString::fromCppOpt(const CppOptType& cpp) -> ObjcType
{
if (!cpp) {
return nil;
}
return ::djinni::get_cpp_proxy<DBTestInterfaceWithString>(cpp);
}

} // namespace djinni_generated

@end
13 changes: 13 additions & 0 deletions test-suite/generated-src/objc/DBTestInterfaceWithString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file was generated by Djinni from string_view_test.djinni

#import <Foundation/Foundation.h>


/** @flag "--cpp-use-string-view true" */
@interface DBTestInterfaceWithString : NSObject

/** Method with string parameter to test string_view generation */
- (nonnull NSString *)processString:(nonnull NSString *)message;

@end
9 changes: 9 additions & 0 deletions test-suite/generated-src/outFileList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ djinni-output-temp/cpp/record_with_duration_and_derivings.cpp
djinni-output-temp/cpp/date_record.hpp
djinni-output-temp/cpp/date_record.cpp
djinni-output-temp/cpp/map_date_record.hpp
djinni-output-temp/cpp/test_interface_with_string.hpp
djinni-output-temp/cpp/DataRefTest.hpp
djinni-output-temp/cpp/constant_enum.hpp
djinni-output-temp/cpp/constant_with_enum.hpp
Expand Down Expand Up @@ -81,6 +82,7 @@ djinni-output-temp/java/TestDuration.java
djinni-output-temp/java/RecordWithDurationAndDerivings.java
djinni-output-temp/java/DateRecord.java
djinni-output-temp/java/MapDateRecord.java
djinni-output-temp/java/TestInterfaceWithString.java
djinni-output-temp/java/DataRefTest.java
djinni-output-temp/java/ConstantEnum.java
djinni-output-temp/java/ConstantWithEnum.java
Expand Down Expand Up @@ -150,6 +152,8 @@ djinni-output-temp/jni/NativeDateRecord.hpp
djinni-output-temp/jni/NativeDateRecord.cpp
djinni-output-temp/jni/NativeMapDateRecord.hpp
djinni-output-temp/jni/NativeMapDateRecord.cpp
djinni-output-temp/jni/NativeTestInterfaceWithString.hpp
djinni-output-temp/jni/NativeTestInterfaceWithString.cpp
djinni-output-temp/jni/NativeDataRefTest.hpp
djinni-output-temp/jni/NativeDataRefTest.cpp
djinni-output-temp/jni/NativeConstantEnum.hpp
Expand Down Expand Up @@ -263,6 +267,7 @@ djinni-output-temp/objc/DBDateRecord.h
djinni-output-temp/objc/DBDateRecord.mm
djinni-output-temp/objc/DBMapDateRecord.h
djinni-output-temp/objc/DBMapDateRecord.mm
djinni-output-temp/objc/DBTestInterfaceWithString.h
djinni-output-temp/objc/DBDataRefTest.h
djinni-output-temp/objc/DBConstantEnum.h
djinni-output-temp/objc/DBConstantWithEnum.h
Expand Down Expand Up @@ -355,6 +360,8 @@ djinni-output-temp/objc/DBDateRecord+Private.h
djinni-output-temp/objc/DBDateRecord+Private.mm
djinni-output-temp/objc/DBMapDateRecord+Private.h
djinni-output-temp/objc/DBMapDateRecord+Private.mm
djinni-output-temp/objc/DBTestInterfaceWithString+Private.h
djinni-output-temp/objc/DBTestInterfaceWithString+Private.mm
djinni-output-temp/objc/DBDataRefTest+Private.h
djinni-output-temp/objc/DBDataRefTest+Private.mm
djinni-output-temp/objc/DBConstantEnum+Private.h
Expand Down Expand Up @@ -471,6 +478,8 @@ djinni-output-temp/wasm/NativeDateRecord.hpp
djinni-output-temp/wasm/NativeDateRecord.cpp
djinni-output-temp/wasm/NativeMapDateRecord.hpp
djinni-output-temp/wasm/NativeMapDateRecord.cpp
djinni-output-temp/wasm/NativeTestInterfaceWithString.hpp
djinni-output-temp/wasm/NativeTestInterfaceWithString.cpp
djinni-output-temp/wasm/NativeDataRefTest.hpp
djinni-output-temp/wasm/NativeDataRefTest.cpp
djinni-output-temp/wasm/NativeConstantEnum.hpp
Expand Down
6 changes: 6 additions & 0 deletions test-suite/generated-src/ts/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export interface /*record*/ MapDateRecord {
datesById: Map<string, Date>;
}

/** @flag "--cpp-use-string-view true" */
export interface TestInterfaceWithString {
/** Method with string parameter to test string_view generation */
processString(message: string): string;
}

export interface DataRefTest {
sendData(data: Uint8Array): void;
retriveAsBin(): Uint8Array;
Expand Down
Loading
Loading