]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Create Packable interface class
authorAmos Jeffries <squid3@treenet.co.nz>
Tue, 3 Mar 2015 09:45:37 +0000 (01:45 -0800)
committerAmos Jeffries <squid3@treenet.co.nz>
Tue, 3 Mar 2015 09:45:37 +0000 (01:45 -0800)
Packer class model used C-style function pointers and a standalone
object to perform C-style trampoline for function/method calls.

C++ virtual methods offer to inline all that directly in the data store
objects and enforces type safety on the child object methods instead of
forcing manual type casting on developers.

Re-implement Packer as a wrapper class providing the Packable interface
for backward compatibility with Packer* code. Future code should inherit
objects directly from Packable and implement the interface.

src/Packer.cc
src/Packer.h
src/base/Makefile.am
src/base/Packable.h [new file with mode: 0644]

index fcc674cc751545ee6a6c09d6d2c0a6c95207cc1c..4f51f8d1ebc623df61d4b5426b0faaca6d2828ab 100644 (file)
@@ -6,38 +6,7 @@
  * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-/* DEBUG: section 60    Packer: A uniform interface to store-like modules */
-
-/*
- * Rationale:
- * ----------
- *
- * OK, we have two major interfaces comm.c and store.c.
- *
- * Store.c has a nice storeAppend[Printf] capability which makes "storing"
- * things easy and painless.
- *
- * Comm.c lacks commAppend[Printf] because comm does not handle its own
- * buffers (no mem_obj equivalent for comm.c).
- *
- * Thus, if one wants to be able to store _and_ Comm::Write an object, s/he
- * has to implement two almost identical functions.
- *
- * Packer
- * ------
- *
- * Packer provides for a more uniform interface to store and comm modules.
- * Packer has its own append and printf routines that "know" where to send
- * incoming data. In case of store interface, Packer sends data to
- * storeAppend.  Otherwise, Packer uses a MemBuf that can be flushed later to
- * Comm::Write.
- *
- * Thus, one can write just one function that will either "pack" things for
- * Comm::Write or "append" things to store, depending on actual packer
- * supplied.
- *
- * It is amazing how much work a tiny object can save. :)
- */
+/* DEBUG: section 60    Generic Data Packer */
 
 #include "squid.h"
 #include "MemBuf.h"
index 86a37bd6d56d087ddfb757ca2660763eeaa79ec9..1e630838da05ae2946387a56e37840ed1b8e466b 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef SQUID_PACKER_H
 #define SQUID_PACKER_H
 
+#include "base/Packable.h"
+
 /* see Packer.cc for description */
 class Packer;
 
@@ -19,18 +21,14 @@ typedef void (*ObjPackMethod) (void *obj, Packer * p);
 typedef void (*append_f) (void *, const char *buf, int size);
 typedef void (*vprintf_f) (void *, const char *fmt, va_list args);
 
-class Packer
+class Packer : public Packable
 {
 
 public:
     virtual ~Packer();
 
+    /* Packable API */
     virtual void append(const char *buf, int size);
-
-    /*
-     * \note  we use Printf instead of printf so the compiler won't
-     *        think we're calling the libc printf()
-     */
     virtual void Printf(const char *fmt,...) PRINTF_FORMAT_ARG2;
 
     /* protected, use interface functions instead */
index 8904db7f20bc162f4d177b347a69a9a84663185d..8643df4da23cdf11bcce6418506a397bbe44fe6d 100644 (file)
@@ -26,6 +26,7 @@ libbase_la_SOURCES = \
        InstanceId.h \
        Lock.h \
        LruMap.h \
+       Packable.h \
        RunnersRegistry.cc \
        RunnersRegistry.h \
        Subscription.h \
diff --git a/src/base/Packable.h b/src/base/Packable.h
new file mode 100644 (file)
index 0000000..770bd2c
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#ifndef SQUID_SRC_BASE_PACKABLE_H
+#define SQUID_SRC_BASE_PACKABLE_H
+
+/**
+ * A uniform interface to store-like modules
+ *
+ * Rationale:
+ * ----------
+ *
+ * We have two major interfaces Comm and Store, which take a variety of
+ * different data buffering objects and have different output actions
+ * to be performed on data.
+ *
+ * Store has a nice storeAppend[Printf] capability which makes "storing"
+ * things easy and painless.
+ *
+ * Comm lacks commAppend[Printf] because Comm does not handle its own
+ * buffers (no mem_obj equivalent for Comm).
+ *
+ * Thus, if one wants to be able to Store _and_ Comm::Write an object, 'e
+ * has to implement almost identical functions for using all the data
+ * storage objects and their associated actions. Doing this for all the
+ * available data storage types is a tedious nightmare of almost-duplicated
+ * code.
+ *
+ * Packer
+ * ------
+ *
+ * Objects inheriting from Packable provide a uniform interface for code to
+ * assemble data before passing to Store and Comm modules.
+ *
+ * Packable objects have their own append and printf routines that "know"
+ * where to send incoming data. In case of Store interface, sending data to
+ * storeAppend. Packable buffer objects retain the data such that it can be
+ * flushed later to Comm::Write.
+ *
+ * Thus, one can write just one function that will take a Packer* pointer
+ * and either "pack" things for Comm::Write or "append" things to Store,
+ * depending on actual Packable object supplied.
+ */
+class Packable
+{
+public:
+    /// Appends a c-string to existing packed data.
+    virtual void append(const char *buf, int size) = 0;
+
+    /**
+     * Append operation with printf-style arguments.
+     *
+     * \note  we use Printf instead of printf so the compiler won't
+     *        think we're calling the libc printf()
+     */
+    virtual void Printf(const char *fmt,...) PRINTF_FORMAT_ARG2 = 0;
+};
+
+#endif /* SQUID_SRC_BASE_PACKABLE_H */
+