[SOLVED] Ogre 1.9 SharedPtr

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

[SOLVED] Ogre 1.9 SharedPtr

Post by cool_thomas »

So I am using the code from here.

Any way to delete or move this topic since it is in wrong thread??

The page states the code is incompatible with 1.9; I had been using 1.8 but recently switched over to 1.9

The first issue was with pUseCount in TextFilePtr in the linked page, here is the code for my class which is essentially a copy and paste:

Code: Select all

class Cube27ResFilePtr : public Ogre::SharedPtr< Cube27ResFile > {
         public:
            Cube27ResFilePtr() : Ogre::SharedPtr< Cube27ResFile >() {}
            explicit Cube27ResFilePtr( Cube27ResFile *rep ) : Ogre::SharedPtr< Cube27ResFile >( rep ) {}
            Cube27ResFilePtr( const Cube27ResFilePtr &r ) : Ogre::SharedPtr< Cube27ResFile >( r ) {}
            Cube27ResFilePtr( const Ogre::ResourcePtr &r ) : Ogre::SharedPtr< Cube27ResFile >() {
                if ( r.isNull() )
                    return;
                // lock and copy other mutex pointer
                OGRE_LOCK_MUTEX( *r.OGRE_AUTO_MUTEX_NAME )
                OGRE_COPY_AUTO_SHARED_MUTEX( r.OGRE_AUTO_MUTEX_NAME )
                pRep = static_cast< Cube27ResFile* >( r.getPointer() );
                pUseCount = r.useCountPointer();
                useFreeMethod = r.freeMethod();
                if ( pUseCount ) {
                    ++( *pUseCount );
                }
            }

            // Operator used to convert a ResourcePtr to Cube27ResFilePtr
            Cube27ResFilePtr &operator = ( const Ogre::ResourcePtr &r ) {
                if ( pRep == static_cast< Cube27ResFile* >( r.getPointer() ) )
                     return *this;
                 release();
                 if( r.isNull() )
                     return *this;
                     // resource ptr is null, so the call to release above has done all we need to do.
                 // lock & copy other mutex pointer
                 OGRE_LOCK_MUTEX( *r.OGRE_AUTO_MUTEX_NAME );
                 OGRE_COPY_AUTO_SHARED_MUTEX( r.OGRE_AUTO_MUTEX_NAME );
                 pRep           = static_cast< Cube27ResFile* >( r.getPointer() );
                 pUseCount      = r.useCountPointer();
                 useFreeMethod  = r.freeMethod();
                 if ( pUseCount ) {
                     ++( *pUseCount );
                 }
                 return *this;
            }
     };
I gives this error:

Code: Select all

error: 'pUseCount' was not declared in this scope
I can see pUseCount is in the OgreSharedPtr.h in 1.8 but not in 1.9; I fixed this by just adding unsigned int* in front of my own pUseCount.

This results in:

Code: Select all

error: 'const ResourcePtr' has no member named 'useCountPointer'
error: 'useFreeMethod' was not declared in this scope
error: 'const ResourcePtr' has no member named 'freeMethod'
Were this methods replaced with one of another name, is there a different way of doing this?

I would happily update the wiki page to reflect these changes for 1.9 if anyone would mind explaining how I might fix the issue, or at least give direction on what to look for.

Thanks.

SOLUTION posted at http://www.ogre3d.org/forums/viewtopic.php?f=2&t=81235
Last edited by cool_thomas on Wed Jun 25, 2014 9:58 pm, edited 3 times in total.
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: Ogre 1.9 SharedPtr

Post by cool_thomas »

Sorry I posted this in the wrong forum anyway to move it to help? Sorry for the inconvenience and thanks.
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: Ogre 1.9 SharedPtr

Post by cool_thomas »

This is finally what I came up with, please let me know if it is correct or if it's likely to cause problems at some point.

Code: Select all

class Cube27ResFilePtr : public Ogre::SharedPtr< Cube27ResFile > {
         public:
            Cube27ResFilePtr() : Ogre::SharedPtr< Cube27ResFile >() {}
            explicit Cube27ResFilePtr( Cube27ResFile *rep ) : Ogre::SharedPtr< Cube27ResFile >( rep ) {}
            Cube27ResFilePtr( const Cube27ResFilePtr &r ) : Ogre::SharedPtr< Cube27ResFile >( r ) {}
            Cube27ResFilePtr( const Ogre::ResourcePtr &r ) : Ogre::SharedPtr< Cube27ResFile >() {
                if ( r.isNull() )
                    return;
                *this = r.staticCast<Cube27ResFile>();
            }
}
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: Ogre 1.9 SharedPtr

Post by cool_thomas »

So I would like to update the wiki page regarding this: http://www.ogre3d.org/tikiwiki/tiki-ind ... ceManagers

My question is, do I need to do it in a way that the 1.8 stuff is still available or just update it to 1.9?

Is there an example page I can look at? I'll look around to see which answer I get first. Also before I do I'd like feedback on the above fix to make sure it's acceptable or if I am doing something wrong.

Thanks.
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: Ogre 1.9 SharedPtr

Post by cool_thomas »

Okay my fix is not correct as it gives a segfault of:

Code: Select all

Program received signal SIGSEGV, Segmentation fault.
In __pthread_self_lite () ()
Debugger finished with status 0
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: Ogre 1.9 SharedPtr

Post by cool_thomas »

Okay I solved this by referring to http://www.ogre3d.org/forums/viewtopic.php?f=1&t=79505 except that the following code would not work for me:

Code: Select all

Ogre::ResourcePtr resource;
      Ogre::MaterialPtr mat = resource.static_cast<Ogre::Material>();
I simply omitted the copy constructor and operator= method and in the Load method did:

Code: Select all

Cube27ResFilePtr Cube27ResFileManager::load( const Ogre::String &name, const Ogre::String &group ) {
        LOG( "Looking for resource: " + name );
        Cube27ResFilePtr resf = Cube27ResFilePtr( static_cast<Cube27ResFile*>( getResourceByName( name ).get() ) );

        if ( resf.isNull() ) {
            resf = Cube27ResFilePtr( static_cast<Cube27ResFile*>( createResource( name, group ).get() ) );
            LOG( "Resource not found, creating." );
        }

        LOG( "Resource " + name + " loaded." );
        resf->load();
        return resf;
    }
So I will go ahead and update the wiki page in a few times, giving time for anyone to contribute to this if there is a better or more elegant way of fixing the issue.

Thanks.
Spekoda
Halfling
Posts: 40
Joined: Wed Jun 12, 2013 5:38 pm
Location: Finland
x 2

Re: Ogre 1.9 SharedPtr

Post by Spekoda »

Hei!
The Ogre::SharedPtr< T >::staticCast() const, should work I had to start using it in 1.9 to get compiled.

I do the following in my code and it has always worked (taken from the ogre caelum plugin, see ogre addons, I had to fix the static_cast to use the 1.9 smart pointer .staticCast):

Code: Select all

		MaterialPtr checkLoadMaterialClone (
				const String& originalName,
				const String& cloneName )
		{
			MaterialPtr scriptMaterial = MaterialManager::getSingletonPtr()->getByName(originalName).staticCast< Material > ();
			if (scriptMaterial.isNull()) 
			{
				// Throw exception: Material 'originalName' should have been loaded before this function was called.
                                 THROW_UNSUPPORTED_EXCEPTION (
						"Can't find material \"" + originalName + "\"",
						"Source");
			}

			// Create clone
			MaterialPtr clonedMaterial (scriptMaterial->clone (cloneName));

			// Test clone loads and there is at least on supported technique
			clonedMaterial->load ();
			if (clonedMaterial->getBestTechnique () == 0) 
			{
				THROW_UNSUPPORTED_EXCEPTION (
						"Can't load material \"" + originalName + "\": " + clonedMaterial->getUnsupportedTechniquesExplanation(), 
						"Source");
			}
			return clonedMaterial;
		}
User avatar
cool_thomas
Kobold
Posts: 29
Joined: Wed Oct 09, 2013 3:41 pm

Re: Ogre 1.9 SharedPtr

Post by cool_thomas »

Hmm I wonder why .staticCast() wouldn't work for me. Maybe I will try it again to see if it works.

Thanks for the reply :)
Post Reply