summaryrefslogtreecommitdiff
path: root/util/goodies.h
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2010-02-26 11:33:20 -0500
committerEliot Horowitz <eliot@10gen.com>2010-02-26 11:33:20 -0500
commit066b8376cf67fef649d2bb346fe130a6b886fb37 (patch)
treed28551ef3e04eed9e3f486956f53bdc6c629595a /util/goodies.h
parent96d6221a9e1bfea31c0687bad8ef3530d73d4d84 (diff)
downloadmongo-066b8376cf67fef649d2bb346fe130a6b886fb37.tar.gz
thread safe string
Diffstat (limited to 'util/goodies.h')
-rw-r--r--util/goodies.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/util/goodies.h b/util/goodies.h
index 33d59bb41cb..6577b9db7e6 100644
--- a/util/goodies.h
+++ b/util/goodies.h
@@ -497,5 +497,46 @@ namespace mongo {
private:
TicketHolder * _holder;
};
+
+
+ /**
+ * this is a thread safe string
+ * you will never get a bad pointer, though data may be mungedd
+ */
+ class ThreadSafeString {
+ public:
+ ThreadSafeString( size_t size=256 )
+ : _size( 256 ) , _buf( new char[256] ){
+ memset( _buf , 0 , _size );
+ }
+
+ ~ThreadSafeString(){
+ delete _buf;
+ _buf = 0;
+ }
+
+ operator string() const {
+ return (string)_buf;
+ }
+
+ ThreadSafeString& operator=( const char * str ){
+ size_t s = strlen(str);
+ if ( s >= _size - 2 )
+ s = _size - 2;
+ strncpy( _buf , str , s );
+ _buf[s] = 0;
+ return *this;
+ }
+
+ bool empty() const {
+ return _buf[0] == 0;
+ }
+
+ private:
+ size_t _size;
+ char * _buf;
+ };
+
+ ostream& operator<<( ostream &s, const ThreadSafeString &o );
} // namespace mongo