Category: Proxy usage

Q

Can I extend proxy types in C++?

A

Yes and no. At the current time [Codemesh runtime v.3.2] you can extend a proxy type in C++, but if you use an instance of that type with a Java API, all extended information might get lost.

This question is typically asked when a developer would like to add some data fields to a proxy type by extending it, for example:

class MyVector : public java::util::Vector
{
private:
    int myCppDataElement;
public:
    MyVector() : 
        java::util::Vector( _use_java_ctor ),
        myCppDataElement( 0 ) 
    {
    }
 
    ...
};

You can use this type in your C++ application and it will work just as expected unless you do something like this:

MyVector      myv;
Hashtable     ht( _use_java_ctor );
 
ht.put( "my vector", myv );

After executing this snippet, the Hashtable ht contains a regular Vector instance. The value of myCppData is not conveyed to the Java side; infact, all extended type information is lost the moment the instance crosses the language boundary. myv of course still has all that information, but there currently simply is no mechanism to transport the C++ information into Java.

Consequently, the answer to your question is: it depends on your expectations! If you just need the extended information on the C++ side, you can extend a Java type to your heart's content. If you want to convey the extended information into Java, the answer is mostly that you can't do it (please check out callbacks for the exception to this rule).

Also please be aware that you can't extend multiple proxy types at all!

Think about what the ability to extend more than one Java type would imply: in C++, you would have a type that pretends to extend a Java class and implement several Java interfaces. What would the Java side make of that? The inheritance hierarchy only exists in C++ and Java would not have a corresponding type available. Also, there would be no way to ensure that you don't violate Java inheritance rules (only one superclass).


Copyright 2006-2011 by Codemesh, Inc., ALL RIGHTS RESERVED

:
frequently asked questions
home products support customers partners newsroom about us contact us