Skip to content

Commit

Permalink
noncopyable added
Browse files Browse the repository at this point in the history
  • Loading branch information
yedf committed Apr 26, 2016
1 parent 59f9e11 commit f7a94aa
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions handy/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace handy {

//Tcp连接,使用引用计数
struct TcpConn: public std::enable_shared_from_this<TcpConn> {
struct TcpConn: public std::enable_shared_from_this<TcpConn>, private noncopyable {
//Tcp连接的个状态
enum State { Invalid=1, Handshaking, Connected, Closed, Failed, };
//Tcp构造函数,实际可用的连接应当通过createConnection创建
Expand Down Expand Up @@ -92,7 +92,7 @@ namespace handy {
};

//Tcp服务器
struct TcpServer {
struct TcpServer: private noncopyable {
TcpServer(EventBases* bases);
//return 0 on sucess, errno on error
int bind(const std::string& host, short port, bool reusePort=false);
Expand Down
4 changes: 2 additions & 2 deletions handy/event_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef std::shared_ptr<TcpServer> TcpServerPtr;
typedef std::function<void(const TcpConnPtr&)> TcpCallBack;
typedef std::function<void(const TcpConnPtr&, Slice msg)> MsgCallBack;

struct EventBases {
struct EventBases: private noncopyable {
virtual EventBase* allocBase() = 0;
};

Expand Down Expand Up @@ -60,7 +60,7 @@ struct MultiBase: public EventBases{
};

//通道,封装了可以进行epoll的一个fd
struct Channel {
struct Channel: private noncopyable {
//base为事件管理器,fd为通道内部的fd,events为通道关心的事件
Channel(EventBase* base, int fd, int events);
~Channel();
Expand Down
2 changes: 1 addition & 1 deletion handy/handy-imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct EventBase;
typedef std::unique_ptr<IdleIdImp> IdleId;
typedef std::pair<int64_t, int64_t> TimerId;

struct AutoContext {
struct AutoContext: noncopyable {
void* ctx;
Task ctxDel;
AutoContext():ctx(0) {}
Expand Down
3 changes: 2 additions & 1 deletion handy/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <stdio.h>
#include <atomic>
#include "util.h"

#ifdef NDEBUG
#define hlog(level, ...) \
Expand Down Expand Up @@ -36,7 +37,7 @@

namespace handy {

struct Logger {
struct Logger: private noncopyable {
enum LogLevel{LFATAL=0, LERROR, LUERR, LWARN, LINFO, LDEBUG, LTRACE, LALL};
Logger();
~Logger();
Expand Down
2 changes: 1 addition & 1 deletion handy/poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace handy {

const int kMaxEvents = 2000;

struct PollerBase {
struct PollerBase: private noncopyable {
int64_t id_;
int lastActive_;
PollerBase(): lastActive_(-1) { static std::atomic<int64_t> id(0); id_ = ++id; }
Expand Down
2 changes: 1 addition & 1 deletion handy/stat-svr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef std::function<void(const HttpRequest&, HttpResponse&)> StatCallBack;
typedef std::function<std::string()> InfoCallBack;
typedef std::function<int64_t()> IntCallBack;

struct StatServer {
struct StatServer: private noncopyable {
enum StatType { STATE, PAGE, CMD, };
StatServer(EventBase* base);
int bind(const std::string& host, short port) { return server_.bind(host, port); }
Expand Down
5 changes: 3 additions & 2 deletions handy/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#include <limits>
#include <condition_variable>
#include <mutex>
#include "util.h"

namespace handy {

template<typename T> struct SafeQueue: private std::mutex {
template<typename T> struct SafeQueue: private std::mutex, private noncopyable {
static const int wait_infinite = std::numeric_limits<int>::max();
//0 不限制队列中的任务数
SafeQueue(size_t capacity=0): capacity_(capacity), exit_(false) {}
Expand All @@ -35,7 +36,7 @@ template<typename T> struct SafeQueue: private std::mutex {
typedef std::function<void()> Task;
extern template class SafeQueue<Task>;

struct ThreadPool {
struct ThreadPool: private noncopyable {
//创建线程池
ThreadPool(int threads, int taskCapacity=0, bool start=true);
~ThreadPool();
Expand Down
8 changes: 7 additions & 1 deletion handy/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

namespace handy {

struct noncopyable {
noncopyable() {};
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
};

struct util {
static std::string format(const char* fmt, ...);
static int64_t timeMicro();
Expand All @@ -25,7 +31,7 @@ struct util {
static int addFdFlag(int fd, int flag);
};

struct ExitCaller {
struct ExitCaller: private noncopyable {
~ExitCaller() { functor_(); }
ExitCaller(std::function<void()>&& functor): functor_(std::move(functor)) {}
private:
Expand Down

0 comments on commit f7a94aa

Please sign in to comment.