Skip to main content

Attributes and service classes to control behavior of CodePorting.Native Cs2Cpp when porting C# code to C++ automatically

Total Downloads
17.6K
17,573
Published
May 16, 2022
4y ago

Get Started

dotnet add package CodePorting.Native.Cs2Cpp.PortingControl --version 22.5.0
ProductVersions
Other
.NETFramework3.5.NETFramework4.7.2

README

Description

CodePorting.Native Cs2Cpp is a framework capable of translating C# code to C++. The resulting code provides the same API original .Net code had and can run in the native (unmanaged) C++ environment.

CodePorting.Native.Cs2Cpp.PortingControl package makes it possible to fine-tune the translated code. There are two ways to do so: attributes and service classes.

Attributes

The attributes defined in the CodePorting.Native.Cs2Cpp.PortingControl package are recognized by the code porter (translator) and affect how the output code looks. It is possible to remove, stub or rename members, use weak pointers instead of shared pointers, move arrays from heap to stack, alter inheritance scheme, change includes list, add C++-specific declarations (such as friend class, constexpr field or const method) and so on. To use these attributes, add reference to this package into the project being ported and use the attributes in the code. See the Attributes guide for more details.

Service classes

There are several service classes provided in the CodePorting.Native.Cs2Cpp.PortingControl package that may be used in the original C# code. When this code gets translated to C++, the porter replaces calls into these classes with calls into C++-specific versions of them, thus providing the means of inserting the C++-specific operations into the C# code. For example, the MemoryManagement class provides the ways to control the lifetime of objects in C++ (this should only be used occasionally, where the default translation doesn't work well) without doing any changes to C# code behavior. The Porter class provides the means to check if the code is being executed in C# or C++. Based on the result, one can choose an appropriate logic at runtime. See The Memory management model for more details.

Usage

Make your project depend on CodePorting.Native.Cs2Cpp.PortingControl:

<PackageReference Include="CodePorting.Native.Cs2Cpp.PortingControl" Version="*" />

Now you can use the attributes and service classes from your code:

using System;
using CsToCppPorter;
using CodePorting.Native.Cs2Cpp;

namespace PortedProject
{
    public abstract class MyClass
    {
        [CppWeakPtr, CppMutable]
        private MyClass Parent;

        [CppConstMethod]
        public void DoSomething()
        {
            Parent = this;
            if (Porter.IsPorted)
                PerformCppSpecificOperations();
            else
                PerformCsSpecificOperations();
        }

        protected abstract void PerformCppSpecificOperations();
        protected abstract void PerformCsSpecificOperations();
    }
}

Now, when you use the CodePorting.Native Cs2Cpp to translate your project to C++, the porter will recognize the attributes or service classes and generate the code accordingly.

Resources

Downloads by Version

Similar & Alternative Packages

Hand-picked matching NuGet libraries based on tags & ecosystem

Depended On By

Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

No SPDX license expression is published. Review the license text and your organisation's policy before redistribution.Compatibility metadata lists 2 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.