About Monkey 2 › Forums › Monkey 2 Development › some issues with external abstract method
This topic contains 4 replies, has 2 voices, and was last updated by
abakobo
2 years, 6 months ago.
-
AuthorPosts
-
September 2, 2016 at 4:28 pm #3612
I met two troubles with external abstract classes:
1-private is not accessible to overriden methods declared in mx2 (may be the same when just overriden)
2-you can’t pass by reference to pure virtual methods that had been implemented in mx2 tough by value and by pointer is ok (pointers are value?). And you can pass by reference to non overriden external classes. (may be the same when just overriden)Issues posted on github with example files (attached to this post too) as I suppose these are bugs or at least limitations.
Point 2 prevents me from cleanly implementing the box2d b2Draw class in mx2 (wich is essential for debugging). Hope I won’t have to wrap it..
Attachments:
September 2, 2016 at 10:26 pm #3618private is not accessible to overriden methods declared in mx2 (may be the same when just overriden)
Use ‘Protected’ instead for when you want only subclasses to be able to access members.
you can’t pass by reference to pure virtual methods that had been implemented in mx2
This is because mx2 has no concept of ‘const &’ so there is no way it can generate c++ code with ‘const &’ params.
So your MyVM(i:Int) override does not in fact override the native superclass method (which takes a const int& param), so the superclass remains abstract.
The best solution in this case is probably to create a glue class for problematic methods like this, eg:
Monkey12345678910111213141516171819202122// c++class Foo_glue : public Foo{//mx2 incompatible!virtual void MyVM( const int &i ){//pass to mx2 compatible version...MyVM( i );}//mx2 compatible!virtual void MyVM_glue( int i ){}};' mx2ExternClass Foo Extends Void="Foo_glue"Method MyVM( i:int ) Virtual="MyVM_glue"EndIt’s kind of ugly, but you only have to glue up the non-mx2 compatible methods. I did something like this for litehtml and it wasn’t too bad.
Or, if you can find a ‘C’ wrapper, that’s probably even better!
I’m not sure why the author went with ‘const int&’ here in the first place (or perhaps this is just a test case?) – plain ‘int’ would do the same thing and be faster (and be compatible with mx2!) – but that’s c++ for you. There are just so many ways to do things it’s all too easy to pick a crappy way without even knowing it.
Unfortunately, this wont work in every case, eg: with factory systems where you don’t get a chance to subclass. But it should work for this.
I ultimately intend to use libclang or swig to write a c/c++ extern generator (I actually think this should be quite high priority, but I’ll get the mobile stuff out of the way first) which will help with writing lib modules and perhaps SOME of the glue stuff, but there will likely always be problematic c++ types.
October 3, 2016 at 2:53 pm #4242can we now pass by reference to externals? I have some const& that are passing fine now though I tought I would have to remove them from c++ code!
edit: it was not an abstract method so that must be the point.
October 3, 2016 at 7:23 pm #4250C++ allows you to pass a ‘value’ to a ‘value&’ param, although you need to be a bit careful as unless it’s a ‘const value&’ the c++ code can modify the value you pass it.
So if the c++ interface looks like this:
Monkey12void blah( int &r );…then it’s possible that after calling this…
Monkey123Local i:=10blah( i )…that i no longer contains 10!
However, with this:
Monkey12void blah( const int &r );You can be sure blah does not change the value of ‘r’, thanks to the ‘const’ prefix.
(Also, you can replace ‘int’ in all these examples with any custom struct name and the same applies).
You can think of ‘&’ (reference) params as being very similar to pointers, only the compiler does the ‘varptr’ bit for you automatically (which implies arguments must be variables or you can’t varptr them), eg: this…
Monkey12void blah( int *i );Monkey123Local i:=10blah( Varptr i )…will actually produce very similar/identical assembly code to the first example above, so you can see how it’s possible for blah to modify ‘i’ as it really has a pointer to ‘i’.
Probably just confused things there…
October 4, 2016 at 6:22 am #4264I just forgot that passing by reference was an issue with abstract method only.
However it should faster by value.. -
AuthorPosts
You must be logged in to reply to this topic.