monotone

monotone Mtn Source Tree

Root/src/options.cc

1// Copyright (C) 2006 Timothy Brownawell <tbrownaw@gmail.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include <algorithm>
12
13#include "charset.hh"
14#include "constants.hh"
15#include "options.hh"
16#include "platform.hh"
17#include "sanity.hh"
18#include "ui.hh"
19
20using std::list;
21using std::map;
22using std::set;
23using std::string;
24
25using option::bad_arg_internal;
26
27template<typename T>
28bool has_arg() { return true; }
29template<>
30bool has_arg<bool>() { return false; }
31
32
33
34std::map<options::static_options_fun, std::set<options::static_options_fun> > &
35options::children()
36{
37 static map<static_options_fun, set<static_options_fun> > val;
38 static bool first(true);
39 if (first)
40 {
41# define OPTSET(name) \
42 val[&options::opts::all_options].insert(&options::opts::name);
43# define OPTVAR(optset, type, name, default_)
44# define OPTION(optset, name, hasarg, optstring, description) \
45 val[&options::opts:: optset].insert(&options::opts:: name ## _opt); \
46 val[&options::opts::all_options].insert(&options::opts::name ## _opt);
47# define OPTSET_REL(parent, child) \
48 val[&options::opts:: parent].insert(&options::opts:: child);
49# define HIDE(option)
50# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
51
52# include "options_list.hh"
53
54# undef OPTSET
55# undef OPTVAR
56# undef OPTION
57# undef OPTSET_REL
58# undef HIDE
59# undef DEPRECATE
60
61 first = false;
62 }
63 return val;
64}
65
66std::map<options::static_options_fun, std::list<void(options::*)()> > &
67options::var_membership()
68{
69 static map<static_options_fun, std::list<void(options::*)()> > val;
70 static bool first(true);
71 if (first)
72 {
73# define OPTSET(name)
74# define OPTVAR(optset, type, name, default_) \
75 val[&opts:: optset ].push_back(&options::reset_ ## name );
76# define OPTION(optset, name, hasarg, optstring, description)
77# define OPTSET_REL(parent, child)
78# define HIDE(option)
79# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
80
81# include "options_list.hh"
82
83# undef OPTSET
84# undef OPTVAR
85# undef OPTION
86# undef OPTSET_REL
87# undef HIDE
88# undef DEPRECATE
89
90 first = false;
91 }
92 return val;
93}
94
95std::map<options::static_options_fun, bool> &
96options::hidden()
97{
98 static map<static_options_fun, bool> val;
99 static bool first(true);
100 if (first)
101 {
102# define OPTSET(name)
103# define OPTVAR(optset, type, name, default_)
104# define OPTION(optset, name, hasarg, optstring, description)
105# define OPTSET_REL(parent, child)
106# define HIDE(option) \
107 val[&opts:: option ## _opt ] = true;
108# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
109
110# include "options_list.hh"
111
112# undef OPTSET
113# undef OPTVAR
114# undef OPTION
115# undef OPTSET_REL
116# undef HIDE
117# undef DEPRECATE
118 first = false;
119 }
120 return val;
121}
122
123std::map<options::static_options_fun, char const *> &
124options::deprecated()
125{
126 static map<static_options_fun, char const *> val;
127 static bool first(true);
128 if (first)
129 {
130# define OPTSET(name)
131# define OPTVAR(optset, type, name, default_)
132# define OPTION(optset, name, hasarg, optstring, description)
133# define OPTSET_REL(parent, child)
134# define HIDE(option)
135# define DEPRECATE(option, reason, deprecated_in, will_remove_in) \
136 val[&opts:: option ## _opt ] = reason ;
137
138# include "options_list.hh"
139
140# undef OPTSET
141# undef OPTVAR
142# undef OPTION
143# undef OPTSET_REL
144# undef HIDE
145# undef DEPRECATE
146 first = false;
147 }
148 return val;
149}
150
151
152options::options()
153{
154# define OPTSET(name)
155# define OPTVAR(group, type, name, default_) \
156 name = type ( default_ );
157# define OPTION(optset, name, hasarg, optstring, description) \
158 name ## _given = false;
159# define OPTSET_REL(parent, child)
160# define HIDE(option)
161# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
162
163# include "options_list.hh"
164
165# undef OPTSET
166# undef OPTVAR
167# undef OPTION
168# undef OPTSET_REL
169# undef HIDE
170# undef DEPRECATE
171}
172
173const options &
174options::operator = (options const & other)
175{
176# define OPTSET(name)
177# define OPTVAR(group, type, name, default_) \
178 name = other.name;
179# define OPTION(optset, name, hasarg, optstring, description) \
180 name ## _given = other.name ## _given;
181# define OPTSET_REL(parent, child)
182# define HIDE(option)
183# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
184
185# include "options_list.hh"
186
187# undef OPTSET
188# undef OPTVAR
189# undef OPTION
190# undef OPTSET_REL
191# undef HIDE
192# undef DEPRECATE
193
194 return *this;
195}
196
197static options::options_type
198collect_children(options::static_options_fun opt)
199{
200 options::options_type out;
201 set<options::static_options_fun> const & ch = options::children()[opt];
202 for (set<options::static_options_fun>::const_iterator i = ch.begin();
203 i != ch.end(); ++i)
204 {
205 if (*i != opt)
206 out = out | (*(*i))();
207 }
208 return out;
209}
210
211void options::reset_optset(options::static_options_fun opt)
212{
213 list<void(options::*)()> const & vars = var_membership()[opt];
214 for (list<void(options::*)()>::const_iterator i = vars.begin();
215 i != vars.end(); ++i)
216 {
217 (this->*(*i))();
218 }
219}
220
221options::options_type const & options::opts::none()
222{
223 static options::options_type val;
224 return val;
225}
226
227options::options_type const & options::opts::all_options()
228{
229 static options::options_type val = collect_children(&options::opts::all_options);
230 return val;
231}
232
233# define OPTSET(name) \
234 options::options_type const & options::opts::name() \
235 { \
236 static options::options_type val = \
237 collect_children(&options::opts::name) \
238 | options::option_type("", #name, false, 0, \
239 &options::reset_optset_ ## name , \
240 true, 0); \
241 return val; \
242 } \
243 void options::reset_optset_ ## name () \
244 { \
245 reset_optset(&opts:: name); \
246 }
247
248# define OPTVAR(optset, type, name, default_) \
249 void options::reset_ ## name () \
250 { \
251 name = type ( default_ ); \
252 }
253
254# define OPTION(optset, name, hasarg, optstring, description) \
255 options::options_type const & options::opts::name ## _opt() \
256 { \
257 static options::options_type val(optstring, \
258 description, hasarg, \
259 &options::set_ ## name , \
260 &options::reset_opt_ ## name , \
261 hidden()[&opts:: name ## _opt], \
262 deprecated()[&opts:: name ## _opt]); \
263 return val; \
264 } \
265 void options::reset_opt_ ## name () \
266 { \
267 name ## _given = false; \
268 reset_optset(&opts:: optset); \
269 } \
270 void options::set_ ## name (std::string arg) \
271 { \
272 name ## _given = true; \
273 real_set_ ## name (arg); \
274 } \
275 void options::real_set_ ## name (std::string arg)
276
277# define OPTSET_REL(parent, child)
278# define HIDE(option)
279# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
280
281#define option_bodies
282# include "options_list.hh"
283#undef option_bodies
284
285# undef OPTSET
286# undef OPTVAR
287# undef OPTION
288# undef OPTSET_REL
289# undef HIDE
290# undef DEPRECATE
291
292option::option_set<options>
293operator | (option::option_set<options> const & opts,
294 option::option_set<options> const & (*fun)())
295{
296 return opts | fun();
297}
298
299// Local Variables:
300// mode: C++
301// fill-column: 76
302// c-file-style: "gnu"
303// indent-tabs-mode: nil
304// End:
305// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status