dimanche 15 mars 2020

Creating a generalized resource map without using strings?

Let's assume I want to create an object that will hold some arbitrary data.

// Pseudocode
class MyContainer {
    map<key, pair<void*, size>>;
}

The key in this case also identifies the kind of data stored in the void* (e.g an image, a struct of some kind, maybe even a function).

The most general way to del with this is have the key be a string. Then you can put whatever on earth you want and then you can just read it. As a silly example, the key can just be:

"I am storing a png image and the source file was at location/assets/image.png and today is sunday".

i.e you can encode whatever you want. This is however slow. A much faster alternative is using enumerators and your keys are then IMAGE, HASHMAP, FUNCTION, THE_ANSWER_TO_LIFE...

However that requires you know every single case you need to handle beforehand and create an enumerator for it manually (which is tedious and not very extensible).

Is there a compromise that can be made? i.e something that uses only one key but is faster than strings and more extensible than enums?

Aucun commentaire:

Enregistrer un commentaire