using explicit bool operator for optional

This commit is contained in:
Daniel Larimer 2014-05-20 11:25:31 -04:00
parent 8a8ff28221
commit 17aefe29de
7 changed files with 16 additions and 13 deletions

View file

@ -159,7 +159,7 @@ namespace fc {
{ {
public: public:
inline ~temp_file_base() { remove(); } inline ~temp_file_base() { remove(); }
inline operator bool() const { return _path; } inline operator bool() const { return _path.valid(); }
inline bool operator!() const { return !_path; } inline bool operator!() const { return !_path; }
const fc::path& path() const; const fc::path& path() const;
void remove(); void remove();

View file

@ -29,7 +29,7 @@ namespace fc {
enum seekdir { beg, cur, end }; enum seekdir { beg, cur, end };
ifstream(); ifstream();
ifstream( const fc::path& file, int m ); ifstream( const fc::path& file, int m = binary);
~ifstream(); ~ifstream();
void open( const fc::path& file, int m ); void open( const fc::path& file, int m );

View file

@ -183,7 +183,10 @@ namespace fc {
bool valid()const { return _valid; } bool valid()const { return _valid; }
bool operator!()const { return !_valid; } bool operator!()const { return !_valid; }
operator bool()const { return _valid; }
// this operation is not safe and can result in unintential
// casts and comparisons, use valid() or !!
explicit operator bool()const { return _valid; }
T& operator*() { assert(_valid); return ref(); } T& operator*() { assert(_valid); return ref(); }
const T& operator*()const { assert(_valid); return ref(); } const T& operator*()const { assert(_valid); return ref(); }

View file

@ -238,7 +238,7 @@ namespace fc
variant( const optional<T>& v ) variant( const optional<T>& v )
{ {
memset( this, 0, sizeof(*this) ); memset( this, 0, sizeof(*this) );
if( v ) *this = variant(*v); if( v.valid() ) *this = variant(*v);
} }
template<typename T> template<typename T>

View file

@ -356,7 +356,7 @@ namespace fc {
void temp_file_base::remove() void temp_file_base::remove()
{ {
if (_path) if (_path.valid())
{ {
try try
{ {

View file

@ -36,11 +36,11 @@ namespace fc {
auto lgr = logger::get( cfg.loggers[i].name ); auto lgr = logger::get( cfg.loggers[i].name );
// TODO: finish configure logger here... // TODO: finish configure logger here...
if( cfg.loggers[i].parent ) { if( cfg.loggers[i].parent.valid() ) {
lgr.set_parent( logger::get( *cfg.loggers[i].parent ) ); lgr.set_parent( logger::get( *cfg.loggers[i].parent ) );
} }
lgr.set_name(cfg.loggers[i].name); lgr.set_name(cfg.loggers[i].name);
if( cfg.loggers[i].level ) lgr.set_log_level( *cfg.loggers[i].level ); if( cfg.loggers[i].level.valid() ) lgr.set_log_level( *cfg.loggers[i].level );
for( auto a = cfg.loggers[i].appenders.begin(); a != cfg.loggers[i].appenders.end(); ++a ){ for( auto a = cfg.loggers[i].appenders.begin(); a != cfg.loggers[i].appenders.end(); ++a ){

View file

@ -59,7 +59,7 @@ namespace fc
_path = fc::path( "/" ) / _lpath; _path = fc::path( "/" ) / _lpath;
#endif #endif
fc::getline( ss, _largs ); fc::getline( ss, _largs );
if( _args && _args->size() ) if( _args.valid() && _args->size() )
{ {
// TODO: args = fc::move(_args); // TODO: args = fc::move(_args);
} }
@ -88,16 +88,16 @@ namespace fc
{ {
fc::stringstream ss; fc::stringstream ss;
ss<<my->_proto<<"://"; ss<<my->_proto<<"://";
if( my->_user ) { if( my->_user.valid() ) {
ss << *my->_user; ss << *my->_user;
if( my->_pass ) { if( my->_pass.valid() ) {
ss<<":"<<*my->_pass; ss<<":"<<*my->_pass;
} }
ss<<"@"; ss<<"@";
} }
if( my->_host ) ss<<*my->_host; if( my->_host.valid() ) ss<<*my->_host;
if( my->_port ) ss<<":"<<*my->_port; if( my->_port.valid() ) ss<<":"<<*my->_port;
if( my->_path ) ss<<my->_path->generic_string(); if( my->_path.valid() ) ss<<my->_path->generic_string();
// if( my->_args ) ss<<"?"<<*my->_args; // if( my->_args ) ss<<"?"<<*my->_args;
return ss.str(); return ss.str();
} }