Options.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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_ACL_OPTIONS_H
10 #define SQUID_ACL_OPTIONS_H
11 
12 #include "acl/forward.h"
13 #include "sbuf/forward.h"
14 
15 #include <iosfwd>
16 #include <map>
17 #include <set>
18 
19 // After all same-name acl configuration lines are merged into one ACL:
20 // configuration = acl name type [option...] [[flag...] parameter...]
21 // option = -x[=value] | --name[=value]
22 // flag = option
23 //
24 // Options and flags use the same syntax, but differ in scope and handling code:
25 // * ACL options appear before all parameters and apply to all parameters.
26 // They are handled by ACL kids (or equivalent).
27 // * Parameter flags may appear after some other parameters and apply only to
28 // the subsequent parameters (until they are overwritten by later flags).
29 // They are handled by ACLData kids.
30 // ACL options parsing code skips and leaves leading parameter flags (if any)
31 // for ACLData code to process.
32 
33 namespace Acl {
34 
35 typedef const char *OptionName;
36 
39 class Option
40 {
41 public:
43  explicit Option(ValueExpectation vex = valueNone): valueExpectation(vex) {}
44  virtual ~Option() {}
45 
48  virtual bool configured() const = 0;
49 
51  virtual void configureDefault() const = 0;
52 
54  virtual void configureWith(const SBuf &rawValue) const = 0;
55 
56  virtual bool valued() const = 0;
57 
59  virtual void print(std::ostream &os) const = 0;
60 
61  ValueExpectation valueExpectation = valueNone;
62 };
63 
65 template <class Value>
67 {
68 public:
69  typedef Value value_type;
70 
71  OptionValue(): value {} {}
72  explicit OptionValue(const Value &aValue): value(aValue) {}
73 
74  explicit operator bool() const { return configured; }
75 
76  Value value;
77  bool configured = false;
78  bool valued = false;
79 };
80 
82 template <class Recipient>
83 class TypedOption: public Option
84 {
85 public:
86  //typedef typename Recipient::value_type value_type;
87  explicit TypedOption(ValueExpectation vex = valueNone): Option(vex) {}
88 
90  void linkWith(Recipient *recipient) const
91  {
92  assert(recipient);
93  recipient_ = recipient;
94  }
95 
96  /* Option API */
97 
98  virtual bool configured() const override { return recipient_ && recipient_->configured; }
99  virtual bool valued() const override { return recipient_ && recipient_->valued; }
100 
102  virtual void configureDefault() const override
103  {
104  assert(recipient_);
105  recipient_->configured = true;
106  recipient_->valued = false;
107  // sets recipient_->value to default
108  setDefault();
109  }
110 
112  virtual void configureWith(const SBuf &rawValue) const override
113  {
114  assert(recipient_);
115  recipient_->configured = true;
116  recipient_->valued = true;
117  import(rawValue);
118  }
119 
120  virtual void print(std::ostream &os) const override { if (valued()) os << recipient_->value; }
121 
122 private:
123  void import(const SBuf &rawValue) const { recipient_->value = rawValue; }
124  void setDefault() const { /*leave recipient_->value as is*/}
125 
126  // The "mutable" specifier demarcates set-once Option kind/behavior from the
127  // ever-changing recipient of the actual admin-configured option value.
128  mutable Recipient *recipient_ = nullptr;
129 };
130 
131 /* two typical option kinds: --foo and --bar=text */
136 
137 // this specialization should never be called until we start supporting
138 // boolean option values like --name=enable or --name=false
139 template <>
140 inline void
141 BooleanOption::import(const SBuf &) const
142 {
143  assert(!"boolean options do not have ...=values (for now)");
144 }
145 
146 template <>
147 inline void
149 {
150  recipient_->value = true;
151 }
152 
155 public:
156  bool operator()(const OptionName a, const OptionName b) const;
157 };
159 typedef std::map<OptionName, const Option*, OptionNameCmp> Options;
160 
162 typedef std::set<OptionName, OptionNameCmp> ParameterFlags;
163 
167 void ParseFlags(const Options &options, const ParameterFlags &flags);
168 
169 /* handy for Class::options() and Class::supportedFlags() defaults */
170 const Options &NoOptions();
171 const ParameterFlags &NoFlags();
172 
173 } // namespace Acl
174 
175 std::ostream &operator <<(std::ostream &os, const Acl::Option &option);
176 std::ostream &operator <<(std::ostream &os, const Acl::Options &options);
177 
178 #endif /* SQUID_ACL_OPTIONS_H */
179 
Value value
final value storage, possibly after conversions
Definition: Options.h:76
#define assert(EX)
Definition: assert.h:17
TypedOption< BooleanOptionValue > BooleanOption
Definition: Options.h:134
void setDefault() const
Definition: Options.h:124
ValueExpectation valueExpectation
expect "=value" part?
Definition: Options.h:61
Definition: SBuf.h:86
OptionValue(const Value &aValue)
Definition: Options.h:72
OptionValue< bool > BooleanOptionValue
Definition: Options.h:132
OptionValue< SBuf > TextOptionValue
Definition: Options.h:133
virtual bool configured() const =0
virtual ~Option()
Definition: Options.h:44
virtual void configureWith(const SBuf &rawValue) const =0
called after parsing -x=value or –name=value
virtual bool valued() const override
Definition: Options.h:99
virtual bool valued() const =0
const ParameterFlags & NoFlags()
Definition: Options.cc:254
virtual bool configured() const override
Definition: Options.h:98
Definition: Acl.cc:32
Value value_type
Definition: Options.h:69
virtual void configureWith(const SBuf &rawValue) const override
sets the option value from rawValue
Definition: Options.h:112
void ParseFlags(const Options &options, const ParameterFlags &flags)
Definition: Options.cc:240
virtual void print(std::ostream &os) const =0
prints a configuration snippet (as an admin could have typed)
ValueExpectation
Definition: Options.h:42
virtual void print(std::ostream &os) const override
prints a configuration snippet (as an admin could have typed)
Definition: Options.h:120
std::set< OptionName, OptionNameCmp > ParameterFlags
a set of parameter flag names
Definition: Options.h:162
void import(const SBuf &rawValue) const
Definition: Options.h:123
std::ostream & operator<<(std::ostream &os, const Acl::Option &option)
Definition: Options.cc:261
virtual void configureDefault() const =0
called after parsing -x or –name
option name comparison functor
Definition: Options.h:154
std::map< OptionName, const Option *, OptionNameCmp > Options
name:option map
Definition: Options.h:159
void linkWith(Recipient *recipient) const
who to tell when this option is enabled
Definition: Options.h:90
TypedOption(ValueExpectation vex=valueNone)
Definition: Options.h:87
int a
Definition: membanger.c:50
Option(ValueExpectation vex=valueNone)
Definition: Options.h:43
a type-specific Option (e.g., a boolean –toggle or -m=SBuf)
Definition: Options.h:83
virtual void configureDefault() const override
sets the default value when option is used without a value
Definition: Options.h:102
Stores configuration of a typical boolean flag or a single-value Option.
Definition: Options.h:66
const Options & NoOptions()
Definition: Options.cc:247
const char * OptionName
Definition: Options.h:35
TypedOption< TextOptionValue > TextOption
Definition: Options.h:135

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors