]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/YesNoNone.h
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / base / YesNoNone.h
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_YESNONONE_H_
10 #define SQUID_YESNONONE_H_
11
12 #include "base/TextException.h"
13
14 // TODO: generalize / template to non-boolean option types
15 // and make YesNoNone the boolean instance of the template
16
17 /**
18 * Used for boolean enabled/disabled options with complex default logic.
19 * Allows Squid to compute the right default after configuration.
20 * Checks that not-yet-defined option values are not used.
21 * Allows for implicit default Yes/No values to be used by initialization
22 * without configure() being called, but not dumped as squid.conf content.
23 *
24 * Use x.configure(bool) when the value is configured.
25 * Use x.defaultTo(bool) to assign defaults.
26 */
27 class YesNoNone
28 {
29 enum SetHow : uint8_t { optUnspecified = 0, optImplicitly = 1, optConfigured = 2 };
30
31 public:
32 // this constructor initializes to 'unspecified' state
33 YesNoNone():
34 setHow_(optUnspecified),
35 option(false)
36 {}
37
38 // this constructor initializes to 'implicit' state
39 explicit YesNoNone(bool beSet):
40 setHow_(optImplicitly),
41 option(beSet)
42 {}
43
44 /// the boolean equivalent of the value stored.
45 /// asserts if the value has not been set.
46 explicit operator bool() const {
47 Must(setHow_ != optUnspecified);
48 return option;
49 }
50
51 /// enables or disables the option; updating to 'configured' state
52 void configure(bool beSet) {
53 setHow_ = optConfigured;
54 option = beSet;
55 }
56
57 /// enables or disables the option; updating to 'implicit' state
58 void defaultTo(bool beSet) {
59 Must(setHow_ != optConfigured);
60 setHow_ = optImplicitly;
61 option = beSet;
62 }
63
64 /// whether the option was enabled or disabled,
65 /// by squid.conf values resulting in explicit configure() usage.
66 bool configured() const {return setHow_ == optConfigured;}
67
68 private:
69 SetHow setHow_; ///< how the option was set
70 bool option; ///< specified yes/no value; meaningless if optUnspecified
71 };
72
73 #endif /* SQUID_YESNONONE_H_ */
74