You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by am...@apache.org on 2018/07/27 21:08:53 UTC

[trafficserver] 01/02: For PostScript class, remove problematic parameter forwarding to functor (rely on lambda capturing instead).

This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit df6c2fb2dcc7d347bfca6fba74f2a9e3264f0b9f
Author: Walter Karas <wk...@oath.com>
AuthorDate: Fri Jul 27 10:22:31 2018 -0500

    For PostScript class, remove problematic parameter forwarding to functor (rely on lambda capturing instead).
---
 lib/ts/Makefile.am                   |  6 +++---
 lib/ts/PostScript.h                  | 17 +++++++----------
 lib/ts/unit-tests/test_PostScript.cc | 20 ++++++++++----------
 3 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/lib/ts/Makefile.am b/lib/ts/Makefile.am
index 8b500a9..ef44472 100644
--- a/lib/ts/Makefile.am
+++ b/lib/ts/Makefile.am
@@ -20,7 +20,7 @@ include $(top_srcdir)/build/tidy.mk
 
 library_includedir=$(includedir)/ts
 
-library_include_HEADERS = apidefs.h TextView.h PostScript.h
+library_include_HEADERS = apidefs.h PostScript.h TextView.h
 
 noinst_PROGRAMS = mkdfa CompileParseRules
 check_PROGRAMS = test_tsutil test_arena test_atomic test_freelist test_geometry test_List test_Map test_Vec test_X509HostnameValidator test_tslib
@@ -75,7 +75,6 @@ libtsutil_la_SOURCES = \
 	EventNotify.h \
 	fastlz.c \
 	fastlz.h \
-	PostScript.h \
 	Hash.cc \
 	HashFNV.cc \
 	HashFNV.h \
@@ -183,6 +182,7 @@ libtsutil_la_SOURCES = \
 	MT_hashtable.h \
 	ParseRules.cc \
 	ParseRules.h \
+	PostScript.h \
 	PriorityQueue.h \
 	Ptr.h \
 	RawHashTable.cc \
@@ -266,7 +266,6 @@ test_tslib_SOURCES = \
 	unit-tests/unit_test_main.cc \
 	unit-tests/test_BufferWriter.cc \
 	unit-tests/test_BufferWriterFormat.cc \
-	unit-tests/test_PostScript.cc \
 	unit-tests/test_History.cc \
 	unit-tests/test_ink_inet.cc \
 	unit-tests/test_IntrusiveDList.cc \
@@ -276,6 +275,7 @@ test_tslib_SOURCES = \
 	unit-tests/test_MemSpan.cc \
 	unit-tests/test_MemArena.cc \
 	unit-tests/test_MT_hashtable.cc \
+	unit-tests/test_PostScript.cc \
 	unit-tests/test_Ptr.cc \
 	unit-tests/test_Regex.cc \
 	unit-tests/test_Scalar.cc \
diff --git a/lib/ts/PostScript.h b/lib/ts/PostScript.h
index af91471..b7deafa 100644
--- a/lib/ts/PostScript.h
+++ b/lib/ts/PostScript.h
@@ -24,28 +24,26 @@
 
 #pragma once
 
-#include <tuple>
-
 namespace ts
 {
-// The destructor of this class calls the function object passed to its constructor, with the given arguments.
-// For example:
-//   ts::PostScript g(TSHandleMLocRelease, bufp, parent, hdr);
+// The destructor of this class calls the function object passed to its constructor.  The function object must support a
+// function call operator with no parameters.  For example:
+//
+//   ts::PostScript g([=]() -> void { TSHandleMLocRelease(bufp, parent, hdr); });
 //
 // The release() member will prevent the call to the function upon destruction.
 //
-// Helpful in avoiding errors due to exception throws or error function return points, like the one that caused
-// Heartbleed.
+// Helpful in avoiding errors due to exception throws or error function return points (like the one that caused Heartbleed).
 //
 template <typename Callable, typename... Args> class PostScript
 {
 public:
-  PostScript(Callable f, Args &&... args) : _f(f), _argsTuple(args...) {}
+  PostScript(Callable f) : _f(f) {}
 
   ~PostScript()
   {
     if (_armed) {
-      std::apply(_f, _argsTuple);
+      _f();
     }
   }
 
@@ -62,7 +60,6 @@ public:
 private:
   bool _armed = true;
   Callable _f;
-  std::tuple<Args...> _argsTuple;
 };
 
 } // end namespace ts
diff --git a/lib/ts/unit-tests/test_PostScript.cc b/lib/ts/unit-tests/test_PostScript.cc
index 1f95fec..2eb640e 100644
--- a/lib/ts/unit-tests/test_PostScript.cc
+++ b/lib/ts/unit-tests/test_PostScript.cc
@@ -30,14 +30,17 @@ int f1Called;
 int f2Called;
 int f3Called;
 
+int dummy;
+
 void
-f1(int a, double b, int c)
+f1(int a, double b, int *c, int &d)
 {
   ++f1Called;
 
   REQUIRE(a == 1);
   REQUIRE(b == 2.0);
-  REQUIRE(c == 3);
+  REQUIRE(c == &dummy);
+  REQUIRE(&d == &dummy);
 }
 
 void
@@ -55,17 +58,15 @@ f3(int a, double b)
   REQUIRE(b == 6.0);
 }
 
-} // namespace
+} // end anonymous namespace
 
 TEST_CASE("PostScript", "[PSC]")
 {
-  int lambdaCalled = 0;
-
   {
-    ts::PostScript g1(f1, 1, 2.0, 3);
-    ts::PostScript g2(f2, 4);
-    ts::PostScript g3(f3, 5, 6.0);
-    ts::PostScript g4([&]() -> void { ++lambdaCalled; });
+    int *p = &dummy;
+    ts::PostScript g1([&]() -> void { f1(1, 2.0, p, dummy); });
+    ts::PostScript g2([=]() -> void { f2(4); });
+    ts::PostScript g3([=]() -> void { f3(5, 6.0); });
 
     g2.release();
   }
@@ -73,5 +74,4 @@ TEST_CASE("PostScript", "[PSC]")
   REQUIRE(f1Called == 1);
   REQUIRE(f2Called == 0);
   REQUIRE(f3Called == 1);
-  REQUIRE(lambdaCalled == 1);
 }