1
SFML wiki / [Sources] Thread-safe Resource Manager
« on: September 16, 2009, 01:15:59 pm »Quote from: "l0calh05t"
I disagree. Most of the time, a resource that failed to load is not a catastrophic failure. A texture did not load? Ok, just use a default texture (maybe a nice hot pink :-P ). A font didn't load? Use the default font.
I think you must have a very different use case in mind than me. If I'm writing a game and a texture fails to load, it means that my game data is corrupted somehow. Even it I want to proceed with fallback data, I'm already at the stuff-is-screwed-up-who-cares-how-fast-it-is point.
Quote
I can't think of any example in the STL that this would apply to.
It really depends a lot on what you're doing in the loop. For instance, in a loop that iterates over a vector and sums the elements, the call to end() will dwarf the actual body of the loop.
As a more realistic example of something (not in the STL) where this comes up is when people write depth-first tree iterators. The "end" state for such an iteration is something like "all nodes are in the (implicit) visited set," so calling end() involves instantiating a set and adding all the nodes to it.
The other place I see things like this is when the exit comparison is more complicated, like "I == MyMap[Foo].end()". Even that's not so bad, but it builds up as you add more indirections.
Plus, if you cache end() by default, your code becomes more self-documenting in that cases where you don't cache it must be because you're doing invalidating mutation, though having comments to that effect is still a good idea.
Quote
Besides, I'm deleting elements in this loop, so in the case where nontrivial work is done for end, it is likely that the cached iterator would become invalidated, so it poses an extra risk of undefined behavior.
Yup. And I put a giant comment to that effect. ;-)
Quote
If you don't mind the extra entries (which especially for games that stream large amounts of resources might be a lot), you could just as well use weak pointers so that the deletion is taken care of automatically (and spread over multiple frames).
True, though boost::weak_ptr doesn't play nice with my other suggestion to avoid double-lookups. ;-)