aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/salticidae/conn.h6
-rw-r--r--include/salticidae/network.h2
-rw-r--r--include/salticidae/ref.h60
-rw-r--r--include/salticidae/util.h40
4 files changed, 61 insertions, 47 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index 589bf16..c8ccf31 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -212,9 +212,9 @@ class ConnPool {
protected:
/** close the connection and free all on-going or planned events. */
virtual void close() {
- ev_read.del();
- ev_write.del();
- ev_connect.del();
+ ev_read.clear();
+ ev_write.clear();
+ ev_connect.clear();
::close(fd);
fd = -1;
}
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index 1b20ce1..cede475 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -315,7 +315,7 @@ void PeerNetwork<MsgType>::Conn::on_teardown() {
auto it = pn->id2peer.find(peer_id);
if (it == pn->id2peer.end()) return;
Peer *p = it->second;
- if (this != p->conn) return;
+ if (this != p->conn.get()) return;
p->ev_ping_timer.del();
p->connected = false;
p->conn = nullptr;
diff --git a/include/salticidae/ref.h b/include/salticidae/ref.h
index a1c5dc7..c0b77a3 100644
--- a/include/salticidae/ref.h
+++ b/include/salticidae/ref.h
@@ -89,7 +89,17 @@ class _BoxObj {
return *this;
}
- operator T*() const { return obj; }
+ T &operator *() const { return *obj; }
+ T *get() const { return obj; }
+ operator bool() const { return obj != nullptr; }
+ bool operator==(const _BoxObj &other) const { return obj == other.obj; }
+ bool operator!=(const _BoxObj &other) const { return obj != other.obj; }
+ bool operator==(std::nullptr_t) const { return obj == nullptr; }
+ bool operator!=(std::nullptr_t) const { return obj != nullptr; }
+ bool operator<(const _BoxObj &other) const { return obj < other.obj; }
+ bool operator>(const _BoxObj &other) const { return obj > other.obj; }
+ bool operator<=(const _BoxObj &other) const { return obj <= other.obj; }
+ bool operator>=(const _BoxObj &other) const { return obj >= other.obj; }
};
template<typename T, typename D = default_delete<T>>
@@ -102,7 +112,7 @@ class BoxObj: public _BoxObj<T, D> {
BoxObj() = default;
BoxObj(T *obj): base_t(obj) {}
template<typename T_, typename =
- typename std::enable_if<!std::is_array<T_>::value>::value>
+ typename std::enable_if<!std::is_array<T_>::value>::type>
BoxObj(BoxObj<T_> &&other): base_t(other) {}
T *operator->() const { return base_t::obj; }
@@ -175,6 +185,7 @@ struct _ARCCtl {
~_ARCCtl() {}
};
+template<typename T, typename R, typename D> class _RcObjBase;
template<typename T, typename R, typename D> class RcObjBase;
template<typename T, typename R>
@@ -182,9 +193,8 @@ class _WeakObjBase {
T *obj;
R *ctl;
- template<typename D> using ROB = RcObjBase<T, R, D>;
- friend class ROB;
- friend std::hash<_WeakObjBase<T, R>>;
+ template<typename T_, typename R_, typename D>
+ friend class _RcObjBase;
public:
_WeakObjBase(): ctl(nullptr) {}
@@ -223,7 +233,7 @@ class _WeakObjBase {
}
template<typename D>
- _WeakObjBase(const RcObjBase<T, R, D> &other);
+ _WeakObjBase(const _RcObjBase<T, R, D> &other);
~_WeakObjBase() { if (ctl) ctl->release_weak(); }
};
@@ -231,22 +241,30 @@ class _WeakObjBase {
template<typename T, typename R>
class WeakObjBase: public _WeakObjBase<T, R> {
+ using base_t = _WeakObjBase<T, R>;
+ friend std::hash<WeakObjBase>;
public:
WeakObjBase() = default;
WeakObjBase(const WeakObjBase &other) = default;
WeakObjBase(WeakObjBase &&other) = default;
template<typename D>
- WeakObjBase(const RcObjBase<T, R, D> &other);
+ WeakObjBase(const RcObjBase<T, R, D> &other): base_t(other) {}
+ WeakObjBase &operator=(const WeakObjBase &) = default;
+ WeakObjBase &operator=(WeakObjBase &&) = default;
};
template<typename T, typename R>
class WeakObjBase<T[], R>: public _WeakObjBase<T, R> {
+ using base_t = _WeakObjBase<T, R>;
+ friend std::hash<WeakObjBase>;
public:
WeakObjBase() = default;
WeakObjBase(const WeakObjBase &other) = default;
WeakObjBase(WeakObjBase &&other) = default;
template<typename D>
- WeakObjBase(const RcObjBase<T[], R, D> &other);
+ WeakObjBase(const RcObjBase<T[], R, D> &other): base_t(other) {}
+ WeakObjBase &operator=(const WeakObjBase &) = default;
+ WeakObjBase &operator=(WeakObjBase &&) = default;
};
@@ -257,13 +275,11 @@ class _RcObjBase {
ptr_type obj;
R *ctl;
- friend WeakObjBase<T, R>;
- friend std::hash<_RcObjBase<T, R, D>>;
+ friend _WeakObjBase<T, R>;
template<typename T_, typename R_, typename D_>
friend class _RcObjBase;
public:
- operator T*() const { return obj; }
_RcObjBase(): obj(nullptr), ctl(nullptr) {}
_RcObjBase(ptr_type obj): obj(obj), ctl(new R()) {}
_RcObjBase(BoxObj<T> &&box_ref): obj(box_ref.obj), ctl(new R()) {
@@ -315,7 +331,7 @@ class _RcObjBase {
other.ctl = nullptr;
}
- _RcObjBase(const WeakObjBase<T, R> &other) {
+ _RcObjBase(const _WeakObjBase<T, R> &other) {
if (other.ctl && other.ctl->ref_cnt)
{
obj = other.obj;
@@ -335,11 +351,23 @@ class _RcObjBase {
}
size_t get_cnt() const { return ctl ? ctl->get_cnt() : 0; }
+ T &operator *() const { return *obj; }
+ T *get() const { return obj; }
+ operator bool() const { return obj != nullptr; }
+ bool operator==(const _RcObjBase &other) const { return obj == other.obj; }
+ bool operator!=(const _RcObjBase &other) const { return obj != other.obj; }
+ bool operator==(std::nullptr_t) const { return obj == nullptr; }
+ bool operator!=(std::nullptr_t) const { return obj != nullptr; }
+ bool operator<(const _RcObjBase &other) const { return obj < other.obj; }
+ bool operator>(const _RcObjBase &other) const { return obj > other.obj; }
+ bool operator<=(const _RcObjBase &other) const { return obj <= other.obj; }
+ bool operator>=(const _RcObjBase &other) const { return obj >= other.obj; }
};
template<typename T, typename R, typename D = default_delete<T>>
class RcObjBase: public _RcObjBase<T, R, D> {
using base_t = _RcObjBase<T, R, D>;
+ friend std::hash<RcObjBase>;
template<typename T__, typename D__, typename T_, typename R_, typename D_>
friend RcObjBase<T__, R_, D__> static_pointer_cast(const RcObjBase<T_, R_, D_> &other);
template<typename T__, typename D__, typename T_, typename R_, typename D_>
@@ -352,16 +380,15 @@ class RcObjBase: public _RcObjBase<T, R, D> {
RcObjBase(BoxObj<T> &&box_ref): base_t(std::move(box_ref)) {}
template<typename T_, typename D_,
- typename = typename std::enable_if<!std::is_array<T_>::value>::value>
+ typename = typename std::enable_if<!std::is_array<T_>::value>::type>
RcObjBase(const RcObjBase<T_, R, D_> &other): base_t(other) {}
template<typename T_, typename D_,
- typename = typename std::enable_if<!std::is_array<T_>::value>::value>
+ typename = typename std::enable_if<!std::is_array<T_>::value>::type>
RcObjBase(RcObjBase<T_, R, D_> &&other): base_t(std::move(other)) {}
RcObjBase(const WeakObjBase<T, R> &other): base_t(other) {}
-
RcObjBase(const RcObjBase &other) = default;
RcObjBase(RcObjBase &&other) = default;
RcObjBase &operator=(const RcObjBase &) = default;
@@ -371,6 +398,7 @@ class RcObjBase: public _RcObjBase<T, R, D> {
template<typename T, typename R, typename D>
class RcObjBase<T[], R, D>: public _RcObjBase<T, R, D> {
using base_t = _RcObjBase<T, R, D>;
+ friend std::hash<RcObjBase>;
template<typename T__, typename D__, typename T_, typename R_, typename D_>
friend RcObjBase<T__, R_, D__> static_pointer_cast(const RcObjBase<T_, R_, D_> &other);
template<typename T__, typename D__, typename T_, typename R_, typename D_>
@@ -419,7 +447,7 @@ RcObjBase<T, R_, D> static_pointer_cast(RcObjBase<T_, R_, D_> &&other) {
template<typename T, typename R>
template<typename D>
-inline _WeakObjBase<T, R>::_WeakObjBase(const RcObjBase<T, R, D> &other):
+inline _WeakObjBase<T, R>::_WeakObjBase(const _RcObjBase<T, R, D> &other):
obj(other.obj), ctl(other.ctl) {
if (ctl) ctl->add_weak();
}
diff --git a/include/salticidae/util.h b/include/salticidae/util.h
index 0324215..de63146 100644
--- a/include/salticidae/util.h
+++ b/include/salticidae/util.h
@@ -271,47 +271,33 @@ class Event {
eb(eb), fd(fd), events(events),
ev(event_new(eb, fd, events, Event::_then, this)),
callback(callback) {}
- Event(const Event &other):
- eb(other.eb), fd(other.fd), events(other.events),
- ev(event_new(eb, fd, events, Event::_then, this)),
- callback(other.callback) {}
Event(Event &&other):
eb(other.eb), fd(other.fd), events(other.events),
- ev(event_new(eb, fd, events, Event::_then, this)),
- callback(std::move(other.callback)) {}
-
- void swap(Event &other) {
- std::swap(eb, other.eb);
- std::swap(fd, other.fd);
- std::swap(events, other.events);
- std::swap(ev, other.ev);
- std::swap(callback, other.callback);
+ callback(std::move(other.callback)) {
+ other.clear();
+ ev = event_new(eb, fd, events, Event::_then, this);
}
Event &operator=(Event &&other) {
- if (this != &other)
- {
- Event tmp(std::move(other));
- tmp.swap(*this);
- }
+ clear();
+ other.clear();
+ eb = other.eb;
+ fd = other.fd;
+ events = other.events;
+ ev = event_new(eb, fd, events, Event::_then, this);
+ callback = std::move(other.callback);
return *this;
}
- Event &operator=(const Event &other) {
- if (this != &other)
- {
- Event tmp(other);
- tmp.swap(*this);
- }
- return *this;
- }
+ ~Event() { clear(); }
- ~Event() {
+ void clear() {
if (ev != nullptr)
{
event_del(ev);
event_free(ev);
+ ev = nullptr;
}
}