Skip to content

Commit

Permalink
replace boost::ptr_vector<T> with std::vector<std::unique_ptr<T>> in …
Browse files Browse the repository at this point in the history
…examples.

replace boost::array with std::array in examples.
  • Loading branch information
chenshuo committed Aug 24, 2017
1 parent 7159ef5 commit 224b328
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 106 deletions.
12 changes: 5 additions & 7 deletions examples/asio/chat/loadtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <muduo/net/EventLoopThreadPool.h>
#include <muduo/net/TcpClient.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <stdio.h>

using namespace muduo;
Expand Down Expand Up @@ -105,13 +103,13 @@ class ChatClient : noncopyable
Timestamp receiveTime_;
};

void statistic(const boost::ptr_vector<ChatClient>& clients)
void statistic(const std::vector<std::unique_ptr<ChatClient>>& clients)
{
LOG_INFO << "statistic " << clients.size();
std::vector<double> seconds(clients.size());
for (size_t i = 0; i < clients.size(); ++i)
{
seconds[i] = timeDifference(clients[i].receiveTime(), g_startTime);
seconds[i] = timeDifference(clients[i]->receiveTime(), g_startTime);
}

std::sort(seconds.begin(), seconds.end());
Expand Down Expand Up @@ -147,13 +145,13 @@ int main(int argc, char* argv[])
loopPool.start();

g_receiveTime.reserve(g_connections);
boost::ptr_vector<ChatClient> clients(g_connections);
std::vector<std::unique_ptr<ChatClient>> clients(g_connections);
g_statistic = std::bind(statistic, std::ref(clients));

for (int i = 0; i < g_connections; ++i)
{
clients.push_back(new ChatClient(loopPool.getNextLoop(), serverAddr));
clients[i].connect();
clients[i].reset(new ChatClient(loopPool.getNextLoop(), serverAddr));
clients[i]->connect();
usleep(200);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/cdns/Resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void Resolver::onSockCreate(int sockfd, int type)
Channel* channel = new Channel(loop_, sockfd);
channel->setReadCallback(std::bind(&Resolver::onRead, this, sockfd, _1));
channel->enableReading();
channels_.insert(sockfd, channel);
channels_[sockfd].reset(channel);
}

void Resolver::onSockStateChange(int sockfd, bool read, bool write)
Expand Down
6 changes: 4 additions & 2 deletions examples/cdns/Resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <muduo/base/Timestamp.h>
#include <muduo/net/InetAddress.h>

#include <boost/ptr_container/ptr_map.hpp>
#include <functional>
#include <map>
#include <memory>

extern "C"
{
Expand Down Expand Up @@ -56,7 +58,7 @@ class Resolver : muduo::noncopyable
muduo::net::EventLoop* loop_;
ares_channel ctx_;
bool timerActive_;
typedef boost::ptr_map<int, muduo::net::Channel> ChannelList;
typedef std::map<int, std::unique_ptr<muduo::net::Channel>> ChannelList;
ChannelList channels_;

void onRead(int sockfd, muduo::Timestamp t);
Expand Down
11 changes: 5 additions & 6 deletions examples/curl/download.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <examples/curl/Curl.h>
#include <muduo/base/Logging.h>
#include <muduo/net/EventLoop.h>
#include <boost/ptr_container/ptr_vector.hpp>
#include <stdio.h>
#include <sstream>

Expand Down Expand Up @@ -157,10 +156,10 @@ class Downloader : noncopyable
{
range << i * pieceLen << "-" << length_ - 1;
}
pieces_.push_back(new Piece(req,
out,
range.str().c_str(), // std::string -> muduo::string
std::bind(&Downloader::onDownloadDone, this)));
pieces_[i].reset(new Piece(req,
out,
range.str().c_str(), // std::string -> muduo::string
std::bind(&Downloader::onDownloadDone, this)));
}
else
{
Expand Down Expand Up @@ -192,7 +191,7 @@ class Downloader : noncopyable
bool acceptRanges_;
int64_t length_;
FilePtr out_;
boost::ptr_vector<Piece> pieces_;
std::vector<std::unique_ptr<Piece>> pieces_;
int concurrent_;

const static int kConcurrent = 4;
Expand Down
7 changes: 3 additions & 4 deletions examples/memcached/client/bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <muduo/net/TcpClient.h>

#include <boost/program_options.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>

#include <stdio.h>
Expand Down Expand Up @@ -210,11 +209,11 @@ int main(int argc, char* argv[])
char buf[32];
CountDownLatch connected(clients);
CountDownLatch finished(clients);
boost::ptr_vector<Client> holder;
std::vector<std::unique_ptr<Client>> holder;
for (int i = 0; i < clients; ++i)
{
snprintf(buf, sizeof buf, "%d-", i+1);
holder.push_back(new Client(buf,
holder.emplace_back(new Client(buf,
pool.getNextLoop(),
serverAddr,
op,
Expand All @@ -229,7 +228,7 @@ int main(int argc, char* argv[])
Timestamp start = Timestamp::now();
for (int i = 0; i < clients; ++i)
{
holder[i].send();
holder[i]->send();
}
finished.wait();
Timestamp end = Timestamp::now();
Expand Down
5 changes: 2 additions & 3 deletions examples/memcached/server/MemcacheServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
#include <muduo/net/TcpServer.h>
#include <examples/wordcount/hash.h>

#include <array>
#include <unordered_map>
#include <unordered_set>

#include <boost/array.hpp>

class MemcacheServer : muduo::noncopyable
{
public:
Expand Down Expand Up @@ -77,7 +76,7 @@ class MemcacheServer : muduo::noncopyable

const static int kShards = 4096;

boost::array<MapWithLock, kShards> shards_;
std::array<MapWithLock, kShards> shards_;

// NOT guarded by mutex_, but here because server_ has to destructs before
// sessions_
Expand Down
15 changes: 6 additions & 9 deletions examples/pingpong/bench.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <muduo/net/Channel.h>
#include <muduo/net/EventLoop.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <stdio.h>
#include <sys/resource.h>
#include <sys/socket.h>
Expand All @@ -20,7 +18,7 @@ int numPipes;
int numActive;
int numWrites;
EventLoop* g_loop;
boost::ptr_vector<Channel> g_channels;
std::vector<std::unique_ptr<Channel>> g_channels;

int g_reads, g_writes, g_fired;

Expand Down Expand Up @@ -51,7 +49,7 @@ std::pair<int, int> runOnce()
Timestamp beforeInit(Timestamp::now());
for (int i = 0; i < numPipes; ++i)
{
Channel& channel = g_channels[i];
Channel& channel = *g_channels[i];
channel.setReadCallback(std::bind(readCallback, _1, channel.fd(), i));
channel.enableReading();
}
Expand Down Expand Up @@ -124,7 +122,7 @@ int main(int argc, char* argv[])
for (int i = 0; i < numPipes; ++i)
{
Channel* channel = new Channel(&loop, g_pipes[i*2]);
g_channels.push_back(channel);
g_channels.emplace_back(channel);
}

for (int i = 0; i < 25; ++i)
Expand All @@ -133,11 +131,10 @@ int main(int argc, char* argv[])
printf("%8d %8d\n", t.first, t.second);
}

for (boost::ptr_vector<Channel>::iterator it = g_channels.begin();
it != g_channels.end(); ++it)
for (const auto& channel : g_channels)
{
it->disableAll();
it->remove();
channel->disableAll();
channel->remove();
}
g_channels.clear();
}
Expand Down
15 changes: 6 additions & 9 deletions examples/pingpong/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <muduo/net/EventLoopThreadPool.h>
#include <muduo/net/InetAddress.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <utility>

#include <stdio.h>
Expand Down Expand Up @@ -108,7 +106,7 @@ class Client : noncopyable
snprintf(buf, sizeof buf, "C%05d", i);
Session* session = new Session(threadPool_.getNextLoop(), serverAddr, buf, this);
session->start();
sessions_.push_back(session);
sessions_.emplace_back(session);
}
}

Expand All @@ -133,11 +131,10 @@ class Client : noncopyable

int64_t totalBytesRead = 0;
int64_t totalMessagesRead = 0;
for (boost::ptr_vector<Session>::iterator it = sessions_.begin();
it != sessions_.end(); ++it)
for (const auto& session : sessions_)
{
totalBytesRead += it->bytesRead();
totalMessagesRead += it->messagesRead();
totalBytesRead += session->bytesRead();
totalMessagesRead += session->messagesRead();
}
LOG_WARN << totalBytesRead << " total bytes read";
LOG_WARN << totalMessagesRead << " total messages read";
Expand All @@ -161,15 +158,15 @@ class Client : noncopyable
LOG_WARN << "stop";
for (auto& session : sessions_)
{
session.stop();
session->stop();
}
}

EventLoop* loop_;
EventLoopThreadPool threadPool_;
int sessionCount_;
int timeout_;
boost::ptr_vector<Session> sessions_;
std::vector<std::unique_ptr<Session>> sessions_;
string message_;
AtomicInt32 numConnected_;
};
Expand Down
10 changes: 4 additions & 6 deletions examples/procmon/dummyload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <muduo/base/Timestamp.h>
#include <muduo/net/EventLoop.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <math.h>
#include <stdio.h>

Expand Down Expand Up @@ -137,11 +135,11 @@ int main(int argc, char* argv[])

g_percent = argc > 2 ? atoi(argv[2]) : 43;
int numThreads = argc > 3 ? atoi(argv[3]) : 1;
boost::ptr_vector<Thread> threads;
std::vector<std::unique_ptr<Thread>> threads;
for (int i = 0; i < numThreads; ++i)
{
threads.push_back(new Thread(threadFunc));
threads.back().start();
threads.emplace_back(new Thread(threadFunc));
threads.back()->start();
}

switch (argv[1][0])
Expand Down Expand Up @@ -178,6 +176,6 @@ int main(int argc, char* argv[])
}
for (int i = 0; i < numThreads; ++i)
{
threads[i].join();
threads[i]->join();
}
}
10 changes: 4 additions & 6 deletions examples/protobuf/rpcbalancer/balancer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <muduo/net/protorpc/RpcCodec.h>
#include <muduo/net/protorpc/rpc.pb.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <stdio.h>

using namespace muduo;
Expand Down Expand Up @@ -152,7 +150,7 @@ class Balancer : noncopyable
struct PerThread
{
size_t current;
boost::ptr_vector<BackendSession> backends;
std::vector<std::unique_ptr<BackendSession>> backends;
PerThread() : current(0) { }
};

Expand All @@ -167,8 +165,8 @@ class Balancer : noncopyable
{
char buf[32];
snprintf(buf, sizeof buf, "%s#%d", backends_[i].toIpPort().c_str(), count);
t.backends.push_back(new BackendSession(ioLoop, backends_[i], buf));
t.backends.back().connect();
t.backends.emplace_back(new BackendSession(ioLoop, backends_[i], buf));
t.backends.back()->connect();
}
}

Expand All @@ -192,7 +190,7 @@ class Balancer : noncopyable
bool succeed = false;
for (size_t i = 0; i < t.backends.size() && !succeed; ++i)
{
succeed = t.backends[t.current].send(*msg, conn);
succeed = t.backends[t.current]->send(*msg, conn);
t.current = (t.current+1) % t.backends.size();
}
if (!succeed)
Expand Down
10 changes: 4 additions & 6 deletions examples/protobuf/rpcbalancer/balancer_raw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <muduo/net/protorpc/RpcCodec.h>
#include <muduo/net/protorpc/rpc.pb.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <endian.h>
#include <stdio.h>

Expand Down Expand Up @@ -241,7 +239,7 @@ class Balancer : noncopyable
struct PerThread
{
size_t current;
boost::ptr_vector<BackendSession> backends;
std::vector<std::unique_ptr<BackendSession>> backends;
PerThread() : current(0) { }
};

Expand All @@ -256,8 +254,8 @@ class Balancer : noncopyable
{
char buf[32];
snprintf(buf, sizeof buf, "%s#%d", backends_[i].toIpPort().c_str(), count);
t.backends.push_back(new BackendSession(ioLoop, backends_[i], buf));
t.backends.back().connect();
t.backends.emplace_back(new BackendSession(ioLoop, backends_[i], buf));
t.backends.back()->connect();
}
}

Expand Down Expand Up @@ -301,7 +299,7 @@ class Balancer : noncopyable
bool succeed = false;
for (size_t i = 0; i < t.backends.size() && !succeed; ++i)
{
succeed = t.backends[t.current].send(msg, conn);
succeed = t.backends[t.current]->send(msg, conn);
t.current = (t.current+1) % t.backends.size();
}
if (!succeed)
Expand Down
10 changes: 4 additions & 6 deletions examples/protobuf/rpcbench/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <muduo/net/TcpConnection.h>
#include <muduo/net/protorpc/RpcChannel.h>

#include <boost/ptr_container/ptr_vector.hpp>

#include <stdio.h>

using namespace muduo;
Expand Down Expand Up @@ -119,18 +117,18 @@ int main(int argc, char* argv[])
pool.start();
InetAddress serverAddr(argv[1], 8888);

boost::ptr_vector<RpcClient> clients;
std::vector<std::unique_ptr<RpcClient>> clients;
for (int i = 0; i < nClients; ++i)
{
clients.push_back(new RpcClient(pool.getNextLoop(), serverAddr, &allConnected, &allFinished));
clients.back().connect();
clients.emplace_back(new RpcClient(pool.getNextLoop(), serverAddr, &allConnected, &allFinished));
clients.back()->connect();
}
allConnected.wait();
Timestamp start(Timestamp::now());
LOG_INFO << "all connected";
for (int i = 0; i < nClients; ++i)
{
clients[i].sendRequest();
clients[i]->sendRequest();
}
allFinished.wait();
Timestamp end(Timestamp::now());
Expand Down
Loading

0 comments on commit 224b328

Please sign in to comment.