Crystal Space
Welcome, Guest. Please login or register.
September 02, 2014, 12:18:07 pm

Login with username, password and session length
Search:     Advanced search
9009 Posts in 2043 Topics by 8377 Members
Latest Member: Berys3
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  Using reference counting for own classes
« previous next »
Pages: [1] Print
Author Topic: Using reference counting for own classes  (Read 2254 times)
GangMan
Newbie
*
Posts: 6

18456839 gangman2000@hotmail.com
View Profile WWW
« on: January 09, 2006, 04:55:00 pm »

Hello!

I'm still new to Crystal Space so I hope this question doesn't look like a total newbie question.
I'm currently implementing a small application with CS and have therefore been confronted with the reference counting mechanisms that are being used (csRef).
I was now wondering if i could use this for my own classes as well. So that I can use csRef<MyClass> foo; instead of MyClass* foo;
I have been working through the manual but all I found out that I would have to implement IncRef() and DecRef() but I'm not sure how since I'm not too familiar with this ref counting implementation.

Could anyone please explain to me what I have to do to make reference counting available with my own classes?

Thank you in advance!

GangMan
Logged
sunshine
Administrator
Sr. Member
*****
Posts: 294


View Profile
« Reply #1 on: January 09, 2006, 10:25:38 pm »

You already answered your own question. As long as your class provides IncRef() and DecRef() methods, you can use it with csRef<>. You will also need to add a variable to your class in which you store the actual count of references, and that variable will be manipulated by IncRef() and DecRef(). DecRef() is slightly special in that it also has the responsibility of destroying the object when the reference count falls to zero. If you don't want to code this up manually, you can inherit from csRefCount, which is a simple base in <csutil/refcount.h>, implementing this behavior on your behalf.
Logged
GangMan
Newbie
*
Posts: 6

18456839 gangman2000@hotmail.com
View Profile WWW
« Reply #2 on: January 10, 2006, 12:23:41 am »

Subclassing csRefCount is just what I was looking for. I somehow assumed that IncRef() and DecRef() were in/decreasing a counter but I guess I would have forgotten about destroying the object.

Thanks for the quick help!

GangMan

P.S.: I'm not sure but I guess using the reference counting mechanism for self-written classes could be something more people are looking for. Maybe it's worth creating a FAQ or manual entry suggesting the usage of csutil/refcount.h?
Logged
sunshine
Administrator
Sr. Member
*****
Posts: 294


View Profile
« Reply #3 on: January 27, 2006, 05:58:03 am »

Quote
P.S.: I'm not sure but I guess using the reference counting mechanism for self-written classes could be something more people are looking for. Maybe it's worth creating a FAQ or manual entry suggesting the usage of csutil/refcount.h?

The csRefCount utility class is mentioned in the "Correctly Using Smart Pointers" section of the CS manual (presently section 4.4.2) in the first paragraph.

http://www.crystalspace3d.org/docs/online/manual/cs_4.4.2.php#4.4.2
Logged
GangMan
Newbie
*
Posts: 6

18456839 gangman2000@hotmail.com
View Profile WWW
« Reply #4 on: January 27, 2006, 03:39:52 pm »

The csRefCount utility class is mentioned in the "Correctly Using Smart Pointers" section of the CS manual (presently section 4.4.2) in the first paragraph.
I know. Still - for me as a beginner - it wasn't explained good enough, how to handle that functionality exactly. All I knew was that those two methods had to be implemented but not how. And if you don't know anything about how this particular ref-counting system works, its only helpful up to a certain point. Whereas a sentence like "You can include csutil/refcount.h" for a default implementation" might give novices just the hint they were looking for.

GangMan
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 4.136 seconds with 15 queries.