Skip to content

Commit

Permalink
Added an example
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Apr 9, 2024
1 parent bbb36b4 commit 1275fa7
Show file tree
Hide file tree
Showing 23 changed files with 966 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ compile_commands.json
.cache
main.aot
CTestCostData.txt
libAnimal.so
animal_export.h
33 changes: 33 additions & 0 deletions example/CMakeLists.txt
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}/>
)
25 changes: 25 additions & 0 deletions example/animal.cpp
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";
}
19 changes: 19 additions & 0 deletions example/animal.h
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();
};
22 changes: 22 additions & 0 deletions example/app/bin/main.dart
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++
}
12 changes: 12 additions & 0 deletions example/app/bin/test_leak.dart
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.");
}
}
Loading

0 comments on commit 1275fa7

Please sign in to comment.