Skip to content

Commit

Permalink
base to cpp11
Browse files Browse the repository at this point in the history
replace boost::shared_ptr/boost::weak_ptr with std::shared_ptr/std::weak_ptr.
replace boost::function/boost::bind with std::function/std::bind.
replace boost::ptr_vector<T> with std::vector<std::unique_ptr<T>>.
  • Loading branch information
chenshuo committed Nov 6, 2016
1 parent e6c04c4 commit 59044ce
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 102 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set(CXX_FLAGS
-Wwrite-strings
-march=native
# -MMD
# -std=c++0x
-std=c++0x
-rdynamic
)
if(CMAKE_BUILD_BITS EQUAL 32)
Expand Down
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ BUILD_TYPE=${BUILD_TYPE:-release}
INSTALL_DIR=${INSTALL_DIR:-../${BUILD_TYPE}-install}
BUILD_NO_EXAMPLES=${BUILD_NO_EXAMPLES:-0}

mkdir -p $BUILD_DIR/$BUILD_TYPE \
&& cd $BUILD_DIR/$BUILD_TYPE \
mkdir -p $BUILD_DIR/$BUILD_TYPE-cpp11 \
&& cd $BUILD_DIR/$BUILD_TYPE-cpp11 \
&& cmake \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
Expand Down
20 changes: 11 additions & 9 deletions muduo/base/AsyncLogging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AsyncLogging::AsyncLogging(const string& basename,
running_(false),
basename_(basename),
rollSize_(rollSize),
thread_(boost::bind(&AsyncLogging::threadFunc, this), "Logging"),
thread_(std::bind(&AsyncLogging::threadFunc, this), "Logging"),
latch_(1),
mutex_(),
cond_(mutex_),
Expand All @@ -35,11 +35,11 @@ void AsyncLogging::append(const char* logline, int len)
}
else
{
buffers_.push_back(currentBuffer_.release());
buffers_.push_back(std::move(currentBuffer_));

if (nextBuffer_)
{
currentBuffer_ = boost::ptr_container::move(nextBuffer_);
currentBuffer_ = std::move(nextBuffer_);
}
else
{
Expand Down Expand Up @@ -73,12 +73,12 @@ void AsyncLogging::threadFunc()
{
cond_.waitForSeconds(flushInterval_);
}
buffers_.push_back(currentBuffer_.release());
currentBuffer_ = boost::ptr_container::move(newBuffer1);
buffers_.push_back(std::move(currentBuffer_));
currentBuffer_ = std::move(newBuffer1);
buffersToWrite.swap(buffers_);
if (!nextBuffer_)
{
nextBuffer_ = boost::ptr_container::move(newBuffer2);
nextBuffer_ = std::move(newBuffer2);
}
}

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

if (buffersToWrite.size() > 2)
Expand All @@ -110,14 +110,16 @@ void AsyncLogging::threadFunc()
if (!newBuffer1)
{
assert(!buffersToWrite.empty());
newBuffer1 = buffersToWrite.pop_back();
newBuffer1 = std::move(buffersToWrite.back());
buffersToWrite.back();
newBuffer1->reset();
}

if (!newBuffer2)
{
assert(!buffersToWrite.empty());
newBuffer2 = buffersToWrite.pop_back();
newBuffer2 = std::move(buffersToWrite.back());
buffersToWrite.back();
newBuffer2->reset();
}

Expand Down
10 changes: 2 additions & 8 deletions muduo/base/AsyncLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

#include <muduo/base/LogStream.h>

#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/ptr_container/ptr_vector.hpp>

namespace muduo
{
Expand Down Expand Up @@ -51,15 +49,11 @@ class AsyncLogging : boost::noncopyable

private:

// declare but not define, prevent compiler-synthesized functions
AsyncLogging(const AsyncLogging&); // ptr_container
void operator=(const AsyncLogging&); // ptr_container

void threadFunc();

typedef muduo::detail::FixedBuffer<muduo::detail::kLargeBuffer> Buffer;
typedef boost::ptr_vector<Buffer> BufferVector;
typedef BufferVector::auto_type BufferPtr;
typedef std::vector<std::unique_ptr<Buffer>> BufferVector;
typedef BufferVector::value_type BufferPtr;

const int flushInterval_;
bool running_;
Expand Down
2 changes: 1 addition & 1 deletion muduo/base/Singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace detail
template<typename T>
struct has_no_destroy
{
template <typename C> static char test(typeof(&C::no_destroy)); // or decltype in C++11
template <typename C> static char test(decltype(&C::no_destroy));
template <typename C> static int32_t test(...);
const static bool value = sizeof(test<T>(0)) == 1;
};
Expand Down
7 changes: 3 additions & 4 deletions muduo/base/Thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/weak_ptr.hpp>

#include <errno.h>
#include <stdio.h>
Expand Down Expand Up @@ -66,11 +65,11 @@ struct ThreadData
typedef muduo::Thread::ThreadFunc ThreadFunc;
ThreadFunc func_;
string name_;
boost::weak_ptr<pid_t> wkTid_;
std::weak_ptr<pid_t> wkTid_;

ThreadData(const ThreadFunc& func,
const string& name,
const boost::shared_ptr<pid_t>& tid)
const std::shared_ptr<pid_t>& tid)
: func_(func),
name_(name),
wkTid_(tid)
Expand All @@ -80,7 +79,7 @@ struct ThreadData
{
pid_t tid = muduo::CurrentThread::tid();

boost::shared_ptr<pid_t> ptid = wkTid_.lock();
std::shared_ptr<pid_t> ptid = wkTid_.lock();
if (ptid)
{
*ptid = tid;
Expand Down
9 changes: 5 additions & 4 deletions muduo/base/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
#include <muduo/base/Atomic.h>
#include <muduo/base/Types.h>

#include <boost/function.hpp>
#include <functional>
#include <memory>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <pthread.h>

namespace muduo
Expand All @@ -20,12 +20,13 @@ namespace muduo
class Thread : boost::noncopyable
{
public:
typedef boost::function<void ()> ThreadFunc;
typedef std::function<void ()> ThreadFunc;

explicit Thread(const ThreadFunc&, const string& name = string());
#ifdef __GXX_EXPERIMENTAL_CXX0X__
explicit Thread(ThreadFunc&&, const string& name = string());
#endif
// FIXME: make it movable in C++11
~Thread();

void start();
Expand All @@ -44,7 +45,7 @@ class Thread : boost::noncopyable
bool started_;
bool joined_;
pthread_t pthreadId_;
boost::shared_ptr<pid_t> tid_;
std::shared_ptr<pid_t> tid_;
ThreadFunc func_;
string name_;

Expand Down
15 changes: 8 additions & 7 deletions muduo/base/ThreadPool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include <muduo/base/Exception.h>

#include <boost/bind.hpp>
#include <assert.h>
#include <stdio.h>

using namespace muduo;
using namespace std::placeholders;

ThreadPool::ThreadPool(const string& nameArg)
: mutex_(),
Expand Down Expand Up @@ -40,9 +40,9 @@ void ThreadPool::start(int numThreads)
{
char id[32];
snprintf(id, sizeof id, "%d", i+1);
threads_.push_back(new muduo::Thread(
boost::bind(&ThreadPool::runInThread, this), name_+id));
threads_[i].start();
threads_.emplace_back(new muduo::Thread(
std::bind(&ThreadPool::runInThread, this), name_+id));
threads_[i]->start();
}
if (numThreads == 0 && threadInitCallback_)
{
Expand All @@ -57,9 +57,10 @@ void ThreadPool::stop()
running_ = false;
notEmpty_.notifyAll();
}
for_each(threads_.begin(),
threads_.end(),
boost::bind(&muduo::Thread::join, _1));
for (auto& thr : threads_)
{
thr->join();
}
}

size_t ThreadPool::queueSize() const
Expand Down
7 changes: 3 additions & 4 deletions muduo/base/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@
#include <muduo/base/Thread.h>
#include <muduo/base/Types.h>

#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/ptr_container/ptr_vector.hpp>

#include <deque>
#include <vector>

namespace muduo
{

class ThreadPool : boost::noncopyable
{
public:
typedef boost::function<void ()> Task;
typedef std::function<void ()> Task;

explicit ThreadPool(const string& nameArg = string("ThreadPool"));
~ThreadPool();
Expand Down Expand Up @@ -57,7 +56,7 @@ class ThreadPool : boost::noncopyable
Condition notFull_;
string name_;
Task threadInitCallback_;
boost::ptr_vector<muduo::Thread> threads_;
std::vector<std::unique_ptr<muduo::Thread>> threads_;
std::deque<Task> queue_;
size_t maxQueueSize_;
bool running_;
Expand Down
1 change: 1 addition & 0 deletions muduo/base/TimeZone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <vector>

#include <assert.h>
//#define _BSD_SOURCE
#include <endian.h>

Expand Down
4 changes: 2 additions & 2 deletions muduo/base/TimeZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define MUDUO_BASE_TIMEZONE_H

#include <muduo/base/copyable.h>
#include <boost/shared_ptr.hpp>
#include <memory>
#include <time.h>

namespace muduo
Expand Down Expand Up @@ -44,7 +44,7 @@ class TimeZone : public muduo::copyable

private:

boost::shared_ptr<Data> data_;
std::shared_ptr<Data> data_;
};

}
Expand Down
29 changes: 13 additions & 16 deletions muduo/base/WeakCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
#define MUDUO_BASE_WEAKCALLBACK_H

#include <functional>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>

namespace muduo
{
Expand All @@ -20,14 +19,12 @@ namespace muduo

#ifdef __GXX_EXPERIMENTAL_CXX0X__

// FIXME: support std::shared_ptr as well, maybe using template template parameters

template<typename CLASS, typename... ARGS>
class WeakCallback
{
public:

WeakCallback(const boost::weak_ptr<CLASS>& object,
WeakCallback(const std::weak_ptr<CLASS>& object,
const std::function<void (CLASS*, ARGS...)>& function)
: object_(object), function_(function)
{
Expand All @@ -37,7 +34,7 @@ class WeakCallback

void operator()(ARGS&&... args) const
{
boost::shared_ptr<CLASS> ptr(object_.lock());
std::shared_ptr<CLASS> ptr(object_.lock());
if (ptr)
{
function_(ptr.get(), std::forward<ARGS>(args)...);
Expand All @@ -50,19 +47,19 @@ class WeakCallback

private:

boost::weak_ptr<CLASS> object_;
std::weak_ptr<CLASS> object_;
std::function<void (CLASS*, ARGS...)> function_;
};

template<typename CLASS, typename... ARGS>
WeakCallback<CLASS, ARGS...> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
WeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
void (CLASS::*function)(ARGS...))
{
return WeakCallback<CLASS, ARGS...>(object, function);
}

template<typename CLASS, typename... ARGS>
WeakCallback<CLASS, ARGS...> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
WeakCallback<CLASS, ARGS...> makeWeakCallback(const std::shared_ptr<CLASS>& object,
void (CLASS::*function)(ARGS...) const)
{
return WeakCallback<CLASS, ARGS...>(object, function);
Expand All @@ -77,8 +74,8 @@ class WeakCallback
{
public:

WeakCallback(const boost::weak_ptr<CLASS>& object,
const boost::function<void (CLASS*)>& function)
WeakCallback(const std::weak_ptr<CLASS>& object,
const std::function<void (CLASS*)>& function)
: object_(object), function_(function)
{
}
Expand All @@ -87,7 +84,7 @@ class WeakCallback

void operator()() const
{
boost::shared_ptr<CLASS> ptr(object_.lock());
std::shared_ptr<CLASS> ptr(object_.lock());
if (ptr)
{
function_(ptr.get());
Expand All @@ -100,19 +97,19 @@ class WeakCallback

private:

boost::weak_ptr<CLASS> object_;
boost::function<void (CLASS*)> function_;
std::weak_ptr<CLASS> object_;
std::function<void (CLASS*)> function_;
};

template<typename CLASS>
WeakCallback<CLASS> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
WeakCallback<CLASS> makeWeakCallback(const std::shared_ptr<CLASS>& object,
void (CLASS::*function)())
{
return WeakCallback<CLASS>(object, function);
}

template<typename CLASS>
WeakCallback<CLASS> makeWeakCallback(const boost::shared_ptr<CLASS>& object,
WeakCallback<CLASS> makeWeakCallback(const std::shared_ptr<CLASS>& object,
void (CLASS::*function)() const)
{
return WeakCallback<CLASS>(object, function);
Expand Down
Loading

0 comments on commit 59044ce

Please sign in to comment.