Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# / dotnet target for diplomat-tool #124

Merged
merged 19 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/src/ast/enums.rs
Expand Up @@ -4,7 +4,7 @@ use super::utils::get_doc_lines;
use super::Method;

/// A fieldless enum declaration in an FFI module.
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq, Eq)]
pub struct Enum {
pub name: String,
pub doc_lines: String,
Expand Down
2 changes: 1 addition & 1 deletion core/src/ast/structs.rs
Expand Up @@ -42,7 +42,7 @@ impl From<&syn::ItemStruct> for Struct {
/// A struct annotated with [`diplomat::opaque`] whose fields are not visible.
/// Opaque structs cannot be passed by-value across the FFI boundary, so they
/// must be boxed or passed as references.
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq, Eq)]
pub struct OpaqueStruct {
pub name: String,
pub doc_lines: String,
Expand Down
2 changes: 1 addition & 1 deletion core/src/ast/types.rs
Expand Up @@ -13,7 +13,7 @@ use super::{Enum, Method, OpaqueStruct, Path, Struct, ValidityError};
use crate::Env;

/// A type declared inside a Diplomat-annotated module.
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq, Eq)]
pub enum CustomType {
/// A non-opaque struct whose fields will be visible across the FFI boundary.
Struct(Struct),
Expand Down
22 changes: 22 additions & 0 deletions feature_tests/c/CountedOpaque.h
@@ -0,0 +1,22 @@
#ifndef CountedOpaque_H
#define CountedOpaque_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct CountedOpaque CountedOpaque;
#include "Counter.h"

CountedOpaque* CountedOpaque_new(const Counter* counter);
void CountedOpaque_destroy(CountedOpaque* self);

#ifdef __cplusplus
}
#endif
#endif
23 changes: 23 additions & 0 deletions feature_tests/c/Counter.h
@@ -0,0 +1,23 @@
#ifndef Counter_H
#define Counter_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Counter Counter;

Counter* Counter_new();

size_t Counter_count(const Counter* self);
void Counter_destroy(Counter* self);

#ifdef __cplusplus
}
#endif
#endif
25 changes: 25 additions & 0 deletions feature_tests/c/Float64Vec.h
@@ -0,0 +1,25 @@
#ifndef Float64Vec_H
#define Float64Vec_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Float64Vec Float64Vec;

Float64Vec* Float64Vec_new(const double* v_data, size_t v_len);

void Float64Vec_fill_slice(const Float64Vec* self, double* v_data, size_t v_len);

void Float64Vec_set_value(Float64Vec* self, const double* new_slice_data, size_t new_slice_len);
void Float64Vec_destroy(Float64Vec* self);

#ifdef __cplusplus
}
#endif
#endif
27 changes: 27 additions & 0 deletions feature_tests/c/MyString.h
@@ -0,0 +1,27 @@
#ifndef MyString_H
#define MyString_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct MyString MyString;

MyString* MyString_new(const char* v_data, size_t v_len);

void MyString_set_str(MyString* self, const char* new_str_data, size_t new_str_len);

void MyString_get_str(const MyString* self, DiplomatWriteable* writeable);

void MyString_make_uppercase(char* v_data, size_t v_len);
void MyString_destroy(MyString* self);

#ifdef __cplusplus
}
#endif
#endif
21 changes: 21 additions & 0 deletions feature_tests/c/OwnershipEater.h
@@ -0,0 +1,21 @@
#ifndef OwnershipEater_H
#define OwnershipEater_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct OwnershipEater OwnershipEater;

OwnershipEater* OwnershipEater_new();
void OwnershipEater_destroy(OwnershipEater* self);

#ifdef __cplusplus
}
#endif
#endif
2 changes: 2 additions & 0 deletions feature_tests/cpp/docs/index.rst
Expand Up @@ -6,7 +6,9 @@ Documentation
:caption: Modules:

option_ffi
ownership_ffi
result_ffi
slices_ffi
structs_ffi

Indices and tables
Expand Down
20 changes: 20 additions & 0 deletions feature_tests/cpp/docs/ownership_ffi.rst
@@ -0,0 +1,20 @@
``ownership::ffi``
==================

.. cpp:class:: CountedOpaque

.. cpp:function:: static CountedOpaque new_(const Counter& counter)

.. cpp:class:: Counter

Counts how many distinct ``CountedOpaque`` objects are instanciated

.. cpp:function:: static Counter new_()

.. cpp:function:: size_t count() const

.. cpp:class:: OwnershipEater

"Ownership is a delicous dish." — OwnershipEater

.. cpp:function:: static OwnershipEater new_()
22 changes: 22 additions & 0 deletions feature_tests/cpp/docs/slices_ffi.rst
@@ -0,0 +1,22 @@
``slices::ffi``
===============

.. cpp:class:: Float64Vec

.. cpp:function:: static Float64Vec new_(const diplomat::span<double> v)

.. cpp:function:: void fill_slice(diplomat::span<double> v) const

.. cpp:function:: void set_value(const diplomat::span<double> new_slice)

.. cpp:class:: MyString

.. cpp:function:: static MyString new_(const std::string_view v)

.. cpp:function:: void set_str(const std::string_view new_str)

.. cpp:function:: template<typename W> void get_str_to_writeable(W& writeable) const

.. cpp:function:: std::string get_str() const

.. cpp:function:: static void make_uppercase(std::string_view v)
22 changes: 22 additions & 0 deletions feature_tests/cpp/include/CountedOpaque.h
@@ -0,0 +1,22 @@
#ifndef CountedOpaque_H
#define CountedOpaque_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct CountedOpaque CountedOpaque;
#include "Counter.h"

CountedOpaque* CountedOpaque_new(const Counter* counter);
void CountedOpaque_destroy(CountedOpaque* self);

#ifdef __cplusplus
}
#endif
#endif
45 changes: 45 additions & 0 deletions feature_tests/cpp/include/CountedOpaque.hpp
@@ -0,0 +1,45 @@
#ifndef CountedOpaque_HPP
#define CountedOpaque_HPP
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <algorithm>
#include <memory>
#include <variant>
#include <optional>
#include "diplomat_runtime.hpp"

namespace capi {
#include "CountedOpaque.h"
}

class Counter;
class CountedOpaque;

/**
* A destruction policy for using CountedOpaque with std::unique_ptr.
*/
struct CountedOpaqueDeleter {
void operator()(capi::CountedOpaque* l) const noexcept {
capi::CountedOpaque_destroy(l);
}
};
class CountedOpaque {
public:
static CountedOpaque new_(const Counter& counter);
inline const capi::CountedOpaque* AsFFI() const { return this->inner.get(); }
inline capi::CountedOpaque* AsFFIMut() { return this->inner.get(); }
inline CountedOpaque(capi::CountedOpaque* i) : inner(i) {}
CountedOpaque() = default;
CountedOpaque(CountedOpaque&&) noexcept = default;
CountedOpaque& operator=(CountedOpaque&& other) noexcept = default;
private:
std::unique_ptr<capi::CountedOpaque, CountedOpaqueDeleter> inner;
};

#include "Counter.hpp"

inline CountedOpaque CountedOpaque::new_(const Counter& counter) {
return CountedOpaque(capi::CountedOpaque_new(counter.AsFFI()));
}
#endif
23 changes: 23 additions & 0 deletions feature_tests/cpp/include/Counter.h
@@ -0,0 +1,23 @@
#ifndef Counter_H
#define Counter_H
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "diplomat_runtime.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct Counter Counter;

Counter* Counter_new();

size_t Counter_count(const Counter* self);
void Counter_destroy(Counter* self);

#ifdef __cplusplus
}
#endif
#endif
47 changes: 47 additions & 0 deletions feature_tests/cpp/include/Counter.hpp
@@ -0,0 +1,47 @@
#ifndef Counter_HPP
#define Counter_HPP
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <algorithm>
#include <memory>
#include <variant>
#include <optional>
#include "diplomat_runtime.hpp"

namespace capi {
#include "Counter.h"
}

class Counter;

/**
* A destruction policy for using Counter with std::unique_ptr.
*/
struct CounterDeleter {
void operator()(capi::Counter* l) const noexcept {
capi::Counter_destroy(l);
}
};
class Counter {
public:
static Counter new_();
size_t count() const;
inline const capi::Counter* AsFFI() const { return this->inner.get(); }
inline capi::Counter* AsFFIMut() { return this->inner.get(); }
inline Counter(capi::Counter* i) : inner(i) {}
Counter() = default;
Counter(Counter&&) noexcept = default;
Counter& operator=(Counter&& other) noexcept = default;
private:
std::unique_ptr<capi::Counter, CounterDeleter> inner;
};


inline Counter Counter::new_() {
return Counter(capi::Counter_new());
}
inline size_t Counter::count() const {
return capi::Counter_count(this->inner.get());
}
#endif