Template support and collections

Open
#1,208 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
cpp, csharp

Research direction

Start by reviewing the linked CppSharp issues 836 and 1201, then reproduce the report with simple_vector.h and simple_vector.cpp under the stated MSVC settings. Inspect why the templated simple_vector is absent from generated C# and how array parameters and returns are represented. Done means template classes and collection/array interop are supported with clear generated C# output.

Written by the indexing model from the issue text.

Description

Brief Description

Presently there is not way, as far as I can tell, to pass arrays, vectors, or anything templated to between C# and C++.

According to this issue, template support has been completed, but it was speaking to the future. Are templates supported?
https://github.com/mono/CppSharp/issues/836
I saw this: https://github.com/mono/CppSharp/issues/1201

However, none of my C++ template classes are being represented on the C# side. Classes like unique_ptr are getting members generated, but ultimately are being treated as simply ptr fields. That's fine for my purposes, but the following class does not get a corresponding class in C#. In the example below, the public header is all that is being processed by CppSharp, and the pimpl implementation hiding pattern works fine everywhere else. However there is no SimpleVector class or simple_vector class on the C# side. I am even adding an explicit template instantiation which I tried to put in both the cpp file and the header.

simple_vector.h

#pragma once

#pragma warning( disable : 4251 )

#include <memory>
#include <mapping/export.h>
#include <mapping/spimpl.h>

namespace mapping
{
	template<typename T>
	class MAPPING_API simple_vector
	{	
	public:

		simple_vector();

	private:
		class Impl;
		spimpl::unique_impl_ptr<Impl> _pImpl;
	};
	
	template MAPPING_API class simple_vector<int>;
}

simple_vector.cpp

#include <mapping/simple_vector.h>
#include <vector>

namespace mapping
{
	template<class T>
	class simple_vector<T>::Impl
	{	
		std::vector<T> _vector;
	public:
	
		Impl() {}
	};

	template<class T>
	simple_vector<T>::simple_vector() : _pImpl(spimpl::make_unique_impl<Impl>()) {}

	template MAPPING_API class simple_vector<int>;
}

Furthermore, when trying to pass an array from C# to C++, it will always require an additional parameter for the length of the array. I also cannot figure out how to return an array from C++ to C# in anyway whatsoever. The best I can think to do is to create a pimpl style C++ class for every possible array type I'll be using - IntArray, DoubleArray, FooClassArray, etc. Yes, I could use macros on the C++ side to make things a little less painful, but it's still rather unwieldy.

OS: Windows

Used headers
Used settings

Target: MSVC

Other settings

Stack trace or incompilable generated code
Dominant language
C#
Stars
3.4k
Forks
541
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from mono/CppSharp

All issues in mono/CppSharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.