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:
inline ~temp_file_base() { remove(); }
inline operator bool() const { return _path; }
inline operator bool() const { return _path.valid(); }
inline bool operator!() const { return !_path; }
const fc::path& path() const;
void remove();

View file

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

View file

@ -183,7 +183,10 @@ namespace fc {
bool valid()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(); }
const T& operator*()const { assert(_valid); return ref(); }

View file

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

View file

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

View file

@ -36,11 +36,11 @@ namespace fc {
auto lgr = logger::get( cfg.loggers[i].name );
// 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_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 ){

View file

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