monotone

monotone Mtn Source Tree

Root/options.cc

1
2#include "base.hh"
3#include <algorithm>
4
5#include "charset.hh"
6#include "options.hh"
7#include "platform.hh"
8#include "sanity.hh"
9#include "ui.hh"
10
11using std::list;
12using std::map;
13using std::set;
14using std::string;
15
16using option::bad_arg_internal;
17
18template<typename T>
19bool has_arg() { return true; }
20template<>
21bool has_arg<bool>() { return false; }
22
23
24
25std::map<options::static_options_fun, std::set<options::static_options_fun> > &
26options::children()
27{
28 static map<static_options_fun, set<static_options_fun> > val;
29 static bool first(true);
30 if (first)
31 {
32# define OPTSET(name) \
33 val[&options::opts::all_options].insert(&options::opts::name);
34# define OPTVAR(optset, type, name, default_)
35# define OPTION(optset, name, hasarg, optstring, description) \
36 val[&options::opts:: optset].insert(&options::opts:: name); \
37 val[&options::opts::all_options].insert(&options::opts::name);
38# define OPTSET_REL(parent, child) \
39 val[&options::opts:: parent].insert(&options::opts:: child);
40
41# include "options_list.hh"
42
43# undef OPTSET
44# undef OPTVAR
45# undef OPTION
46# undef OPTSET_REL
47
48 first = false;
49 }
50 return val;
51}
52
53std::map<options::static_options_fun, std::list<void(options::*)()> > &
54options::var_membership()
55{
56 static map<static_options_fun, std::list<void(options::*)()> > val;
57 static bool first(true);
58 if (first)
59 {
60# define OPTSET(name)
61# define OPTVAR(optset, type, name, default_) \
62 val[&opts:: optset ].push_back(&options::reset_ ## name );
63# define OPTION(optset, name, hasarg, optstring, description)
64# define OPTSET_REL(parent, child)
65
66# include "options_list.hh"
67
68# undef OPTSET
69# undef OPTVAR
70# undef OPTION
71# undef OPTSET_REL
72
73 first = false;
74 }
75 return val;
76}
77
78
79options::options()
80{
81# define OPTSET(name)
82# define OPTVAR(group, type, name, default_)\
83 name = type ( default_ );
84# define OPTION(optset, name, hasarg, optstring, description)\
85 name ## _given = false;
86# define OPTSET_REL(parent, child)
87
88# include "options_list.hh"
89
90# undef OPTSET
91# undef OPTVAR
92# undef OPTION
93# undef OPTSET_REL
94}
95
96static options::options_type
97collect_children(options::static_options_fun opt)
98{
99 options::options_type out;
100 set<options::static_options_fun> const & ch = options::children()[opt];
101 for (set<options::static_options_fun>::const_iterator i = ch.begin();
102 i != ch.end(); ++i)
103 {
104 if (*i != opt)
105out = out | (*(*i))();
106 }
107 return out;
108}
109
110void options::reset_optset(options::static_options_fun opt)
111{
112 list<void(options::*)()> const & vars = var_membership()[opt];
113 for (list<void(options::*)()>::const_iterator i = vars.begin();
114 i != vars.end(); ++i)
115 {
116 (this->*(*i))();
117 }
118}
119
120options::options_type const & options::opts::none()
121{
122 static options::options_type val;
123 return val;
124}
125
126options::options_type const & options::opts::all_options()
127{
128 static options::options_type val = collect_children(&options::opts::all_options);
129 return val;
130}
131
132# define OPTSET(name) \
133 options::options_type const & options::opts::name()\
134 {\
135 static options::options_type val =\
136 collect_children(&options::opts::name)\
137 | options::option_type("", #name, false, 0, \
138 &options::reset_optset_ ## name );\
139 return val;\
140 }\
141 void options::reset_optset_ ## name ()\
142 {\
143 reset_optset(&opts:: name);\
144 }
145
146# define OPTVAR(optset, type, name, default_) \
147 void options::reset_ ## name () \
148 { \
149 name = type ( default_ ); \
150 }
151
152# define OPTION(optset, name, hasarg, optstring, description)\
153 options::options_type const & options::opts::name()\
154 {\
155 localize_monotone(); \
156 \
157 static options::options_type val(optstring,\
158 gettext(description), hasarg,\
159 &options::set_ ## name ,\
160 &options::reset_opt_ ## name );\
161 return val;\
162 }\
163 void options::reset_opt_ ## name ()\
164 {\
165 name ## _given = false;\
166 reset_optset(&opts:: name);\
167 }\
168 void options::set_ ## name (std::string arg) \
169 {\
170 name ## _given = true;\
171 real_set_ ## name (arg);\
172 }\
173 void options::real_set_ ## name (std::string arg)
174
175# define OPTSET_REL(parent, child)
176
177#define option_bodies
178# include "options_list.hh"
179#undef option_bodies
180
181# undef OPTSET
182# undef OPTVAR
183# undef OPTION
184# undef OPTSET_REL
185
186
187option::option_set<options>
188operator | (option::option_set<options> const & opts,
189 option::option_set<options> const & (*fun)())
190{
191 return opts | fun();
192}
193
194// Local Variables:
195// mode: C++
196// fill-column: 76
197// c-file-style: "gnu"
198// indent-tabs-mode: nil
199// End:
200// 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