c# - Use shared_ptr of unsigned char array in SWIG -
i'm using swig make c++ code available in c#. 1 of tasks exchange data via std::shared_ptr<unsigned char>
representing array of unsigned chars. size given separately. think suitable representation in c# byte[]
.
unfortunately not able manage "translation". here examplary swig interface file:
%module example %include <stdint.i> %include <std_pair.i> %include <std_shared_ptr.i> %include <arrays_csharp.i> // result in compile error: "error c2679: binary '=' : no operator found takes right-hand operand of type 'unsigned char *' (or there no acceptable conversion)" //%apply unsigned char input[] {std::shared_ptr<unsigned char>} //%apply unsigned char output[] {std::shared_ptr<unsigned char>} %apply unsigned char input[] {unsigned char*} %apply unsigned char output[] {unsigned char*} %apply unsigned int {std::size_t} %template (mydatapair) std::pair<std::shared_ptr<unsigned char>, std::size_t>; %inline %{ #include <memory> typedef std::pair<std::shared_ptr<unsigned char>,std::size_t> mydatapair; struct mydatastruct { std::shared_ptr<unsigned char> data; std::size_t size; mydatastruct(std::shared_ptr<unsigned char> p_data, std::size_t p_size) : data(p_data), size(p_size) {} }; void setdatawithpointer(unsigned char* p_data, std::size_t p_size); void setdatawithsharedpointer(std::shared_ptr<unsigned char> p_data, std::size_t p_size); std::shared_ptr<unsigned char> getdataofpair(const mydatapair p_data) { return p_data.first; } std::shared_ptr<unsigned char> getdataofstruct(const mydatastruct p_data) { return p_data.data; } %}
the output looks (generated in example.cs):
public class example { public static void setdatawithpointer(byte[] p_data, uint p_size) { examplepinvoke.setdatawithpointer(p_data, p_size); } public static void setdatawithsharedpointer(swigtype_p_std__shared_ptrt_unsigned_char_t p_data, uint p_size) { examplepinvoke.setdatawithsharedpointer(swigtype_p_std__shared_ptrt_unsigned_char_t.getcptr(p_data), p_size); if (examplepinvoke.swigpendingexception.pending) throw examplepinvoke.swigpendingexception.retrieve(); } public static swigtype_p_std__shared_ptrt_unsigned_char_t getdataofpair(mydatapair p_data) { swigtype_p_std__shared_ptrt_unsigned_char_t ret = new swigtype_p_std__shared_ptrt_unsigned_char_t(examplepinvoke.getdataofpair(mydatapair.getcptr(p_data)), true); if (examplepinvoke.swigpendingexception.pending) throw examplepinvoke.swigpendingexception.retrieve(); return ret; } public static swigtype_p_std__shared_ptrt_unsigned_char_t getdataofstruct(mydatastruct p_data) { swigtype_p_std__shared_ptrt_unsigned_char_t ret = new swigtype_p_std__shared_ptrt_unsigned_char_t(examplepinvoke.getdataofstruct(mydatastruct.getcptr(p_data)), true); if (examplepinvoke.swigpendingexception.pending) throw examplepinvoke.swigpendingexception.retrieve(); return ret; } }
only first method, using raw pointer, has byte[]
in signature, rest type swigtype_p_std__shared_ptrt_unsigned_char_t
used, cannot used in practical way (or can it? far understand can passed around, not "really" used).
has dealt or similar problem before? self-defined typemaps?
Comments
Post a Comment