I'm currently playing around with this in an error code system...using a map to store the messages by index, creating error states and then using these to make error policies (ie FatalError, TolerableError, etc.). The beauty of using this combo (in addition to efficiency) is that the only place that needs extending is the errors[] array, the template automatically sets the correct size, so the entire setup is data driven.
// Returns the number of elements, pass an array.template<typename T, std::size_t N>inline std::size_t containerSize( T (&)[N] ){ return N;}// Error messages...struct ErrorMessages{ struct MapThunk { const int idx; const char * message; operator std::pair<const int,const std::string>() { return std::make_pair(idx,message); } }; static MapThunk errors[]; static const std::size_t size;};// Create map, control and add codes here...ErrorMessages::MapThunk ErrorMessages::errors[] = { {1,"This is error 1"}, {2,"This is error 2"}, {3,"This is error 3"}, {4,"This is error 4"},};// Get codes container size...const std::size_t ErrorMessages::size = containerSize(ErrorMessages::errors);// Define the codemap...struct ErrorMap{ typedef std::map<const int,const std::string> MapType; ErrorMap() : data(ErrorMessages::errors,ErrorMessages::errors+ErrorMessages::size) { } MapType data;};// Message accessors...struct Messages{ static std::string get(const int idx) { if (map.data.find(idx) == map.data.end()) throw std::runtime_error("Illegal error message index"); return map.data[idx]; } static ErrorMap map;};// Initialize the message map...ErrorMap Messages::map;// Error states...template <const int idx,typename T=std::ostream>struct ErrorStates{ static void die() { T stream; stream << "\nFATAL ERROR: " + Messages::get(idx) + ", exiting..." << std::endl; exit (1); } static void survive() { T stream; stream << "\nWARNING: " + Messages::get(idx) + ", continuing..." << std::endl; // Continue... }};// Error Policies...
--random
--random_thinkerAs Albert Einstein said: 'Imagination is more important than knowledge'. Of course, he also said: 'If I had only known, I would have been a locksmith'.