I'm currently trying to reduce redundant disk writes from my editor, which stores it's objects in some storage class.
So i decided to add a flag to the storage. If the storage goes offline, it is saved to disk only if the flag is set, otherwise it's just deleted because i know the file on disk is still up to date.
To avoid a need to go through all code and setting the flag on any changes, i came up with this idea (code is member functions of a StorageManager class):
const SceneNode* GetNode (const Link link) const
{
const DynamicStorage* storage = GetStorage (link);
if (!storage) return NULL;
// not setting the flag. Becasue we get a const pointer, there won't be changes to the node
if (link.index<0 || link.index>=storage->nodes.size()) return NULL;
return &storage->nodes[link.index];
}
SceneNode* GetNode (const Link link)
{
DynamicStorage* storage = GetStorage (link);
if (!storage) return NULL;
storage->hasChanged = true; // <- setting the flag here, because if we get a non const pointer, we likely do some changes to the node
if (link.index<0 || link.index >= storage->nodes.size()) return NULL;
return &storage->nodes[link.index];
}
See the comments to follow my thought. Let me know if you think that's a bad idea in general.
I think it should work, but it doesn't.
If i do something like this:
const SceneNode *node = GetNode(link);
The non const getter is called, so the flag is accidently set. Although i try to make clear i want a const pointer.
What do i miss here? I never payed attention to this language detail before.
I guess i could solve it this way:
const SceneNode *node = ((const StorageManager*)this)->GetNode(link);
To make clear not only the returned pointer is const, but also the Manager.
But then i'd need to go through all code again, which is what i've hoped to avoid.