-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
966 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ compile_commands.json | |
.cache | ||
main.aot | ||
CTestCostData.txt | ||
libAnimal.so | ||
animal_export.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This file is part of Dartagnan. | ||
# | ||
# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
# SPDX-License-Identifier: MIT | ||
|
||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(Animal LANGUAGES CXX) | ||
|
||
set(CMAKE_CXX_VISIBILITY_PRESET hidden) | ||
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) | ||
|
||
set(SOURCES | ||
animal.cpp | ||
|
||
# For the sake of simplicity, we also add the generated binding glue here | ||
# Otherwise you'd need to build ./generated/AnimalBindings/CMakeLists.txt and get | ||
# a 2nd C++ library. | ||
generated/AnimalBindings/dart/ffi/Animal_c.cpp | ||
) | ||
|
||
|
||
# The Animal lib is the client C++ library, which we'll generate bindings for | ||
add_library(Animal SHARED ${SOURCES}) | ||
include(GenerateExportHeader) | ||
generate_export_header(Animal) | ||
|
||
set_property(TARGET Animal PROPERTY CXX_STANDARD 14) | ||
|
||
target_include_directories(Animal | ||
PUBLIC | ||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// This file is part of Dartagnan. | ||
/// | ||
/// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
/// SPDX-License-Identifier: MIT | ||
|
||
#include "animal.h" | ||
|
||
#include <iostream> | ||
|
||
void Animal::eat() | ||
{ | ||
std::cout << "eat from C++\n"; | ||
openMouth(); | ||
closeMouth(); | ||
} | ||
|
||
void Animal::openMouth() | ||
{ | ||
std::cout << "openMouth from C++\n"; | ||
} | ||
|
||
void Animal::closeMouth() | ||
{ | ||
std::cout << "closeMouth from C++\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/// This file is part of Dartagnan. | ||
/// | ||
/// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
/// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include "animal_export.h" | ||
|
||
class ANIMAL_EXPORT Animal | ||
{ | ||
public: | ||
// non-virtual! for example purposes | ||
void eat(); | ||
|
||
protected: | ||
virtual void openMouth(); | ||
virtual void closeMouth(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'package:AnimalBindings/Bindings.dart' as AnimalBindings; | ||
|
||
class MyDartAlligator extends AnimalBindings.Animal { | ||
@override | ||
void openMouth() { | ||
print("openMouth from Dart"); | ||
} | ||
} | ||
|
||
main() { | ||
final alligator = MyDartAlligator(); | ||
|
||
/// This will call C++ eat() via FFI | ||
/// which in turn will call into Dart to invoke openMouth() | ||
/// closeMouth() will be C++ to C++ directly, as it's not overridden in Dart | ||
alligator.eat(); | ||
|
||
// Prints: | ||
// eat from C++ | ||
// openMouth from Dart | ||
// closeMouth from C++ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'package:ffi/ffi.dart'; | ||
|
||
void main(List<String> args) { | ||
bool shouldLeak = args.contains("leak") || args.contains("--leak"); | ||
|
||
if (shouldLeak) { | ||
print("Leak!"); | ||
"foo".toNativeUtf8(); | ||
} else { | ||
print("No leak! Pass --leak to leak."); | ||
} | ||
} |
Oops, something went wrong.