Resource Management... Howto?

Samuel Winchenbach swinchen at eece.maine.edu
Sat Jul 7 05:57:51 PDT 2007


Thanks Jarrett!

I sort of went with a hybrid approach (for now)

module resource;

class TextureHandle
{
     private Texture mTexture;
     private char[]  mName;
     this(texture aTexture, char[] aName)
     {
         mTexture = aTexture;
         mName = aName;

     }
     ~this()
     {
         mTexture.mRef--;
         if (mTexture.mRef == 0)
         {
             mTexture.freeGPUtexture();
             textureList.remove(aName);
             delete mTexture;
         }
     }

}

TextureHandle loadTexture(char[] aName)
{
     if (auto t = (aName in textureList))
     {
         (*t).mRef++;
         return new TextureHandle(*t, aName);

     }

     auto t = new Texture();
     textureList[aName] = t;
     t.mRef++;
     return new TextureHandle(t, aName);
}

private
{
     Texture[char[]] textureList;

     class Texture
     {
         private uint mRef = 0;
         this()
         {
         }

         ~this()
         {
         }

         void freeGPUtexture()
         {
         }
     }

}


I am trying to think ahead and see if this is what I really want. 
Eventually I will want to implement a file that contains all the various 
resources.   I am not sure if I will run into a scope problem with that 
or not.  I guess I just need to play around with it and see.  :) 
Somehow it also seems I like should be able to make this more generic... 
  instead of having a texture handle, have a generic resource handle. 
Overall it makes a LOT more sense to me in D than when I tried to do the 
same thing in C++ :)

Sam


More information about the Digitalmars-d-learn mailing list