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