> The traditional way to make class names stand out is to use CamelCase style.
The traditional Java / Microsoft way.
The traditional C and C++ way is the way shown by the standard C++ library:
std::recursive_timed_mutex,
std::priority_queue,
std::basic_ios etc.
And if the names are common enough to warrant emphasizing that it is a type, then suffix with
_type or
_t:
std::string::size_type,
std::size_t,
std::regex_constants::match_flag_type etc.
Boost is quite traditional.
IMHO.
> does anyone have a better suggestion than using a map to get the status names?
I wouldn't use a map to look up four entries.
But, more fundamentally: I wouldn't pollute the enclosing namespace with order status.
An unscoped enum at class scope is preferable to a scoped enum at namespace scope.
I would do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <string>
#include <cassert>
#include <stdexcept>
struct order
{
enum class status : unsigned int { NEW_ORDER = 0, CONFIRMED = 1, SHIPPED = 2, FILLED = 3 };
static std::string to_string( status s ) ;
// ...
};
inline std::ostream& operator<< ( std::ostream& stm, order::status s )
{ return stm << order::to_string(s) ; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
// ------------- cpp file -----------------
namespace
{
constexpr const char* const order_status_string[] =
{ "NEW_ORDER", "CONFIRMED", "SHIPPED", "FILLED" } ;
inline bool initialized( order::status s ) { return s <= order::status::FILLED ; }
}
std::string order::to_string( order::status s )
{
assert( "order::status has been initialized" && initialized(s) ) ;
static const std::string prefix = "order::" ;
if( initialized(s) ) return prefix + order_status_string[ std::size_t(s) ] ;
throw std::domain_error( "from 'order::to_string(order::status)': uninitialized order::status" ) ;
}
int main()
{
try
{
std::cout << order::status::CONFIRMED << std::endl ;
order::status uninitialized ;
std::cout << uninitialized << '\n' ;
}
catch( const std::exception& e )
{
std::cerr << "*** error: " << e.what() << '\n' ;
}
}
|
http://coliru.stacked-crooked.com/a/f5c079c5ce521744