summaryrefslogtreecommitdiff
path: root/src/stream_base.h
diff options
context:
space:
mode:
authorFedor Indutny <fedor@indutny.com>2015-08-11 12:27:22 -0700
committerFedor Indutny <fedor@indutny.com>2015-08-20 01:14:37 -0700
commit291b310e219023c4d93b216b1081ef47386f8750 (patch)
treef9f28676798681673642bee1156185b15809e911 /src/stream_base.h
parent0d39d35739828afdc388140ccb30420aea3d3484 (diff)
downloadnode-new-291b310e219023c4d93b216b1081ef47386f8750.tar.gz
stream_base: various improvements
Expose and use in TLSWrap an `v8::External` wrap of the `StreamBase*` pointer instead of guessing the ancestor C++ class in `node_wrap.h`. Make use of `StreamBase::Callback` structure for storing/passing both callback and context in a single object. Introduce `GetObject()` for future user-land usage, when a child class is not going to be inherited from AsyncWrap. PR-URL: https://github.com/nodejs/node/pull/2351 Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Diffstat (limited to 'src/stream_base.h')
-rw-r--r--src/stream_base.h72
1 files changed, 42 insertions, 30 deletions
diff --git a/src/stream_base.h b/src/stream_base.h
index 31854b3435..c5a0977792 100644
--- a/src/stream_base.h
+++ b/src/stream_base.h
@@ -106,6 +106,22 @@ class WriteWrap: public ReqWrap<uv_write_t>,
class StreamResource {
public:
+ template <class T>
+ struct Callback {
+ Callback() : fn(nullptr), ctx(nullptr) {}
+ Callback(T fn, void* ctx) : fn(fn), ctx(ctx) {}
+ Callback(const Callback&) = default;
+
+ inline bool is_empty() { return fn == nullptr; }
+ inline void clear() {
+ fn = nullptr;
+ ctx = nullptr;
+ }
+
+ T fn;
+ void* ctx;
+ };
+
typedef void (*AfterWriteCb)(WriteWrap* w, void* ctx);
typedef void (*AllocCb)(size_t size, uv_buf_t* buf, void* ctx);
typedef void (*ReadCb)(ssize_t nread,
@@ -113,11 +129,8 @@ class StreamResource {
uv_handle_type pending,
void* ctx);
- StreamResource() : after_write_cb_(nullptr),
- alloc_cb_(nullptr),
- read_cb_(nullptr) {
+ StreamResource() {
}
-
virtual ~StreamResource() = default;
virtual int DoShutdown(ShutdownWrap* req_wrap) = 0;
@@ -131,44 +144,37 @@ class StreamResource {
// Events
inline void OnAfterWrite(WriteWrap* w) {
- if (after_write_cb_ != nullptr)
- after_write_cb_(w, after_write_ctx_);
+ if (!after_write_cb_.is_empty())
+ after_write_cb_.fn(w, after_write_cb_.ctx);
}
inline void OnAlloc(size_t size, uv_buf_t* buf) {
- if (alloc_cb_ != nullptr)
- alloc_cb_(size, buf, alloc_ctx_);
+ if (!alloc_cb_.is_empty())
+ alloc_cb_.fn(size, buf, alloc_cb_.ctx);
}
inline void OnRead(size_t nread,
const uv_buf_t* buf,
uv_handle_type pending = UV_UNKNOWN_HANDLE) {
- if (read_cb_ != nullptr)
- read_cb_(nread, buf, pending, read_ctx_);
+ if (!read_cb_.is_empty())
+ read_cb_.fn(nread, buf, pending, read_cb_.ctx);
}
- inline void set_after_write_cb(AfterWriteCb cb, void* ctx) {
- after_write_ctx_ = ctx;
- after_write_cb_ = cb;
+ inline void set_after_write_cb(Callback<AfterWriteCb> c) {
+ after_write_cb_ = c;
}
- inline void set_alloc_cb(AllocCb cb, void* ctx) {
- alloc_cb_ = cb;
- alloc_ctx_ = ctx;
- }
+ inline void set_alloc_cb(Callback<AllocCb> c) { alloc_cb_ = c; }
+ inline void set_read_cb(Callback<ReadCb> c) { read_cb_ = c; }
- inline void set_read_cb(ReadCb cb, void* ctx) {
- read_cb_ = cb;
- read_ctx_ = ctx;
- }
+ inline Callback<AfterWriteCb> after_write_cb() { return after_write_cb_; }
+ inline Callback<AllocCb> alloc_cb() { return alloc_cb_; }
+ inline Callback<ReadCb> read_cb() { return read_cb_; }
private:
- AfterWriteCb after_write_cb_;
- void* after_write_ctx_;
- AllocCb alloc_cb_;
- void* alloc_ctx_;
- ReadCb read_cb_;
- void* read_ctx_;
+ Callback<AfterWriteCb> after_write_cb_;
+ Callback<AllocCb> alloc_cb_;
+ Callback<ReadCb> read_cb_;
};
class StreamBase : public StreamResource {
@@ -211,7 +217,9 @@ class StreamBase : public StreamResource {
virtual ~StreamBase() = default;
- virtual AsyncWrap* GetAsyncWrap() = 0;
+ // One of these must be implemented
+ virtual AsyncWrap* GetAsyncWrap();
+ virtual v8::Local<v8::Object> GetObject();
// Libuv callbacks
static void AfterShutdown(ShutdownWrap* req, int status);
@@ -227,8 +235,12 @@ class StreamBase : public StreamResource {
int WriteString(const v8::FunctionCallbackInfo<v8::Value>& args);
template <class Base>
- static void GetFD(v8::Local<v8::String>,
- const v8::PropertyCallbackInfo<v8::Value>&);
+ static void GetFD(v8::Local<v8::String> key,
+ const v8::PropertyCallbackInfo<v8::Value>& args);
+
+ template <class Base>
+ static void GetExternal(v8::Local<v8::String> key,
+ const v8::PropertyCallbackInfo<v8::Value>& args);
template <class Base,
int (StreamBase::*Method)( // NOLINT(whitespace/parens)