Jump to content
  • Advertisement
Sign in to follow this  
DaesDemon

Function which take pointer declaration

This topic is 4799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello again, Sorry to disturb you again, but there are some things that disturb me: For example, i have a function which take two pointers on objects: void PopulateLights(PanelObjectlist*, SceneManager*); These two kind of object are registered in AS without special behaviour. I registered this function like this:
engine->RegisterGlobalFunction("void PopulateObjectList(PanelObjectList& in,SceneManager& in),asFUNCTION(LightPopulate),asCALL_CDECL);
and use it like this: 2 global property: - SceneManager sceneMgr; - PanelObjectlist control; and call: PopulateLights(control,sceneMgr); So, it worked, but as soon as i register one this type with size 0 it say that there is no copy operator. I have read that passing the params by ref requires the copy of the variable. 1) Is there another way to register this function to avoid the copying behaviour? 2) Will i need too a constructor behaviour and destructor(so wrapping function) for aggregate objects even if never create one of theses object by script, but only in application? If it is the case , i see that as a major problem for my implementation because of the loads of class i will have to manage. 3) Will there be a pointer implementation in the future of AS? I will be away for two weeks , so please excuse delay ;)

Share this post


Link to post
Share on other sites
Advertisement
Unfortunately, this is a limitation in AngelScript that I have yet to resolve in a satisfying manner.

Possible solutions:

You could register the assignment operator for the type, and let AngelScript take a copy of the object in order to send it by reference to your function. Or you can register the AddRef and Release behaviours and send the the objects by handle to the function.

I'm planning on implementing support for pointers again. However, pointers will not be safe in an open scripting environment, so if you use them you probably shouldn't allow users to change the scripts. The pointers won't be implemented anytime soon, though as I don't have much time and prioritize other things in AS.

Share this post


Link to post
Share on other sites
Thank you again WitchLord for explanations.

That is really a sad news for me but everything is not always already cooked ;) and far from me the idea to make you responsible for anything in my little problems, we are in the opensource world and everything i can say is thank you for your work.

Quote:
You could register the assignment operator for the type, and let AngelScript take a copy of the object in order to send it by reference to your function. Or you can register the AddRef and Release behaviours and send the the objects by handle to the function.


Do you mean that it would be enough to only register an assignement operator for each of my class used as parameter like this:
Object &Assign(Object *self, Object &other)
{
*self = other;
return *self;
}

engine->RegisterObjectBehaviour("object", asBEHAVE_ASSIGNMENT, "object &f(const object &in)", asFUNCTION(Assign), asCALL_CDECL_OBJFIRST);


So i could perhaps make a template
template<class T> T &Assign(T *self, T &other)
{
*self = other;
return *self;
}
engine->RegisterObjectBehaviour("object", asBEHAVE_ASSIGNMENT, "object &f(const object &in)", asFUNCTION(Assign<Object>), asCALL_CDECL_OBJFIRST);


It will add only a line for each class :) but would do a bit To bit copy of the class but it is what we want as we don't want a complete copy of the object , only a copy for a reference, so perhaps would it works.

[edit]Or perhaps a SharedPtr<T> version of each class that can allow me to use its addref and release to use the handles , but i haven't had the time to look into that for now[/edit]

I will see that in two weeks and probably go deeper into the AngelScript Source if i have some time too ( time is the problem ;) )

See you later, i'll probably come back with a better mind as i leave for hollyday :D

Thanks for all the good works.

Share this post


Link to post
Share on other sites
Hello everyone
I am back from hollyday, and i have a clearer mind than before, i hope ;)

So i come back with my problem as i don't see a solution for now.

To explain better what problem i am in front, i will take this example:

i have a c++ class:
class Engine
{
void removeCamera(Camera* camera);
}

I register it like that:
RegisterObjectMethod("Engine","void removeCamera(Camera& in),asMethod...)

and use it like that:
engine.removeCamera(camera);

So if i understand it well , what will happens is:
1) camera2 = camera // copy function by AngelScript
2) engine.removeCamera(&camera2);
So as my cameras are registered in the engine the method removeCamera will fail as it will not find the good pointer but a pointer on a camera that is not registered.
So as in the documentation, it is said that "it normally doesn't pass references to the true object" , is there a way to force the reference to the true object to avoid the copy?
If not , can someone explain me better how can i manage this kind of problems, assuming that i cannot modify my applications classes?
Would handles resolves my problems?

Thanks in advance ;)
Regards.

Share this post


Link to post
Share on other sites
I think handles would solve your problem, except you would have to register AddRef and Release to your classes in order for AS to allow you to use handles.

Share this post


Link to post
Share on other sites
Huum the problem is i cannot modify my application classes so i cannot change my return type to be of smartpointer type.

But perhaps could i add an external addref and release function like this:
[source language=cpp]
std::map<void*,int> ReferenceCount;

void CameraAddRef(Camera* cam)
{
//Check existence before... else create it
ReferenceCount[cam]++;
}
void CameraRelease(Camera* cam)
{
ReferenceCount[cam]--;
if (ReferenceCount[cam]==0)
{
delete cam; // and destroy the object map...
}
}
// Registered like this
engine->RegisterObjectBehaviour("Camera", asBEHAVE_ADDREF, "void f()", asFUNCTION(CameraAddRef), asCALL_CDECL_OBJFIRST);
engine->RegisterObjectBehaviour("Camera", asBEHAVE_RELEASE, "void f()", asFUNCTION(CameraRelease), asCALL_CDECL_OBJFIRST);




so using an external array to store reference count.( or even do nothing in the addref and release function as however i don't want AngelScript to delete my objects)

This way without modify the class i would be able to use the AngelScript handles.

Do you think that could be a possible solution?

Share this post


Link to post
Share on other sites
If your application takes care of memory management you could very well register the AddRef and Release behaviours as dummy functions that don't do anything. You'll have to figure out a way to guarantee that the scripts don't store a handle to the object before deleting it though.

You could also map the pointer to the object to a reference counter like you suggested, but it would probably be quite slow with a big overhead.

Another way to solve this is to use a wrapper object. Instead of passing a pointer to Camera object to the scripts, you instead pass a pointer to a wrapper class that contains a pointer to the real camera object. This would allow you to add reference counting to the wrapper class.

If you design the wrapper class to hold as local member the Camera class, you can even use the Camera's true methods on the wrapper class. Take a look at asCScriptString in the add on folder too see how this work.

Share this post


Link to post
Share on other sites
Oki thanks for all valuable informations.

I will try to generate dummy Addref and Release function as two template function as my script never have to create some object neither to delete some . All my app objects are managed by managers which do that, so i guess it should not be a problem.

I understand the wrapper class concept but it would force me to redeclare one class for each application objects that are returned as pointer so almost all of them and it would imply me too much work, (lazy boy ;) ).

The main problem was as i said before to be sure that the pointer send to my function is the real pointer not a pointer to a copyed object and if AS handles allow me that, i think it should work.

I will post back later for result, time to work now ;)
Thanks for help.
Regards.
DaesDemon

Share this post


Link to post
Share on other sites
I tried

// DUMMY FUNCTION FOR ANGELSCRIPTS HANDLES
void Addref(){}
void Release(){}

mASEngine->RegisterObjectBehaviour("Camera",asBEHAVE_ADDREF,"void f()",asFUNCTION(Addref),asCALL_CDECL);
mASEngine->RegisterObjectBehaviour("Camera",asBEHAVE_RELEASE,"void f()",asFUNCTION(Release),asCALL_CDECL);



but it throw me an error:NOT SUPPORTED.
That's sad as i could have use each time the same dummy function for all of my classes. I understant that i have to declare them as asCALL_CDECL_OBJFIRST one for each of my classes.( but it works with handles, or it seems for now ;) )

Perhaps a little add in the documentation defining the required Calling convention for registering behaviour could be benefic, although registering function that cannot get the object pointer for an Reference counting behaviour doesn't mean too much, but some crazy people can think about it (like me :) )

By the way , do you think this implementation can be easily done or is there a major problem to prevent the use of the CDECL Calling convention for reference counting?

I tried to use a template to design a generic function:
template<class T>Addref(T*) {}
and tried to instanciate it like this:
template<camera> void Addref(Camera*);
But in this case the compilation failed and i don't know why.

And for the end a little think on AngelScript:
I think it is quite disturbing in the documentation to talk about reference to an object for in fact a pointer to a copy of this object in AS. I am sure you have some important reasons to do that , but it is disturbing :)
Do you plan to change this behaviour( i mean reference as copy, not yours ;) ) later, or is it engraved in your vision of future AS?

Regards.
DaesDemon

[Edited by - DaesDemon on September 28, 2005 11:06:04 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!