0

I'm trying to build a wrapper around a managed class so I can call it from native code.

Here is the managed function :

void MyManagedFunction(MyStruct iStruct)
{
  // Code according to what are the types of the 2 MyStruct members
}

struct MyStruct
{
  public MyStruct(Object iValue1, Object iValue2) : this()
  {
    Value1 = iValue1; // Often contains DateTime::Now
    Value2 = iValue2;
  }

  public Object Value1;
  public Object Value2;
}

In my case, Value1 will almost always be System::DateTime::Now and the Value2 will almost always be a common data type (int, double, float, string, bool). I thought of making two templated function in the wrapper.

In the wrapper's .h I have the following :

#ifdef MYWRAPPER_EXPORTS
#  define MYWRAPPER  __declspec(dllexport)
#else
#  define MYWRAPPER  __declspec(dllimport)
#endif  

class MYWRAPPER MyWrapper
{

public:
  MyWrapper();
  ~MyWrapper();

  template <class T> void MyNativeFunction(T iParam1)
  {
    MyStruct^ wStruct = gcnew MyStruct(System::DateTime::Now, iParam1);
    //The class containing the managed function is a singleton
    MyManagedClass::Instance->MyManagedFunction(wStruct); 
  }

  template <class T, class W> void MyNativeFunction(T iParam1, W iParam2)
  {
    MyStruct^ wStruct = gcnew MyStruct(iParam1, iParam2);
    //The class containing the managed function is a singleton
    MyManagedClass::Instance->MyManagedFunction(wStruct);
  }
};

This wrapper compiled without problem. The problem obviously occurred when I included the .h in the purely native code. Since I can't hide the content of the templated function, I have managed stuff visible on the native side which prevent the native code from compiling.

I was wondering if there was a workaround in order to achieve this. I don't mind if I'm limited into using only primitive types as parameters for the function. The best thing is if I was able to simply hide the content of the templated function in the native code so it only knows about the signature

Here's what I've tried/considered so far :

  • Converting the parameters to void* and call a function in which would call the managed one. By doing so, I can't cast the void* back to an object since I lose its type and using typeid to get the 'T' or 'W' type doesn't help since it vary from a compiler to another.
  • Overloading the function for every types I want to use. This is what I'll most likely use if I doesn't find a better solution. The problem is it means alot of overloading (especially for the 2 parameters function considering the number of combination)
Sim
  • 265
  • 1
  • 4
  • 14

1 Answers1

1

If you know all the types your template will take, you can force instantiate it with those variables and thus put the code for the template functions in the source file instead of a header.

You can look at the example provided in Storing C++ template function definitions in a .CPP file

As he says you can do as below (copy-paste alert):

.h file

class foo
{
public:
    template <typename T>
    void do(const T& t);
};

.cpp file

template <typename T>
void foo::do(const T& t)
{
    // Do something with t
}

template void foo::do<int>(const int&);
template void foo::do<std::string>(const std::string&);
Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • Using this approach with overloading in the case of the std::string or other complex types pretty much solved the problem. However, if I don't specialize a type (say double) in the cpp and later try to call the function using that type, I get an unclear error. Is there a way to assert that a specification of the used type exist and if not specify the error to show ? I've tried using : static_assert(sizeof(T) != sizeof(T), "No specialization found"); in the main function however it appears every time even if the type used is correct – Sim Dec 14 '12 at 17:02
  • Hmm, finally it seems even your solution wasn't working. It still gives me linking error. I've tried use the most basic function I could find (template void func();) and event this didn't work. I guess I don't have other choices but to use overloads with specific parameters types – Sim Dec 14 '12 at 18:46
  • I am surprised it doesnt work. It should work as long as you declare it with ALL the types that are going to be used. – Karthik T Dec 15 '12 at 05:41
  • Not sure why too. I was wondering if it might have to do with the fact I'm doing this in CLI. Anyway, I finished using functions overloads instead. By using a macro, I was able to make it simple enough. I'll try to post my solution tomorrow – Sim Dec 17 '12 at 02:47