From: Amos Jeffries Date: Fri, 15 Jan 2016 06:47:59 +0000 (+1300) Subject: SourceLayout: C++11 upgrade for class YesNoNone X-Git-Tag: SQUID_4_0_5~38 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c50b35b557e59c83fa7e4d1a4bb911ac5148fe31;p=thirdparty%2Fsquid.git SourceLayout: C++11 upgrade for class YesNoNone * 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 --- diff --git a/src/Makefile.am b/src/Makefile.am index a9205726a5..f8ce0c3901 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/SquidConfig.h b/src/SquidConfig.h index 89818e7123..0a0689e482 100644 --- a/src/SquidConfig.h +++ b/src/SquidConfig.h @@ -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 index 0309fe54a3..0000000000 --- a/src/YesNoNone.cc +++ /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 index 151538f027..0000000000 --- a/src/YesNoNone.h +++ /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_ */ - diff --git a/src/adaptation/ServiceConfig.h b/src/adaptation/ServiceConfig.h index a926354e20..f18f3bb5c6 100644 --- a/src/adaptation/ServiceConfig.h +++ b/src/adaptation/ServiceConfig.h @@ -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 { diff --git a/src/adaptation/ecap/ServiceRep.cc b/src/adaptation/ecap/ServiceRep.cc index f0cd833995..7577c1b213 100644 --- a/src/adaptation/ecap/ServiceRep.cc +++ b/src/adaptation/ecap/ServiceRep.cc @@ -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 { diff --git a/src/adaptation/icap/ServiceRep.cc b/src/adaptation/icap/ServiceRep.cc index 46c1aa7936..7aec5ca4f2 100644 --- a/src/adaptation/icap/ServiceRep.cc +++ b/src/adaptation/icap/ServiceRep.cc @@ -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); diff --git a/src/base/Makefile.am b/src/base/Makefile.am index a5043dcfe4..86b515ca62 100644 --- a/src/base/Makefile.am +++ b/src/base/Makefile.am @@ -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 index 0000000000..e9f1cf953e --- /dev/null +++ b/src/base/YesNoNone.h @@ -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_ */ + diff --git a/src/tests/stub_cache_cf.cc b/src/tests/stub_cache_cf.cc index 201eec72bf..edd5f5e631 100644 --- a/src/tests/stub_cache_cf.cc +++ b/src/tests/stub_cache_cf.cc @@ -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; } diff --git a/src/tests/testRock.cc b/src/tests/testRock.cc index 303344de4e..19b458e1a5 100644 --- a/src/tests/testRock.cc +++ b/src/tests/testRock.cc @@ -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) diff --git a/src/tests/testStoreController.cc b/src/tests/testStoreController.cc index 0d6c056184..26f5f3307a 100644 --- a/src/tests/testStoreController.cc +++ b/src/tests/testStoreController.cc @@ -55,6 +55,8 @@ commonInit() if (inited) return; + Config.memShared.defaultTo(false); + Mem::Init(); Config.Store.avgObjectSize = 1024; diff --git a/src/tests/testUfs.cc b/src/tests/testUfs.cc index be17ac33b4..c346c0c5fd 100644 --- a/src/tests/testUfs.cc +++ b/src/tests/testUfs.cc @@ -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 index 0000000000..7178a36fda --- /dev/null +++ b/src/tests/testYesNoNone.cc @@ -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 + +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(v)); + + // check explicit setter method + v.configure(false); + CPPUNIT_ASSERT_EQUAL(true, v.configured()); + CPPUNIT_ASSERT(!v); + CPPUNIT_ASSERT_EQUAL(false, static_cast(v)); + } + + // unconfigured, implicit false + { + YesNoNone v(false); + CPPUNIT_ASSERT_EQUAL(false, v.configured()); + CPPUNIT_ASSERT(!v); + CPPUNIT_ASSERT_EQUAL(false, static_cast(v)); + + // check assignment operator + v = YesNoNone(true); + CPPUNIT_ASSERT_EQUAL(false, v.configured()); + CPPUNIT_ASSERT(v); + CPPUNIT_ASSERT_EQUAL(true, static_cast(v)); + } +} diff --git a/src/tests/testYesNoNone.h b/src/tests/testYesNoNone.h new file mode 100644 index 0000000000..0062b9f968 --- /dev/null +++ b/src/tests/testYesNoNone.h @@ -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 + +/* + * 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 */ +