about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFedor Indutny <fedor@indutny.com>2016-05-31 20:34:14 -0400
committerFedor Indutny <fedor@indutny.com>2016-05-31 20:34:14 -0400
commit087b8ef36bea5f6225e4b4a2c0119ba05a489641 (patch)
tree2df54ccca7499f0084373da2d072a4e50308dc59
parent7d91a2ddcb97dc64ed3bc516829dc00d0f010754 (diff)
downloaduv_link_t-087b8ef36bea5f6225e4b4a2c0119ba05a489641.tar.gz
api: default method implementations
-rw-r--r--docs/api.md36
-rw-r--r--include/uv_link_t.h28
-rw-r--r--src/defaults.c57
-rw-r--r--src/uv_link_observer_t.c66
-rw-r--r--src/uv_link_t.c4
-rw-r--r--uv_link_t.gyp1
6 files changed, 134 insertions, 58 deletions
diff --git a/docs/api.md b/docs/api.md
index 1779a94..d1860a0 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -269,6 +269,42 @@ pointers to the functions.
 
 Must be provided to [`uv_link_init()`][].
 
+These functions could be used as a default implementations of
+`uv_link_methods_t` callbacks:
+
+```c
+int uv_link_default_read_start(uv_link_t* link);
+int uv_link_default_read_stop(uv_link_t* link);
+int uv_link_default_write(uv_link_t* link,
+                          uv_link_t* source,
+                          const uv_buf_t bufs[],
+                          unsigned int nbufs,
+                          uv_stream_t* send_handle,
+                          uv_link_write_cb cb,
+                          void* arg);
+int uv_link_default_try_write(uv_link_t* link,
+                              const uv_buf_t bufs[],
+                              unsigned int nbufs);
+int uv_link_default_shutdown(uv_link_t* link,
+                             uv_link_t* source,
+                             uv_link_shutdown_cb cb,
+                             void* arg);
+void uv_link_default_close(uv_link_t* link, uv_link_t* source,
+                           uv_link_close_cb cb);
+```
+
+These maybe used for [`uv_methods_talloc_cb_override`][] and
+[`uv_methods_t.read_cb_override`][].
+
+```c
+void uv_link_default_alloc_cb(uv_link_t* link,
+                              size_t suggested_size,
+                              uv_buf_t* buf);
+void uv_link_default_read_cb(uv_link_t* link,
+                             ssize_t nread,
+                             const uv_buf_t* buf);
+```
+
 ### .read_start
 
 ```c
diff --git a/include/uv_link_t.h b/include/uv_link_t.h
index 03d34df..5e77e34 100644
--- a/include/uv_link_t.h
+++ b/include/uv_link_t.h
@@ -159,4 +159,32 @@ struct uv_link_observer_s {
 
 UV_EXTERN int uv_link_observer_init(uv_link_observer_t* observer);
 
+/* Default callbacks */
+
+int uv_link_default_read_start(uv_link_t* link);
+int uv_link_default_read_stop(uv_link_t* link);
+int uv_link_default_write(uv_link_t* link,
+                          uv_link_t* source,
+                          const uv_buf_t bufs[],
+                          unsigned int nbufs,
+                          uv_stream_t* send_handle,
+                          uv_link_write_cb cb,
+                          void* arg);
+int uv_link_default_try_write(uv_link_t* link,
+                              const uv_buf_t bufs[],
+                              unsigned int nbufs);
+int uv_link_default_shutdown(uv_link_t* link,
+                             uv_link_t* source,
+                             uv_link_shutdown_cb cb,
+                             void* arg);
+void uv_link_default_close(uv_link_t* link, uv_link_t* source,
+                           uv_link_close_cb cb);
+
+void uv_link_default_alloc_cb(uv_link_t* link,
+                              size_t suggested_size,
+                              uv_buf_t* buf);
+void uv_link_default_read_cb(uv_link_t* link,
+                             ssize_t nread,
+                             const uv_buf_t* buf);
+
 #endif  /* INCLUDE_UV_LINK_H_ */
diff --git a/src/defaults.c b/src/defaults.c
new file mode 100644
index 0000000..f566356
--- /dev/null
+++ b/src/defaults.c
@@ -0,0 +1,57 @@
+#include "src/common.h"
+
+int uv_link_default_read_start(uv_link_t* link) {
+  return uv_link_read_start(link->parent);
+}
+
+
+int uv_link_default_read_stop(uv_link_t* link) {
+  return uv_link_read_stop(link->parent);
+}
+
+
+int uv_link_default_write(uv_link_t* link,
+                          uv_link_t* source,
+                          const uv_buf_t bufs[],
+                          unsigned int nbufs,
+                          uv_stream_t* send_handle,
+                          uv_link_write_cb cb,
+                          void* arg) {
+  return uv_link_propagate_write(link->parent, source, bufs, nbufs,
+                                 send_handle, cb, arg);
+}
+
+
+int uv_link_default_try_write(uv_link_t* link,
+                              const uv_buf_t bufs[],
+                              unsigned int nbufs) {
+  return uv_link_try_write(link->parent, bufs, nbufs);
+}
+
+
+int uv_link_default_shutdown(uv_link_t* link,
+                             uv_link_t* source,
+                             uv_link_shutdown_cb cb,
+                             void* arg) {
+  return uv_link_propagate_shutdown(link->parent, source, cb, arg);
+}
+
+
+void uv_link_default_close(uv_link_t* link, uv_link_t* source,
+                           uv_link_close_cb cb) {
+  cb(source);
+}
+
+
+void uv_link_default_alloc_cb(uv_link_t* link,
+                              size_t suggested_size,
+                              uv_buf_t* buf) {
+  return uv_link_propagate_alloc_cb(link, suggested_size, buf);
+}
+
+
+void uv_link_default_read_cb(uv_link_t* link,
+                             ssize_t nread,
+                             const uv_buf_t* buf) {
+  return uv_link_propagate_read_cb(link, nread, buf);
+}
diff --git a/src/uv_link_observer_t.c b/src/uv_link_observer_t.c
index 395a97c..0111440 100644
--- a/src/uv_link_observer_t.c
+++ b/src/uv_link_observer_t.c
@@ -4,50 +4,6 @@
 #include "src/common.h"
 
 
-static int uv_link_observer_read_start(uv_link_t* link) {
-  return uv_link_read_start(link->parent);
-}
-
-
-static int uv_link_observer_read_stop(uv_link_t* link) {
-  return uv_link_read_stop(link->parent);
-}
-
-
-static int uv_link_observer_write(uv_link_t* link,
-                                  uv_link_t* source,
-                                  const uv_buf_t bufs[],
-                                  unsigned int nbufs,
-                                  uv_stream_t* send_handle,
-                                  uv_link_write_cb cb,
-                                  void* arg) {
-  return uv_link_propagate_write(link->parent, source, bufs, nbufs,
-                                 send_handle, cb, arg);
-}
-
-
-static int uv_link_observer_try_write(uv_link_t* link,
-                                      const uv_buf_t bufs[],
-                                      unsigned int nbufs) {
-  return uv_link_try_write(link->parent, bufs, nbufs);
-}
-
-
-static int uv_link_observer_shutdown(uv_link_t* link,
-                                     uv_link_t* source,
-                                     uv_link_shutdown_cb cb,
-                                     void* arg) {
-  return uv_link_propagate_shutdown(link->parent, source, cb, arg);
-}
-
-
-static void uv_link_observer_alloc_cb(uv_link_t* link,
-                                      size_t suggested_size,
-                                      uv_buf_t* buf) {
-  return uv_link_propagate_alloc_cb(link, suggested_size, buf);
-}
-
-
 static void uv_link_observer_read_cb(uv_link_t* link,
                                      ssize_t nread,
                                      const uv_buf_t* buf) {
@@ -62,21 +18,15 @@ static void uv_link_observer_read_cb(uv_link_t* link,
 }
 
 
-void uv_link_observer_close(uv_link_t* link, uv_link_t* source,
-                            uv_link_close_cb cb) {
-  cb(source);
-}
-
-
 static uv_link_methods_t uv_link_observer_methods = {
-  .read_start = uv_link_observer_read_start,
-  .read_stop = uv_link_observer_read_stop,
-  .write = uv_link_observer_write,
-  .try_write = uv_link_observer_try_write,
-  .shutdown = uv_link_observer_shutdown,
-  .close = uv_link_observer_close,
-
-  .alloc_cb_override = uv_link_observer_alloc_cb,
+  .read_start = uv_link_default_read_start,
+  .read_stop = uv_link_default_read_stop,
+  .write = uv_link_default_write,
+  .try_write = uv_link_default_try_write,
+  .shutdown = uv_link_default_shutdown,
+  .close = uv_link_default_close,
+
+  .alloc_cb_override = uv_link_default_alloc_cb,
   .read_cb_override = uv_link_observer_read_cb
 };
 
diff --git a/src/uv_link_t.c b/src/uv_link_t.c
index 852e26e..11bacb2 100644
--- a/src/uv_link_t.c
+++ b/src/uv_link_t.c
@@ -81,6 +81,10 @@ int uv_link_chain(uv_link_t* from, uv_link_t* to) {
   if (from->child != NULL || to->parent != NULL)
     return UV_EINVAL;
 
+  CHECK(to->methods->alloc_cb_override != NULL &&
+            to->methods->read_cb_override != NULL,
+        "Attempting to chain link without overriding callbacks");
+
   from->child = to;
   to->parent = from;
 
diff --git a/uv_link_t.gyp b/uv_link_t.gyp
index b0c63df..16f4205 100644
--- a/uv_link_t.gyp
+++ b/uv_link_t.gyp
@@ -14,6 +14,7 @@
     ],
 
     "sources": [
+      "src/defaults.c",
       "src/uv_link_t.c",
       "src/uv_link_observer_t.c",
       "src/uv_link_source_t.c",