monotone

monotone Mtn Source Tree

Root/options.cc

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