monotone

monotone Mtn Source Tree

Root/options.cc

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