]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
SourceLayout: C++11 upgrade for class YesNoNone
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 15 Jan 2016 06:47:59 +0000 (19:47 +1300)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 15 Jan 2016 06:47:59 +0000 (19:47 +1300)
* shuffles YesNoNone file to src/base/

* converts C++03 hacked 'bool' converter to a proper bool()
  operator.

* adds support for implicit defaults as will as explicit
  squid.conf settings.

* adds unit tests for YesNoNone

15 files changed:
src/Makefile.am
src/SquidConfig.h
src/YesNoNone.cc [deleted file]
src/YesNoNone.h [deleted file]
src/adaptation/ServiceConfig.h
src/adaptation/ecap/ServiceRep.cc
src/adaptation/icap/ServiceRep.cc
src/base/Makefile.am
src/base/YesNoNone.h [new file with mode: 0644]
src/tests/stub_cache_cf.cc
src/tests/testRock.cc
src/tests/testStoreController.cc
src/tests/testUfs.cc
src/tests/testYesNoNone.cc [new file with mode: 0644]
src/tests/testYesNoNone.h [new file with mode: 0644]

index a9205726a5ccdf04af661df2e20060f72db96443..f8ce0c39015f64115196f3df73df45d5bf2ce1ba 100644 (file)
@@ -230,8 +230,6 @@ squid_SOURCES = \
        AsyncEngine.h \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        CacheDigest.h \
@@ -920,7 +918,8 @@ check_PROGRAMS+=\
        tests/testSBufList \
        tests/testConfigParser \
        tests/testStatHist \
-       tests/testLookupTable
+       tests/testLookupTable \
+       tests/testYesNoNone
 
 if HAVE_FS_ROCK
 check_PROGRAMS += tests/testRock
@@ -1009,7 +1008,6 @@ tests_testHttpReply_SOURCES=\
        log/access_log.h \
        tests/stub_access_log.cc \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_cache_manager.cc \
        tests/stub_comm.cc \
@@ -1131,7 +1129,6 @@ tests_testACLMaxUserIP_SOURCES= \
        log/access_log.h \
        tests/stub_access_log.cc \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_client_side.cc \
        tests/stub_debug.cc \
@@ -1265,8 +1262,6 @@ tests_testCacheManager_SOURCES = \
        cache_manager.cc \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        CachePeer.cc \
@@ -1582,7 +1577,6 @@ tests_testDiskIO_SOURCES = \
        tests/stub_access_log.cc \
        tests/stub_acl.cc \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_cache_manager.cc \
        tests/stub_client_db.cc \
@@ -1698,8 +1692,6 @@ tests_testEvent_SOURCES = \
        tests/stub_CacheDigest.cc \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        CachePeer.cc \
@@ -1947,8 +1939,6 @@ tests_testEventLoop_SOURCES = \
        cache_manager.cc \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        CachePeer.cc \
@@ -2190,8 +2180,6 @@ tests_test_http_range_SOURCES = \
        BodyPipe.cc \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        CachePeer.cc \
@@ -2460,7 +2448,6 @@ tests_testHttp1Parser_SOURCES = \
        mime_header.h \
        String.cc \
        cache_cf.h \
-       YesNoNone.h \
        $(SBUF_SOURCE) \
        SBufAlgos.h \
        SBufAlgos.cc \
@@ -2526,8 +2513,6 @@ tests_testHttpRequest_SOURCES = \
        cache_manager.cc \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        debug.cc \
@@ -2867,7 +2852,6 @@ tests_testStore_SOURCES= \
        tests/stub_access_log.cc \
        tests/stub_acl.cc \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_cache_manager.cc \
        tests/stub_client_side_request.cc \
@@ -2973,7 +2957,6 @@ tests_testString_SOURCES = \
        tests/testString.cc \
        tests/testString.h \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_cache_manager.cc \
        tests/stub_cbdata.cc \
@@ -3094,7 +3077,6 @@ tests_testUfs_SOURCES = \
        ConfigOption.cc \
        tests/stub_acl.cc \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_helper.cc \
        cbdata.cc \
@@ -3294,7 +3276,6 @@ tests_testRock_SOURCES = \
        log/access_log.h \
        tests/stub_access_log.cc \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        client_db.h \
        tests/stub_cache_manager.cc \
@@ -3377,8 +3358,6 @@ tests_testURL_SOURCES = \
        BodyPipe.cc \
        cache_cf.h \
        AuthReg.h \
-       YesNoNone.h \
-       YesNoNone.cc \
        RefreshPattern.h \
        cache_cf.cc \
        tests/stub_cache_manager.cc \
@@ -3719,7 +3698,6 @@ tests_testConfigParser_SOURCES = \
        tests/testConfigParser.cc \
        tests/testConfigParser.h \
        cache_cf.h \
-       YesNoNone.h \
        tests/stub_cache_cf.cc \
        tests/stub_cache_manager.cc \
        tests/stub_cbdata.cc \
@@ -3825,6 +3803,18 @@ tests_testEnumIterator_LDADD = \
        $(XTRA_LIBS)
 tests_testEnumIterator_DEPENDENCIES =
 
+tests_testYesNoNone_SOURCES = \
+       tests/testYesNoNone.cc \
+       tests/testYesNoNone.h
+nodist_tests_testYesNoNone_SOURCES = \
+       tests/STUB.h \
+       base/YesNoNone.h
+tests_testYesNoNone_LDADD= \
+       $(SQUID_CPPUNIT_LIBS) \
+       $(COMPAT_LIB) \
+       $(XTRA_LIBS)
+tests_testYesNoNone_LDFLAGS = $(LIBADD_DL)
+
 TESTS += testHeaders
 
 ## Special Universal .h dependency test script
index 89818e7123a195431cf5f0ec601e76e2fe3ab148..0a0689e482f8b76b29084ed44f59f0cd6c0b03de 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "acl/forward.h"
 #include "base/RefCount.h"
+#include "base/YesNoNone.h"
 #include "ClientDelayConfig.h"
 #include "DelayConfig.h"
 #include "helper/ChildConfig.h"
@@ -23,7 +24,6 @@
 #include "ssl/support.h"
 #endif
 #include "store/forward.h"
-#include "YesNoNone.h"
 
 #if USE_OPENSSL
 class sslproxy_cert_sign;
diff --git a/src/YesNoNone.cc b/src/YesNoNone.cc
deleted file mode 100644 (file)
index 0309fe5..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * Copyright (C) 1996-2016 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.
- */
-
-#include "squid.h"
-#include "YesNoNone.h"
-
-YesNoNone::operator void*() const
-{
-    assert(option != 0); // must call configure() first
-    return option > 0 ? (void*)this : NULL;
-}
-
-void
-YesNoNone::configure(bool beSet)
-{
-    option = beSet ? +1 : -1;
-}
-
diff --git a/src/YesNoNone.h b/src/YesNoNone.h
deleted file mode 100644 (file)
index 151538f..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 1996-2016 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_YESNONONE_H_
-#define SQUID_YESNONONE_H_
-
-/// Used for boolean enabled/disabled options with complex default logic.
-/// Allows Squid to compute the right default after configuration.
-/// Checks that not-yet-defined option values are not used.
-class YesNoNone
-{
-// TODO: generalize to non-boolean option types
-public:
-    YesNoNone(): option(0) {}
-
-    /// returns true iff enabled; asserts if the option has not been configured
-    operator void *() const; // TODO: use a fancy/safer version of the operator
-
-    /// enables or disables the option;
-    void configure(bool beSet);
-
-    /// whether the option was enabled or disabled, by user or Squid
-    bool configured() const { return option != 0; }
-
-private:
-    enum { optUnspecified = -1, optDisabled = 0, optEnabled = 1 };
-    int option; ///< configured value or zero
-};
-
-#endif /* SQUID_YESNONONE_H_ */
-
index a926354e20593946fa490147fa5958ecee4c1de4..f18f3bb5c692f7ab9bc8a27d239349d939a6d8a2 100644 (file)
@@ -11,9 +11,9 @@
 
 #include "adaptation/Elements.h"
 #include "base/RefCount.h"
+#include "base/YesNoNone.h"
 #include "security/PeerOptions.h"
 #include "SquidString.h"
-#include "YesNoNone.h"
 
 namespace Adaptation
 {
index f0cd8339956e84b7b5b18191f29ac46cac04340c..7577c1b213c79f9c59c64273dc7b84f98a1a4530 100644 (file)
@@ -171,7 +171,7 @@ Adaptation::Ecap::ServiceRep::finalize()
 {
     Adaptation::Service::finalize();
     if (!cfg().connectionEncryption.configured())
-        writeableCfg().connectionEncryption.configure(true);
+        writeableCfg().connectionEncryption.defaultTo(true);
     theService = FindAdapterService(cfg().uri);
     if (theService) {
         try {
index 46c1aa793623a56141596a0e168f5b31924250b0..7aec5ca4f2f623f7a076f7bb8518d80aab71a8f1 100644 (file)
@@ -85,10 +85,10 @@ Adaptation::Icap::ServiceRep::finalize()
     if (cfg().secure.encryptTransport) {
         debugs(3, DBG_IMPORTANT, "Initializing service " << cfg().resource << " SSL context");
         sslContext = writeableCfg().secure.createClientContext(true);
-        if (!cfg().connectionEncryption.configured())
-            writeableCfg().connectionEncryption.configure(true);
-    } else if (!cfg().connectionEncryption.configured())
-        writeableCfg().connectionEncryption.configure(false);
+    }
+
+    if (!cfg().connectionEncryption.configured())
+        writeableCfg().connectionEncryption.defaultTo(cfg().secure.encryptTransport);
 
     theSessionFailures.configure(TheConfig.oldest_service_failure > 0 ?
                                  TheConfig.oldest_service_failure : -1);
index a5043dcfe488baccdc89b95034e5d8e54d0fdc12..86b515ca622e48164640bd16d5ecc080be6cd6ff 100644 (file)
@@ -38,4 +38,5 @@ libbase_la_SOURCES = \
        Subscription.h \
        TextException.cc \
        TextException.h \
-       TidyPointer.h
+       TidyPointer.h \
+       YesNoNone.h
diff --git a/src/base/YesNoNone.h b/src/base/YesNoNone.h
new file mode 100644 (file)
index 0000000..e9f1cf9
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 1996-2016 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_YESNONONE_H_
+#define SQUID_YESNONONE_H_
+
+#include "base/TextException.h"
+
+// TODO: generalize / template to non-boolean option types
+// and make YesNoNone the boolean instance of the template
+
+/**
+ * Used for boolean enabled/disabled options with complex default logic.
+ * Allows Squid to compute the right default after configuration.
+ * Checks that not-yet-defined option values are not used.
+ * Allows for implicit default Yes/No values to be used by initialization
+ * without configure() being called, but not dumped as squid.conf content.
+ *
+ * Use x.configure(bool) when the value is configured.
+ * Use x.defaultTo(bool) to assign defaults.
+ */
+class YesNoNone
+{
+    enum SetHow : uint8_t { optUnspecified = 0, optImplicitly = 1, optConfigured = 2 };
+
+public:
+    // this constructor initializes to 'unspecified' state
+    YesNoNone():
+        setHow_(optUnspecified),
+        option(false)
+    {}
+
+    // this constructor initializes to 'implicit' state
+    explicit YesNoNone(bool beSet):
+        setHow_(optImplicitly),
+        option(beSet)
+    {}
+
+    /// the boolean equivalent of the value stored.
+    /// asserts if the value has not been set.
+    explicit operator bool() const {
+        Must(setHow_ != optUnspecified);
+        return option;
+    }
+
+    /// enables or disables the option; updating to 'configured' state
+    void configure(bool beSet) {
+        setHow_ = optConfigured;
+        option = beSet;
+    }
+
+    /// enables or disables the option; updating to 'implicit' state
+    void defaultTo(bool beSet) {
+        Must(setHow_ != optConfigured);
+        setHow_ = optImplicitly;
+        option = beSet;
+    }
+
+    /// whether the option was enabled or disabled,
+    /// by squid.conf values resulting in explicit configure() usage.
+    bool configured() const {return setHow_ == optConfigured;}
+
+private:
+    SetHow setHow_; ///< how the option was set
+    bool option; ///< specified yes/no value; meaningless if optUnspecified
+};
+
+#endif /* SQUID_YESNONONE_H_ */
+
index 201eec72bf62293be48b46ce67b454c2bc4e5c53..edd5f5e6317b406a49fe0ddeb6cb303c2be6485a 100644 (file)
@@ -12,7 +12,6 @@
 #include "acl/Acl.h"
 #include "ConfigParser.h"
 #include "wordlist.h"
-#include "YesNoNone.h"
 
 #define STUB_API "cache_cf.cc"
 #include "tests/STUB.h"
@@ -28,5 +27,4 @@ void parse_time_t(time_t * var) STUB
 void ConfigParser::ParseUShort(unsigned short *var) STUB
 void dump_acl_access(StoreEntry * entry, const char *name, acl_access * head) STUB
 void dump_acl_list(StoreEntry*, ACLList*) STUB
-YesNoNone::operator void*() const { STUB_NOP; return NULL; }
 
index 303344de4eaa841a89fbbafa94680fb9315ad834..19b458e1a5bd6c09adfc9cab5e4fdf21547fd0c7 100644 (file)
@@ -57,6 +57,8 @@ testRock::setUp()
     if (0 > system ("rm -rf " TESTDIR))
         throw std::runtime_error("Failed to clean test work directory");
 
+    Config.memShared.defaultTo(false);
+
     // use current directory for shared segments (on path-based OSes)
     Ipc::Mem::Segment::BasePath = getcwd(cwd,MAXPATHLEN);
     if (Ipc::Mem::Segment::BasePath == NULL)
index 0d6c05618424b3d942d1fbdee0b645653e4c76cf..26f5f3307a5bd61fa676709df3a8e00cc7c0ff85 100644 (file)
@@ -55,6 +55,8 @@ commonInit()
     if (inited)
         return;
 
+    Config.memShared.defaultTo(false);
+
     Mem::Init();
 
     Config.Store.avgObjectSize = 1024;
index be17ac33b47cd3ea39709cd9eb5c6814db1ab75f..c346c0c5fdab5d0758d0a94c8e5ec2215d8c8d60 100644 (file)
@@ -65,6 +65,8 @@ testUfs::commonInit()
     Config.replPolicy = new RemovalPolicySettings;
     Config.replPolicy->type = xstrdup("lru");
 
+    Config.memShared.defaultTo(false);
+
     /* garh garh */
     storeReplAdd("lru", createRemovalPolicy_lru);
 
diff --git a/src/tests/testYesNoNone.cc b/src/tests/testYesNoNone.cc
new file mode 100644 (file)
index 0000000..7178a36
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 1996-2016 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.
+ */
+
+#include "squid.h"
+#include "base/YesNoNone.h"
+#include "tests/testYesNoNone.h"
+#include "unitTestMain.h"
+
+#include <stdexcept>
+
+CPPUNIT_TEST_SUITE_REGISTRATION( testYesNoNone );
+
+void
+testYesNoNone::testBasics()
+{
+    // unconfigured, non-implicit
+    {
+        YesNoNone v;
+        CPPUNIT_ASSERT_EQUAL(false, v.configured());
+        // cannot test the value it is 'undefined' and will assert
+    }
+    // implicit dtor test
+
+    // unconfigured, implicit true
+    {
+        YesNoNone v(true);
+        CPPUNIT_ASSERT_EQUAL(false, v.configured());
+        CPPUNIT_ASSERT(v);
+        CPPUNIT_ASSERT_EQUAL(true, static_cast<bool>(v));
+
+        // check explicit setter method
+        v.configure(false);
+        CPPUNIT_ASSERT_EQUAL(true, v.configured());
+        CPPUNIT_ASSERT(!v);
+        CPPUNIT_ASSERT_EQUAL(false, static_cast<bool>(v));
+    }
+
+    // unconfigured, implicit false
+    {
+        YesNoNone v(false);
+        CPPUNIT_ASSERT_EQUAL(false, v.configured());
+        CPPUNIT_ASSERT(!v);
+        CPPUNIT_ASSERT_EQUAL(false, static_cast<bool>(v));
+
+        // check assignment operator
+        v = YesNoNone(true);
+        CPPUNIT_ASSERT_EQUAL(false, v.configured());
+        CPPUNIT_ASSERT(v);
+        CPPUNIT_ASSERT_EQUAL(true, static_cast<bool>(v));
+    }
+}
diff --git a/src/tests/testYesNoNone.h b/src/tests/testYesNoNone.h
new file mode 100644 (file)
index 0000000..0062b9f
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 1996-2016 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_TESTS_TESTYESNONONE_H
+#define SQUID_SRC_TESTS_TESTYESNONONE_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+/*
+ * demonstration test file, as new idioms are made they will
+ * be shown in the testYesNoNone source.
+ */
+
+class testYesNoNone : public CPPUNIT_NS::TestFixture
+{
+    CPPUNIT_TEST_SUITE( testYesNoNone );
+    /* note the statement here and then the actual prototype below */
+    CPPUNIT_TEST( testBasics );
+    CPPUNIT_TEST_SUITE_END();
+
+public:
+
+protected:
+    void testBasics();
+};
+
+#endif /* SQUID_SRC_TESTS_TESTYESNONONE_H */
+