Skip to content

Commit 2302026

Browse files
committed
base to cpp11
1 parent f9da26d commit 2302026

9 files changed

Lines changed: 47 additions & 51 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set(CXX_FLAGS
2424
-Wwrite-strings
2525
-march=native
2626
# -MMD
27-
# -std=c++0x
27+
-std=c++0x
2828
-rdynamic
2929
)
3030
if(CMAKE_BUILD_BITS EQUAL 32)

‎muduo/base/AsyncLogging.cc‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ AsyncLogging::AsyncLogging(const string& basename,
1313
running_(false),
1414
basename_(basename),
1515
rollSize_(rollSize),
16-
thread_(boost::bind(&AsyncLogging::threadFunc, this), "Logging"),
16+
thread_(std::bind(&AsyncLogging::threadFunc, this), "Logging"),
1717
latch_(1),
1818
mutex_(),
1919
cond_(mutex_),
@@ -35,11 +35,11 @@ void AsyncLogging::append(const char* logline, int len)
3535
}
3636
else
3737
{
38-
buffers_.push_back(currentBuffer_.release());
38+
buffers_.push_back(std::move(currentBuffer_));
3939

4040
if (nextBuffer_)
4141
{
42-
currentBuffer_ = boost::ptr_container::move(nextBuffer_);
42+
currentBuffer_ = std::move(nextBuffer_);
4343
}
4444
else
4545
{
@@ -73,12 +73,12 @@ void AsyncLogging::threadFunc()
7373
{
7474
cond_.waitForSeconds(flushInterval_);
7575
}
76-
buffers_.push_back(currentBuffer_.release());
77-
currentBuffer_ = boost::ptr_container::move(newBuffer1);
76+
buffers_.push_back(std::move(currentBuffer_));
77+
currentBuffer_ = std::move(newBuffer1);
7878
buffersToWrite.swap(buffers_);
7979
if (!nextBuffer_)
8080
{
81-
nextBuffer_ = boost::ptr_container::move(newBuffer2);
81+
nextBuffer_ = std::move(newBuffer2);
8282
}
8383
}
8484

@@ -98,7 +98,7 @@ void AsyncLogging::threadFunc()
9898
for (size_t i = 0; i < buffersToWrite.size(); ++i)
9999
{
100100
// FIXME: use unbuffered stdio FILE ? or use ::writev ?
101-
output.append(buffersToWrite[i].data(), buffersToWrite[i].length());
101+
output.append(buffersToWrite[i]->data(), buffersToWrite[i]->length());
102102
}
103103

104104
if (buffersToWrite.size() > 2)
@@ -110,14 +110,16 @@ void AsyncLogging::threadFunc()
110110
if (!newBuffer1)
111111
{
112112
assert(!buffersToWrite.empty());
113-
newBuffer1 = buffersToWrite.pop_back();
113+
newBuffer1 = std::move(buffersToWrite.back());
114+
buffersToWrite.back();
114115
newBuffer1->reset();
115116
}
116117

117118
if (!newBuffer2)
118119
{
119120
assert(!buffersToWrite.empty());
120-
newBuffer2 = buffersToWrite.pop_back();
121+
newBuffer2 = std::move(buffersToWrite.back());
122+
buffersToWrite.back();
121123
newBuffer2->reset();
122124
}
123125

‎muduo/base/AsyncLogging.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
#include <muduo/base/LogStream.h>
1111

12-
#include <boost/bind.hpp>
1312
#include <boost/noncopyable.hpp>
1413
#include <boost/scoped_ptr.hpp>
15-
#include <boost/ptr_container/ptr_vector.hpp>
1614

1715
namespace muduo
1816
{
@@ -58,8 +56,8 @@ class AsyncLogging : boost::noncopyable
5856
void threadFunc();
5957

6058
typedef muduo::detail::FixedBuffer<muduo::detail::kLargeBuffer> Buffer;
61-
typedef boost::ptr_vector<Buffer> BufferVector;
62-
typedef BufferVector::auto_type BufferPtr;
59+
typedef std::vector<std::unique_ptr<Buffer>> BufferVector;
60+
typedef BufferVector::value_type BufferPtr;
6361

6462
const int flushInterval_;
6563
bool running_;

‎muduo/base/Thread.cc‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include <boost/static_assert.hpp>
1212
#include <boost/type_traits/is_same.hpp>
13-
#include <boost/weak_ptr.hpp>
1413

1514
#include <errno.h>
1615
#include <stdio.h>
@@ -65,11 +64,11 @@ struct ThreadData
6564
typedef muduo::Thread::ThreadFunc ThreadFunc;
6665
ThreadFunc func_;
6766
string name_;
68-
boost::weak_ptr<pid_t> wkTid_;
67+
std::weak_ptr<pid_t> wkTid_;
6968

7069
ThreadData(const ThreadFunc& func,
7170
const string& name,
72-
const boost::shared_ptr<pid_t>& tid)
71+
const std::shared_ptr<pid_t>& tid)
7372
: func_(func),
7473
name_(name),
7574
wkTid_(tid)
@@ -79,7 +78,7 @@ struct ThreadData
7978
{
8079
pid_t tid = muduo::CurrentThread::tid();
8180

82-
boost::shared_ptr<pid_t> ptid = wkTid_.lock();
81+
std::shared_ptr<pid_t> ptid = wkTid_.lock();
8382
if (ptid)
8483
{
8584
*ptid = tid;

‎muduo/base/Thread.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <muduo/base/Atomic.h>
1010
#include <muduo/base/Types.h>
1111

12-
#include <boost/function.hpp>
12+
#include <functional>
13+
#include <memory>
1314
#include <boost/noncopyable.hpp>
14-
#include <boost/shared_ptr.hpp>
1515
#include <pthread.h>
1616

1717
namespace muduo
@@ -20,7 +20,7 @@ namespace muduo
2020
class Thread : boost::noncopyable
2121
{
2222
public:
23-
typedef boost::function<void ()> ThreadFunc;
23+
typedef std::function<void ()> ThreadFunc;
2424

2525
explicit Thread(const ThreadFunc&, const string& name = string());
2626
#ifdef __GXX_EXPERIMENTAL_CXX0X__
@@ -44,7 +44,7 @@ class Thread : boost::noncopyable
4444
bool started_;
4545
bool joined_;
4646
pthread_t pthreadId_;
47-
boost::shared_ptr<pid_t> tid_;
47+
std::shared_ptr<pid_t> tid_;
4848
ThreadFunc func_;
4949
string name_;
5050

‎muduo/base/ThreadPool.cc‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
#include <muduo/base/Exception.h>
99

10-
#include <boost/bind.hpp>
1110
#include <assert.h>
1211
#include <stdio.h>
1312

1413
using namespace muduo;
14+
using namespace std::placeholders;
1515

1616
ThreadPool::ThreadPool(const string& name)
1717
: mutex_(),
@@ -40,9 +40,9 @@ void ThreadPool::start(int numThreads)
4040
{
4141
char id[32];
4242
snprintf(id, sizeof id, "%d", i+1);
43-
threads_.push_back(new muduo::Thread(
44-
boost::bind(&ThreadPool::runInThread, this), name_+id));
45-
threads_[i].start();
43+
threads_.push_back(std::unique_ptr<muduo::Thread>(new muduo::Thread(
44+
std::bind(&ThreadPool::runInThread, this), name_+id)));
45+
threads_[i]->start();
4646
}
4747
}
4848

@@ -53,9 +53,10 @@ void ThreadPool::stop()
5353
running_ = false;
5454
notEmpty_.notifyAll();
5555
}
56-
for_each(threads_.begin(),
57-
threads_.end(),
58-
boost::bind(&muduo::Thread::join, _1));
56+
for (auto& thr : threads_)
57+
{
58+
thr->join();
59+
}
5960
}
6061

6162
void ThreadPool::run(const Task& task)

‎muduo/base/ThreadPool.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,18 @@
1111
#include <muduo/base/Thread.h>
1212
#include <muduo/base/Types.h>
1313

14-
#include <boost/function.hpp>
1514
#include <boost/noncopyable.hpp>
16-
#include <boost/ptr_container/ptr_vector.hpp>
1715

1816
#include <deque>
17+
#include <vector>
1918

2019
namespace muduo
2120
{
2221

2322
class ThreadPool : boost::noncopyable
2423
{
2524
public:
26-
typedef boost::function<void ()> Task;
25+
typedef std::function<void ()> Task;
2726

2827
explicit ThreadPool(const string& name = string("ThreadPool"));
2928
~ThreadPool();
@@ -49,7 +48,7 @@ class ThreadPool : boost::noncopyable
4948
Condition notEmpty_;
5049
Condition notFull_;
5150
string name_;
52-
boost::ptr_vector<muduo::Thread> threads_;
51+
std::vector<std::unique_ptr<muduo::Thread>> threads_;
5352
std::deque<Task> queue_;
5453
size_t maxQueueSize_;
5554
bool running_;

‎muduo/base/TimeZone.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#define MUDUO_BASE_TIMEZONE_H
88

99
#include <muduo/base/copyable.h>
10-
#include <boost/shared_ptr.hpp>
10+
#include <memory>
1111
#include <time.h>
1212

1313
namespace muduo
@@ -44,7 +44,7 @@ class TimeZone : public muduo::copyable
4444

4545
private:
4646

47-
boost::shared_ptr<Data> data_;
47+
std::shared_ptr<Data> data_;
4848
};
4949

5050
}

‎muduo/base/WeakCallback.h‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#define MUDUO_BASE_WEAKCALLBACK_H
1111

1212
#include <functional>
13-
#include <boost/function.hpp>
14-
#include <boost/shared_ptr.hpp>
13+
#include <memory>
1514

1615
namespace muduo
1716
{
@@ -20,14 +19,12 @@ namespace muduo
2019

2120
#ifdef __GXX_EXPERIMENTAL_CXX0X__
2221

23-
// FIXME: support std::shared_ptr as well, maybe using template template parameters
24-
2522
template<typename CLASS, typename... ARGS>
2623
class WeakCallback
2724
{
2825
public:
2926

30-
WeakCallback(const boost::weak_ptr<CLASS>& object,
27+
WeakCallback(const std::weak_ptr<CLASS>& object,
3128
const std::function<void (CLASS*, ARGS...)>& function)
3229
: object_(object), function_(function)
3330
{
@@ -37,7 +34,7 @@ class WeakCallback
3734

3835
void operator()(ARGS&&... args) const
3936
{
40-
boost::shared_ptr<CLASS> ptr(object_.lock());
37+
std::shared_ptr<CLASS> ptr(object_.lock());
4138
if (ptr)
4239
{
4340
function_(ptr.get(), std::forward<ARGS>(args)...);
@@ -50,19 +47,19 @@ class WeakCallback
5047

5148
private:
5249

53-
boost::weak_ptr<CLASS> object_;
50+
std::weak_ptr<CLASS> object_;
5451
std::function<void (CLASS*, ARGS...)> function_;
5552
};
5653

5754
template<typename CLASS, typename... ARGS>
58-
WeakCallback<CLASS, ARGS...> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
55+
WeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
5956
void (CLASS::*function)(ARGS...))
6057
{
6158
return WeakCallback<CLASS, ARGS...>(object, function);
6259
}
6360

6461
template<typename CLASS, typename... ARGS>
65-
WeakCallback<CLASS, ARGS...> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
62+
WeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
6663
void (CLASS::*function)(ARGS...) const)
6764
{
6865
return WeakCallback<CLASS, ARGS...>(object, function);
@@ -77,8 +74,8 @@ class WeakCallback
7774
{
7875
public:
7976

80-
WeakCallback(const boost::weak_ptr<CLASS>& object,
81-
const boost::function<void (CLASS*)>& function)
77+
WeakCallback(const std::weak_ptr<CLASS>& object,
78+
const std::function<void (CLASS*)>& function)
8279
: object_(object), function_(function)
8380
{
8481
}
@@ -87,7 +84,7 @@ class WeakCallback
8784

8885
void operator()() const
8986
{
90-
boost::shared_ptr<CLASS> ptr(object_.lock());
87+
std::shared_ptr<CLASS> ptr(object_.lock());
9188
if (ptr)
9289
{
9390
function_(ptr.get());
@@ -100,19 +97,19 @@ class WeakCallback
10097

10198
private:
10299

103-
boost::weak_ptr<CLASS> object_;
104-
boost::function<void (CLASS*)> function_;
100+
std::weak_ptr<CLASS> object_;
101+
std::function<void (CLASS*)> function_;
105102
};
106103

107104
template<typename CLASS>
108-
WeakCallback<CLASS> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
105+
WeakCallback<CLASS> makeWeakCallback(const std::shared_ptr<CLASS>& object,
109106
void (CLASS::*function)())
110107
{
111108
return WeakCallback<CLASS>(object, function);
112109
}
113110

114111
template<typename CLASS>
115-
WeakCallback<CLASS> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
112+
WeakCallback<CLASS> makeWeakCallback(const std::shared_ptr<CLASS>& object,
116113
void (CLASS::*function)() const)
117114
{
118115
return WeakCallback<CLASS>(object, function);

0 commit comments

Comments
 (0)