Skip to content

Commit

Permalink
'better' LazyString
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Feb 20, 2009
1 parent 406442d commit 6f25a72
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 43 deletions.
4 changes: 1 addition & 3 deletions client/dbclient.h
Expand Up @@ -111,9 +111,7 @@ namespace mongo {
Query& where(const char *jscode) { return where(jscode, BSONObj()); }

string toString() const;
static string toString( void *o ) {
return ( (Query*) o )->toString();
}
operator string() const { return toString(); }
};

/** Typically one uses the QUERY(...) macro to construct a Query object.
Expand Down
4 changes: 1 addition & 3 deletions db/jsobj.h
Expand Up @@ -502,9 +502,7 @@ namespace mongo {
This is an abbreviated representation which might be used for logging.
*/
string toString() const;
static string toString( void *o ) {
return ( (BSONObj*) o )->toString();
}
operator string() const { return toString(); }

/** Properly formatted JSON string. */
string jsonString( JsonStringFormat format = Strict ) const;
Expand Down
8 changes: 2 additions & 6 deletions s/shard.h
Expand Up @@ -53,9 +53,7 @@ namespace mongo {
void split( const BSONObj& middle );

string toString() const;
static string toString( void *o ) {
return ( (Shard*) o )->toString();
}
operator string() const { return toString(); }

bool operator==(const Shard& s);

Expand Down Expand Up @@ -104,9 +102,7 @@ namespace mongo {
bool loadByName( const string& ns );

string toString() const;
static string toString( void *o ) {
return ( (ShardInfo*) o )->toString();
}
operator string() const { return toString(); }

private:
DBConfig * _config;
Expand Down
9 changes: 2 additions & 7 deletions stdafx.h
Expand Up @@ -70,12 +70,6 @@ using namespace std;

namespace mongo {

class Stringable {
public:
virtual ~Stringable() throw() {}
virtual string toString() const = 0;
};

/* these are manipulated outside of mutexes, so be careful */
struct Assertion {
Assertion() {
Expand Down Expand Up @@ -113,12 +107,13 @@ namespace mongo {
/* last assert of diff types: regular, wassert, msgassert, uassert: */
extern Assertion lastAssert[4];

class DBException : public exception, public Stringable {
class DBException : public exception {
public:
virtual const char* what() const throw() = 0;
virtual string toString() const {
return what();
}
operator string() const { return toString(); }
};

class AssertionException : public DBException {
Expand Down
47 changes: 23 additions & 24 deletions util/log.h
Expand Up @@ -20,22 +20,33 @@

namespace mongo {

// If you don't want your class to inherit from Stringable (for example, if
// you don't want a virtual destructor) then add a function like the
// following to your class, which takes a pointer to an object of your class
// type:
// static string toString( void * );
class LazyString {
public:
// LazyString is designed to be used in situations where the lifespan of
// a temporary object used to construct a LazyString completely includes
// the lifespan of the LazyString object itself.
template< class T >
LazyString( const T &t ) : obj_( (void*)&t ), fun_( &T::toString ) {}
string val() const { return (*fun_)(obj_); }
LazyString( const T& t ) {
if ( sizeof( Stringifier< T > ) > ( sizeof( Stringifier< unsigned > ) * 2 ) ) {
stringifier_ = &stringifierError_;
} else {
stringifier_ = new ( stringifierBuf_ ) Stringifier< T >( t );
}
}
string val() const { return stringifier_->val(); }
private:
void *obj_;
string (*fun_) (void *);
struct StringifierBase {
virtual ~StringifierBase() {}
virtual string val() const = 0;
};
template< class T >
struct Stringifier : public StringifierBase {
Stringifier( const T& t ) : t_( t ) {}
virtual string val() const { return string( t_ ); }
const T& t_;
};
static struct StringifierError : public StringifierBase {
virtual string val() const { return "Error converting to string"; }
} stringifierError_;
StringifierBase *stringifier_;
char stringifierBuf_[ sizeof( Stringifier< unsigned > ) * 2 ];
};

class Nullstream {
Expand Down Expand Up @@ -72,15 +83,9 @@ namespace mongo {
virtual Nullstream& operator<<(unsigned long long) {
return *this;
}
virtual Nullstream& operator<<(const string&) {
return *this;
}
virtual Nullstream& operator<<(const LazyString&) {
return *this;
}
virtual Nullstream& operator<<(const Stringable&) {
return *this;
}
virtual Nullstream& operator<< (ostream& ( *endl )(ostream&)) {
return *this;
}
Expand Down Expand Up @@ -109,17 +114,11 @@ namespace mongo {
Logstream& operator<<(void *x) LOGIT
Logstream& operator<<(long long x) LOGIT
Logstream& operator<<(unsigned long long x) LOGIT
Logstream& operator<<(const string& x) LOGIT
Logstream& operator<<(const LazyString& x) {
boostlock lk(mutex);
cout << x.val();
return *this;
}
Logstream& operator<<(const Stringable& x) {
boostlock lk(mutex);
cout << x.toString();
return *this;
}
Logstream& operator<< (ostream& ( *_endl )(ostream&)) {
boostlock lk(mutex);
cout << _endl;
Expand Down

0 comments on commit 6f25a72

Please sign in to comment.