Smallest Useful Attribute System Part 2

Reader Alexey provided some useful feedback on The Smallest Useful Attribute System. The first point I want to address is the extra copies of strings. When I embedded the strings in the map and the AttributeDescriptor, I had been thinking that if the strings were part of a DLL, and the DLL got unloaded, there would be trouble. On further reflection, it occurred to me that I had over-thought it. If a DLL got unloaded, and then one attempted to fetch attributes on the now dead classes, there are bigger problems going on. So, here's the fix to the string copies; we go from one static string built into the data and two dynamic copies to just the static string.

class AttributeBinder { ...snip... class AttributeDescriptor { ...snip...
 AttributeDescriptor(const char* name, const std::type_info& ti, ptrdiff_t off) : name(name), ti(&ti), off(off) { }
 const char* name; ...snip...
 }; class Predicate { public: bool operator()(char const*const& lhs, char const*const& rhs) const  { return strcmp(lhs, rhs) < 0; } }; std::map<const char*, AttributeDescriptor, Predicate> attribs; }; 

The std::string has been replaced by const char*, and the map is supplied with an ordering predicate that does the comparisons from the name of the attribute.

Click to read the original post: The Smallest Useful Attribute System.


Content by Nick Porcino (c) 1990-2011