Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from MGTheTrain/feature/implement-csharp-bindin…
Browse files Browse the repository at this point in the history
…g-for-core-module

[Feature] - Implement csharp binding for core module
  • Loading branch information
MGTheTrain committed Apr 15, 2024
2 parents 15601bc + 86614f1 commit b12d0ed
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
set -euo pipefail

apt-get update
apt-get install -y cmake python3 python3-pip
pip3 install clang-format cpplint
apt-get install -y cmake python3 python3-pip pkg-config
#pip3 install clang-format cpplint

cd /tmp/
# vcpkg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail

apt-get update
apt-get install -y cmake python3 python3-pip
apt-get install -y cmake python3 python3-pip pkg-config
pip3 install clang-format cpplint

cd /tmp/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail

apt-get update
apt-get install -y cmake python3 python3-pip
apt-get install -y cmake python3 python3-pip pkg-config
pip3 install clang-format cpplint

cd /tmp/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -euo pipefail

apt-get update
apt-get install -y cmake python3 python3-pip
apt-get install -y cmake python3 python3-pip pkg-config
pip3 install clang-format cpplint

cd /tmp/
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ secrets.cfg
*.tar
*.dll
*.so
__pycache__
__pycache__
obj
bin
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.0] - 15-04-2024

### Added

- [Feature] Sample C# wrappers refering to C++ compiled source code resulting in shared libraries

### Fixed

- Rename, comment out lines and install missing apt dependencies in `install-dependencies.sh` bash scripts

## [0.1.0] - 15-04-2024

### Added
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A collection of sample code snippets demonstrating how to create bindings for va
- [x] Sample Python wrappers refering to C++ compiled source code resulting in shared libraries
- [ ] Sample Rust wrappers refering to C++ compiled source code resulting in shared libraries
- [ ] Sample Go wrappers refering to C++ compiled source code resulting in shared libraries
- [ ] Sample C# wrappers refering to C++ compiled source code resulting in shared libraries
- [x] Sample C# wrappers refering to C++ compiled source code resulting in shared libraries
- [ ] C++ audio module **(Optional, Experimental)**
- [ ] C++ video module **(Optional, Experimental)**

Expand Down Expand Up @@ -71,4 +71,8 @@ cd devops\scripts\ps1

#### Python

Navigate to the [bindings python folder](./bindings/python) after C++ source code compilation and run `python main.py --path <path to libcore_wrapper.so>`
Navigate to the [bindings python folder](./bindings/python) after C++ source code compilation and run `python main.py --path <path to libcore_wrapper.so>`

#### C#

Navigate to the [bindings c# folder](./bindings/csharp) after C++ source code compilation and follow comments in [Program.cs](./bindings/csharp/Mgtt.CoreWrapper/Program.cs)
Empty file removed bindings/c#/.gitignore
Empty file.
45 changes: 45 additions & 0 deletions bindings/csharp/Mgtt.CoreWrapper/CoreWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// The MIT License
//
// Copyright (c) 2024 MGTheTrain
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Runtime.InteropServices;

namespace Mgtt.CoreWrapper;
public class CoreWrapper
{
[DllImport("core_wrapper", EntryPoint = "add")]
public static extern int Add(int a, int b);

[DllImport("core_wrapper", EntryPoint = "subtract")]
public static extern int Subtract(int a, int b);

[DllImport("core_wrapper", EntryPoint = "multiply")]
public static extern int Multiply(int a, int b);

[DllImport("core_wrapper", EntryPoint = "divide")]
public static extern float Divide(float a, float b);

[DllImport("core_wrapper", EntryPoint = "getCircleArea")]
public static extern float GetCircleArea(float radius);

[DllImport("core_wrapper", EntryPoint = "getCircleCircumference")]
public static extern float GetCircleCircumference(float radius);
}
10 changes: 10 additions & 0 deletions bindings/csharp/Mgtt.CoreWrapper/Mgtt.CoreWrapper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
54 changes: 54 additions & 0 deletions bindings/csharp/Mgtt.CoreWrapper/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// The MIT License
//
// Copyright (c) 2024 MGTheTrain
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

namespace Mgtt.CoreWrapper;
class Program
{
static int Main(string[] args)
{
int resultAdd = CoreWrapper.Add(10, 5);
Console.WriteLine("Addition result: " + resultAdd);

int resultSubtract = CoreWrapper.Subtract(10, 5);
Console.WriteLine("Subtraction result: " + resultSubtract);

int resultMultiply = CoreWrapper.Multiply(10, 5);
Console.WriteLine("Multiplication result: " + resultMultiply);

float resultDivide = CoreWrapper.Divide(10.0f, 2.0f);
Console.WriteLine("Division result: " + resultDivide);

float radius = 5.0f;

float area = CoreWrapper.GetCircleArea(radius);
Console.WriteLine("Circle area: " + area);

float circumference = CoreWrapper.GetCircleCircumference(radius);
Console.WriteLine("Circle circumference: " + circumference);

return 0;
}
}

// 0. Execute `dotnet build`
// 0. Copy on unix systems the libcore_wrapper.so to the `bindings/c#/Mgtt.CoreWrapper/bin/Debug/net8.0` folder
// 1. Run `dotnet run`
22 changes: 22 additions & 0 deletions bindings/python/core_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# The MIT License
#
# Copyright (c) 2024 MGTheTrain
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import ctypes

class CoreWrapper:
Expand Down
22 changes: 22 additions & 0 deletions bindings/python/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
# The MIT License
#
# Copyright (c) 2024 MGTheTrain
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import argparse
import os
from core_wrapper import CoreWrapper
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.2.0

0 comments on commit b12d0ed

Please sign in to comment.