Quote:
I have failed to understand the point of your example code
The point was that even without having "load/unload/isLoaded" functions your class can still present the same functionality.
Just use
RAII.
I don't see why I would ever want a Texture that didn't have any texture data. To me, such a class should be called a "maybe_texture". Just like std::string. The string is there once it is created. If you want a maybe_string, use std::string *, or a smart pointer.
So, write a class that *is* a texture. If you have one, you *have* a texture. Hell, write a lightweight wrapper for the actual texture data, to avoid copies:
class ActualTextureData{public: ActualTextureData(ResourceLoader loader, ...); // whatever args you need ~ActualTextureData();private: // forbid copies ActualTextureData(const ActualTextureData &); ActualTextureData &operator=(const ActualTextureData &); // whatever private data is required};class Texture // lightweight class{ Texture(ResourceLoader loader, ...); // again, whatever other argsprivate: boost::shared_ptr<ActualTextureData> actual_texture;};
So, lets examine this class.
Load()
- create a texture instance, fully loaded. Texture tex(loader,"file.png"
- to change the file, with an existing texture instance: tex = Texture(loader,"other.png");
Unload()
- the texture will be unloaded when it goes out of scope.
- the current texture data will be unloaded if assigned to by a new texture
isLoaded()
- always true.
Now, if we really desperately want a maybe_texture, we can use boost::shared_ptr<Texture> ptr. It is "unloaded" if the pointer is null. In all other cases it is loaded. You can force unloading: ptr.reset()
This example uses the texture class. It can be expanded to a level, a world, even an entire game. Yes, it takes a bit of work. If you are used to dealing with "init" and "destroy" functions, it takes effort to think about such a system. But it has its benefits.
Quote:
About that rule of 3 of yours
It isn't mine. Look at the references in that wikipedia page again. Does the name Bjarne Stroustrup ring any bells [smile]
Quote:
I'm 100% positive I don't need copy constructor nor copy assignment operator for any resource class I have - after all, there should be only one of each resource loaded - and after giving it more thought, I figured I don't need a destructor either
Remember, if you don't provide them, the compiler will be more than happy to. Any the compiler tends to do an extremely poor job with complex types.