diff options
author | Andrew Morrow <acm@mongodb.com> | 2015-01-03 14:49:22 -0500 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2015-01-05 18:26:59 -0500 |
commit | e46896c23c4b411f4936fd41068d61a7922418a7 (patch) | |
tree | c22b213f76174495a5ee86323f2240687dc2c5da /src/mongo | |
parent | 939db8fd31790eff642abd446552294c66c5ff8a (diff) | |
download | mongo-e46896c23c4b411f4936fd41068d61a7922418a7.tar.gz |
SERVER-13256 Move ThreadSafeString to own file
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/db/curop.h | 1 | ||||
-rw-r--r-- | src/mongo/dbtests/basictests.cpp | 1 | ||||
-rw-r--r-- | src/mongo/util/goodies.h | 41 | ||||
-rw-r--r-- | src/mongo/util/progress_meter.h | 1 | ||||
-rw-r--r-- | src/mongo/util/thread_safe_string.cpp | 41 | ||||
-rw-r--r-- | src/mongo/util/thread_safe_string.h | 80 | ||||
-rw-r--r-- | src/mongo/util/util.cpp | 5 |
8 files changed, 125 insertions, 46 deletions
diff --git a/src/mongo/SConscript b/src/mongo/SConscript index 8122f1273f0..b4f4c7ea29b 100644 --- a/src/mongo/SConscript +++ b/src/mongo/SConscript @@ -68,6 +68,7 @@ env.Library('foundation', 'util/text.cpp', 'util/time_support.cpp', 'util/timer.cpp', + 'util/thread_safe_string.cpp', "util/touch_pages.cpp", "util/util.cpp", "util/startup_test.cpp", diff --git a/src/mongo/db/curop.h b/src/mongo/db/curop.h index 7b781e7051a..00e9af6b549 100644 --- a/src/mongo/db/curop.h +++ b/src/mongo/db/curop.h @@ -37,6 +37,7 @@ #include "mongo/util/concurrency/spin_lock.h" #include "mongo/util/net/hostandport.h" #include "mongo/util/progress_meter.h" +#include "mongo/util/thread_safe_string.h" #include "mongo/util/time_support.h" diff --git a/src/mongo/dbtests/basictests.cpp b/src/mongo/dbtests/basictests.cpp index 8a407685bb0..0072a78e785 100644 --- a/src/mongo/dbtests/basictests.cpp +++ b/src/mongo/dbtests/basictests.cpp @@ -40,6 +40,7 @@ #include "mongo/util/queue.h" #include "mongo/util/stringutils.h" #include "mongo/util/text.h" +#include "mongo/util/thread_safe_string.h" #include "mongo/util/time_support.h" namespace BasicTests { diff --git a/src/mongo/util/goodies.h b/src/mongo/util/goodies.h index 8c0c46835be..f311ac9dcde 100644 --- a/src/mongo/util/goodies.h +++ b/src/mongo/util/goodies.h @@ -38,7 +38,6 @@ #include <iostream> #include <sstream> -#include "mongo/base/string_data.h" #include "mongo/util/assert_util.h" namespace mongo { @@ -115,45 +114,5 @@ namespace mongo { typedef void *HANDLE; #endif - - /** - * this is a thread safe string - * you will never get a bad pointer, though data may be mungedd - */ - class ThreadSafeString : boost::noncopyable { - public: - ThreadSafeString( size_t size=256 ) - : _size( size ) , _buf( new char[size] ) { - memset( _buf , '\0' , _size ); - } - - ~ThreadSafeString() { - delete[] _buf; - } - - std::string toString() const { - return _buf; - } - - ThreadSafeString& operator=( const StringData& str ) { - size_t s = str.size(); - if ( s >= _size - 2 ) - s = _size - 2; - strncpy( _buf , str.rawData() , s ); - _buf[s] = '\0'; - return *this; - } - - bool empty() const { - return _buf[0] == '\0'; - } - - private: - const size_t _size; - char *const _buf; - }; - - std::ostream& operator<<(std::ostream &s, const ThreadSafeString &o); - } // namespace mongo diff --git a/src/mongo/util/progress_meter.h b/src/mongo/util/progress_meter.h index ce03e190a89..9b3dc744c64 100644 --- a/src/mongo/util/progress_meter.h +++ b/src/mongo/util/progress_meter.h @@ -30,6 +30,7 @@ #pragma once #include "mongo/util/goodies.h" +#include "mongo/util/thread_safe_string.h" #include <boost/noncopyable.hpp> #include <string> diff --git a/src/mongo/util/thread_safe_string.cpp b/src/mongo/util/thread_safe_string.cpp new file mode 100644 index 00000000000..ba6cebfc63e --- /dev/null +++ b/src/mongo/util/thread_safe_string.cpp @@ -0,0 +1,41 @@ +/** + * Copyright (C) 2015 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/platform/basic.h" + +#include <ostream> + +#include "mongo/util/thread_safe_string.h" + +namespace mongo { + + std::ostream& operator<<(std::ostream &s, const ThreadSafeString &o) { + return s << o.toString(); + } + +} // namespace mongo diff --git a/src/mongo/util/thread_safe_string.h b/src/mongo/util/thread_safe_string.h new file mode 100644 index 00000000000..dad3edcfe28 --- /dev/null +++ b/src/mongo/util/thread_safe_string.h @@ -0,0 +1,80 @@ +/** + * Copyright (C) 2015 MongoDB Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the GNU Affero General Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include <cstring> +#include <iosfwd> +#include <string> + +#include <boost/utility.hpp> + +#include "mongo/base/string_data.h" + +namespace mongo { + + /** + * this is a thread safe string + * you will never get a bad pointer, though data may be mungedd + */ + class ThreadSafeString : boost::noncopyable { + public: + ThreadSafeString( size_t size=256 ) + : _size( size ) , _buf( new char[size] ) { + memset( _buf , '\0' , _size ); + } + + ~ThreadSafeString() { + delete[] _buf; + } + + std::string toString() const { + return _buf; + } + + ThreadSafeString& operator=( const StringData& str ) { + size_t s = str.size(); + if ( s >= _size - 2 ) + s = _size - 2; + strncpy( _buf , str.rawData() , s ); + _buf[s] = '\0'; + return *this; + } + + bool empty() const { + return _buf[0] == '\0'; + } + + private: + const size_t _size; + char *const _buf; + }; + + std::ostream& operator<<(std::ostream &s, const ThreadSafeString &o); + +} // namespace mongo diff --git a/src/mongo/util/util.cpp b/src/mongo/util/util.cpp index f510aa6c31a..44e75df5d56 100644 --- a/src/mongo/util/util.cpp +++ b/src/mongo/util/util.cpp @@ -89,11 +89,6 @@ namespace mongo { } } utilTest; - ostream& operator<<( ostream &s, const ThreadSafeString &o ) { - s << o.toString(); - return s; - } - bool StaticObserver::_destroyingStatics = false; } // namespace mongo |