John Labenski wrote:
I've looked it over and I'm a little confused. We'd use a
a2dSmrtPtrList of template classes a2dSmrtPtr right?
That is what i use. In case of wxLua i suggest a normal wxList with
object a2dSmrtPtr.
My smart pointers work mostly on objects of type a2dObject, which has a
m_refcount.
In wxLua we wants to store wxObject pointers, but that object has no
refcount member.
So i want to wrap each wxObject pointer into a wxRefCount object, and
in the wxList we would store smart pointers to such objects.
So a list of:
a2dSmrtPtr< wxRefCount> // which is a list of a2dSmrtPtr where each has a
pointer to a wxRefCount object.
Now each time one item from this list is deleted, it will release the wxRefCount object only once. As soon as all smart pointer ( in the same or other list ) have bin deleted, and the last deletion makes the refcount in wxRefCount zero, the wxObject which was wrapped in there will be delted too.
Would we have to
have templated versions of the a2dSmrtPtr for every class type in
order for it to know how to delete it?
I think we only need one, since it will all be wxObject derived classes
which will be stored in wxRefCount.
Something like storing the object below using a list of
a2dSmartPtr<wxRefCount>.
class wxRefCount()
{
wxRefCount( wxObject* theObjectToWrap )
{
m_refcount = 0;
m_object = theObjectToWrap;
}
virtual ~wxRefCount()
{
wxASSERT_MSG( m_refcount == 0 || m_refcount ==
refcount_nondynamic, wxT("deleting wxRefCount while
referenced") );
delete m_object;
}
virtual wxRefCount* SmrtPtrOwn() {
m_refcount++; return this; }
virtual bool SmrtPtrRelease();
{
m_refcount--;
wxASSERT_MSG( m_refcount >= 0, wxT("wxRefCount Own/Release
not matched (extra Release calls)") ) ;
if ( m_refcount <= 0 )
{
delete this;
return true;
}
return false;
}
//!how many references to this object do exist
int m_refcount;
wxObject* m_object;
}
For example, you've got a class MyDataClass that you can create in C++
and push into lua or create in lua and pass to a C function that may
or may not delete it. Does this mean that your class has to be derived
from the template version of a2dSmrtPtr?
No, that class needs to be wrapped into the above, or if the class it
self has a baseclass having a m_refcount like above class
( like a2dObject which is derived from wxObject ), then it can be
directly stored it its own list if prefered.
But lets first just deal with wxObject derived classes wrapped in
wxRefCount, since in wxLua we do not have the luxury of being able to
have a baseclass in between wxObject and its derived widget classes etc.
What if someone has an
existing class from some other library, like wxWidgets, that they
cannot make it derived from a2dSmrtPtr?
a2dSmartPtr maintain a pointer to an object having a m_refcount. It
increments that refcount on creation of a new a2dSmartPtr, and then that
may be added to a list, of may just be on the stack. As sson as the
a2dSmrtPtr is deleted from the list, or gets out of scope, it will
decrement the m_refcount of the object its points too.
So in our case that will be wxRefCount objects. No the wxRefcount object
will be deleted truly when its m_refcount becomes zero.
At THAT moment its destructor is called, and the m_object will be
deleted too.
How would this work? How would
deleting the object be able to tell the smart pointer that it's gone?
Do you understand from the above?
If these issues can't be resolved I think it would be best to have
your C code pull the data from lua and take ownership of it. If you
wanted to get it back in lua you'd have a function to get it back by
using an id of some sort.
int DoStuffWithMyData(MyDataClass* data)
pull *data from lua and take ownership
return an int id to access the data again
MyDataClass* GetMyDataClass(int id)
return pointer to data based on id or null for none
Does this second way work for you? This way we don't have to force
every class to be derived from a2dSmrtPtr.
I never derived from a2dSmrtPtr, it is a template class around something
else.
What i suggest above is not requiring this, in fact the wxList to
store the wxObject * as wrapped smartpointer to wxRefCount objects, has
a method AppendObject( wxObject* ), that would already do.
The rest is taken care of internal.
The idea is to use C++ code to take care of deletion when now one uses
it anymore.
Shall i prepare something like the above, next you can choose ;-)
regards,
Klaas
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Wxlua-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wxlua-users