irr_ptr

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Ethon
Posts: 21
Joined: Wed Nov 30, 2011 10:01 am

irr_ptr

Post by Ethon »

Hey.
Just a thing I am using to manage reference-counted Irrlicht objects.
It's a small smartpointer class which caries about deletion, copy + move semantics and therefore adds exception safety and safes you from remembering to call grab + drop directly.

http://pastebin.com/NjiEaHA3

Nothing special, but maybe there are people out there, which didn't wrap it yet. ;)
Regards,
Ethon
Last edited by Ethon on Sun Apr 08, 2012 3:53 pm, edited 1 time in total.
JVr
Posts: 28
Joined: Wed Oct 19, 2011 2:32 pm

Re: irr_ptr

Post by JVr »

There is something to do this in irrlicht -
http://irrlicht.sourceforge.net/docu/cl ... unted.html
I'm not sure i your class differ so much maybe do it as an extension to IReferenceCounted
Ethon
Posts: 21
Joined: Wed Nov 30, 2011 10:01 am

Re: irr_ptr

Post by Ethon »

Jup, it's a RAII wrapper around IReferenceCounted with an STL-Smartpointer adapted style. ;)
CuteAlien
Admin
Posts: 9733
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: irr_ptr

Post by CuteAlien »

It seems you are missing tests if you assign the pointer to itself as well as if you assign that irr_ptr to itself. For example you drop the old pointer before you grab a new one - if it's the same pointer and this is the last reference you just killed your pointer.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Re: irr_ptr

Post by Midnight »

I didn't wrap it.. I made a game engine, or making. I simply ripped off reference counter and added it to my engine which I plan to rewrite. Now you've provided some code I can use to expand, appreciated! 8)
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: irr_ptr

Post by gerdb »

Code: Select all

        
irr_ptr<element_type>& operator=(irr_ptr<element_type>&& other)
{
            drop();
            m_ptr = other.m_ptr();
            other.m_ptr = 0;
}
 
looks dangerous to me: no check if other == *this
Ethon
Posts: 21
Joined: Wed Nov 30, 2011 10:01 am

Re: irr_ptr

Post by Ethon »

Yeah, I know. ;)

Just in case people don't know how to fix it, I fixed it. New version uploaded.

http://pastebin.com/NjiEaHA3
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: irr_ptr

Post by REDDemon »

I'm guessing how many irr_ptr will be posted in future :)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Ethon
Posts: 21
Joined: Wed Nov 30, 2011 10:01 am

Re: irr_ptr

Post by Ethon »

REDDemon wrote:I'm guessing how many irr_ptr will be posted in future :)
Well, I think I was the first one, so don't blame me for another one. ;)
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: irr_ptr

Post by REDDemon »

:) I can understand if there are no similiar snippets in the same page :) anyway nice snippet. Is always nice to see 100+1 different ways for doing almost the same thing.
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: irr_ptr

Post by chronologicaldot »

I had a look at the code. I'm thinking this isn't going to work for things you get from the engine that you aren't supposed to drop (despite the fact that they inherit IReferenceCounted) - For example, images and things loaded from file - unless the first pointer used to store them is not of this pointer type.

Otherwise nice work.

EDIT: It'd be nice if there was a "quiet drop" - some way of setting the pointer to null without deleting the data.
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: irr_ptr

Post by REDDemon »

I'm considering a new type of "smart pointer". maybe i'll post the snippet. nor boost or other frameworks have this so probably is useless xD
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply