Type erased objects

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Type erased objects

Jeremy Cowles
Hey all,

I have a type erased object, VtValue, which works like Boost.Any and can hold different C++ types. I *think* I want C++ functions returning VtValue to return C# "object" instead and let the user cast to the appropriate type.

Does this seem like the right approach or should VtValue be exposed as a first class thing in C#? It seems like exposing VtValue will end up in recreating bits of the C# type system which "just work" when dealing with object.

If I should return object, how do I wrap a function that does the type dispatch? More specifically, I know how to setup the type dispatch part, I just don't know how to convert to the various C++ objects into C# objects from within C++ and how to get swig to return "object" from that function.

Thanks!

--
Jeremy

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Type erased objects

Andrew Haining

Bear in mind the type swig needs to return needs an integer member large enough to store a pointer. So in this case it couldn’t return an object directly. It could return an internal type and then downcast that type to object in the interface. To do this you’d need to write a swig typemap to do the conversion.

 

From: Jeremy Cowles [mailto:[hidden email]]
Sent: 24 September 2016 19:53
To: [hidden email]
Subject: [Swig-user] Type erased objects

 

Hey all,

 

I have a type erased object, VtValue, which works like Boost.Any and can hold different C++ types. I *think* I want C++ functions returning VtValue to return C# "object" instead and let the user cast to the appropriate type.

 

Does this seem like the right approach or should VtValue be exposed as a first class thing in C#? It seems like exposing VtValue will end up in recreating bits of the C# type system which "just work" when dealing with object.

 

If I should return object, how do I wrap a function that does the type dispatch? More specifically, I know how to setup the type dispatch part, I just don't know how to convert to the various C++ objects into C# objects from within C++ and how to get swig to return "object" from that function.

 

Thanks!

 

--

Jeremy


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________



Digital Barriers are world leaders in visually intelligent solutions for the global surveillance, security and safety markets, specialising in zero-latency streaming and analysis of secure video and related intelligence over wireless networks, including cellular, satellite, IP mesh and cloud. www.digitalbarriers.com Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Type erased objects

Jeremy Cowles
I ended up creating implicit cast operators for VtValue to/from the supported types, so now the code looks like this:

VtValue v = stringAttr.GetValue();
VtValue v3 = vec3Attr.GetValue();  

-or-

string str = stringAttr.GetValue();
GfVec3f vec = vec3Attr.GetValue();

And I'm pretty happy with that, using object would have actually been less pretty, requiring an explicit cast. For efficiency, I may introduce overloads on GetValue()-like functions so you can skip the type erasure completely:

string str; 
GfVec3f vec;
stringAttr.GetValue(out str);
vec3Attr.GetValue(out vec);

Thanks!

--
Jeremy

On Mon, Sep 26, 2016 at 1:09 AM Andrew Haining <[hidden email]> wrote:

Bear in mind the type swig needs to return needs an integer member large enough to store a pointer. So in this case it couldn’t return an object directly. It could return an internal type and then downcast that type to object in the interface. To do this you’d need to write a swig typemap to do the conversion.

 

From: Jeremy Cowles [mailto:[hidden email]]
Sent: 24 September 2016 19:53
To: [hidden email]
Subject: [Swig-user] Type erased objects

 

Hey all,

 

I have a type erased object, VtValue, which works like Boost.Any and can hold different C++ types. I *think* I want C++ functions returning VtValue to return C# "object" instead and let the user cast to the appropriate type.

 

Does this seem like the right approach or should VtValue be exposed as a first class thing in C#? It seems like exposing VtValue will end up in recreating bits of the C# type system which "just work" when dealing with object.

 

If I should return object, how do I wrap a function that does the type dispatch? More specifically, I know how to setup the type dispatch part, I just don't know how to convert to the various C++ objects into C# objects from within C++ and how to get swig to return "object" from that function.

 

Thanks!

 

--

Jeremy


______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________



Digital Barriers are world leaders in visually intelligent solutions for the global surveillance, security and safety markets, specialising in zero-latency streaming and analysis of secure video and related intelligence over wireless networks, including cellular, satellite, IP mesh and cloud. www.digitalbarriers.com Digital Barriers e-Mail Confidentiality and Disclaimer This message contains confidential information and is intended only for the individual named. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mail transmission cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of e-mail transmission. The recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Digital Barriers plc is a company registered in England and Wales. Registered number: 7149547. Registered office: Cargo Works, 1-2 Hatfields, London SE1 9PG, United Kingdom. For further information about Digital Barriers, please visit www.digitalbarriers.com.
______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
______________________________________________________________________
------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user