FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[FIXED] How to simulate private class members

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
moechofe



Joined: 07 Oct 2006
Posts: 25
Location: Paris, France

PostPosted: Mon Apr 05, 2010 11:55 am    Post subject: [FIXED] How to simulate private class members Reply with quote

I'm using sfml and I make a class to manager sprite and animation.
I want to control animation, time, effect... with miniD because I'm too lazy to create a complex binary format to do the same thing.

So, I want to give access to the sprite class to miniD, but I don't want to give access to the internal part of my class: like pointer or other private members.


Last edited by moechofe on Wed Apr 21, 2010 12:52 am; edited 1 time in total
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Mon Apr 05, 2010 5:03 pm    Post subject: Reply with quote

If you're binding the class from D, this is exactly what the extra fields/bytes inside class instances are for. See the "newInstance" function in minid.interpreter http://svn.dsource.org/projects/minid/trunk/docs/minid.interpreter.html , as well as the page on making native classes: http://www.dsource.org/projects/minid/wiki/API2/Part5

If you're writing the class entirely in MiniD,.. there's not really any way to do it Wink
Back to top
View user's profile Send private message Visit poster's website
moechofe



Joined: 07 Oct 2006
Posts: 25
Location: Paris, France

PostPosted: Wed Apr 07, 2010 11:41 am    Post subject: Reply with quote

So. I can create a native class.
And I need to use newInstance with extraByte to hide my pointer from miniD side.

That means: I cannot instanciate the class from miniD with "new", isn't it?
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Wed Apr 07, 2010 8:20 pm    Post subject: Reply with quote

No, you can; that's why class allocators exist. Smile

You can take a look at the source for the Vector class (/trunk/minid/vector.d) for an example of how to write a class that uses a custom allocator. The page I linked also talks about them.
Back to top
View user's profile Send private message Visit poster's website
moechofe



Joined: 07 Oct 2006
Posts: 25
Location: Paris, France

PostPosted: Fri Apr 09, 2010 2:45 pm    Post subject: Reply with quote

Thanks,

I missed the "A First Attempt" section when I was reading the "Part5" article. What I want is writing here : allocator, checkIntParam...
Back to top
View user's profile Send private message
moechofe



Joined: 07 Oct 2006
Posts: 25
Location: Paris, France

PostPosted: Sat Apr 10, 2010 12:55 pm    Post subject: Reply with quote

I have a miniD class (with allocator and constructor) in a miniD module like this:
Code:
makeModule(t, "Engine", function uint( MDThread* t, size_t num_params)
{
  CreateClass(t, "Sprite", (CreateClass* c)
  {
    c.method("constructor", &constructor);
  });
  newFunction(t, &allocator, "Sprite.allocator");
  newGlobal(t, "Sprite");
  return 0;
});
importModuleNoNS(t, "Engine");


When I allocate it with
Code:
runString("local s = Engine.Sprite(...)");

I get the exception:
Code:
Engine.Sprite.allocator(native): newInstance - expected 'class' for base, not 'namespace'


I try several value for the 2nd parameter of newInstance, but I not sure what to do:
Code:
static size_t allocator(MDThread* t, size_t num_params)
{
  newInstance(t, 0, 0, Sprite.sizeof);
  ...
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sun Apr 11, 2010 1:24 pm    Post subject: Reply with quote

You're never setting the allocator function to the class, so the "Sprite" global variable is pointing at the allocator instead of at the class. You need to change it like so:

Code:
  CreateClass(t, "Sprite", (CreateClass* c)
  {
    c.method("constructor", &constructor);
  });
  newFunction(t, &allocator, "Sprite.allocator");
  setAllocator(t, -2); // <<<< this!
  newGlobal(t, "Sprite");


That should work. Smile
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group