monotone

monotone Mtn Source Tree

Root/options.hh

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#ifndef __OPTIONS_HH__
11#define __OPTIONS_HH__
12
13/*
14 * This defines 'struct options', which includes the variables and options
15 * defined in options_list.hh as members. Options and optsets are available
16 * statically as options::opts::<name>, and option variables are available
17 * as options::<name>.
18 */
19
20#include <algorithm>
21#include <list>
22#include <climits>
23
24#include "option.hh"
25#include "paths.hh"
26#include "dates.hh"
27
28#include "lexical_cast.hh"
29
30template<long low>
31class restricted_long
32{
33 long value;
34public:
35 restricted_long()
36 {
37 if (-1 < low)
38 value = -1;
39 else
40 value = low - 1;
41 }
42 restricted_long(std::string const & x, origin::type o)
43 {
44 value = boost::lexical_cast<long>(x);
45 if (value < low)
46 throw option::bad_arg_internal((F("must not be less than %d") % low).str());
47 }
48 operator long() const { return value; }
49};
50
51class enum_string
52{
53 std::vector<std::string> allowed;
54 std::string allowed_str;
55 std::string value;
56public:
57 enum_string() { }
58 enum_string(std::string a) : allowed_str(a)
59 {
60 size_t x = a.find(",");
61 while (x != std::string::npos)
62 {
63 allowed.push_back(a.substr(0, x));
64 a.erase(0, x + 1);
65 x = a.find(",");
66 }
67 allowed.push_back(a);
68 I(allowed.size() >= 2);
69 value = idx(allowed, 0);
70 }
71 void set(std::string const & v)
72 {
73 if (std::find(allowed.begin(), allowed.end(), v) == allowed.end())
74 {
75 throw option::bad_arg_internal((F("must be one of the following: %s")
76 % allowed_str).str());
77 }
78 else
79 value = v;
80 }
81 void unchecked_set(std::string const & v) { value = v; }
82 operator std::string() const { return value; }
83 bool operator<(enum_string const & e) const { return value < e.value; }
84 bool operator==(std::string const & s) const { return value == s; }
85 bool empty() const { return value.empty(); }
86};
87class enum_string_set
88{
89 std::string allowed;
90 std::set<enum_string> value;
91public:
92 enum_string_set() { }
93 enum_string_set(std::string const & a) : allowed(a) { }
94 void add(std::string const & v)
95 {
96 enum_string e(allowed);
97 e.set(v);
98 value.insert(e);
99 }
100 operator std::set<enum_string>() const { return value; }
101 std::set<enum_string>::const_iterator find(std::string const & s) const
102 {
103 enum_string e(allowed);
104 e.set(s);
105 return value.find(e);
106 }
107 std::set<enum_string>::const_iterator end() const
108 {
109 return value.end();
110 }
111};
112
113struct options
114{
115 options();
116 const options & operator = (options const & other);
117
118 typedef boost::function<void()> reset_function;
119 typedef option::option<options> option_type;
120 typedef option::option_set<options> options_type;
121 typedef options_type const & (*static_options_fun)();
122
123 static std::map<static_options_fun, std::set<static_options_fun> > &children();
124 static std::map<static_options_fun, std::list<void(options::*)()> > &var_membership();
125 static std::map<static_options_fun, bool> &hidden();
126 static std::map<static_options_fun, char const *> &deprecated();
127
128 void reset_optset(static_options_fun opt);
129
130 struct opts
131 {
132 static options_type const & none ();
133 static options_type const & all_options ();
134# define OPTSET(name) \
135 static options_type const & name ();
136
137# define OPTVAR(optset, type, name, default_)
138
139#define OPTION(optset, name, hasarg, optstring, description) \
140 static options_type const & name ## _opt ();
141
142# define OPTSET_REL(parent, child)
143# define HIDE(option)
144# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
145
146# include "options_list.hh"
147
148# undef OPTSET
149# undef OPTVAR
150# undef OPTION
151# undef OPTSET_REL
152# undef HIDE
153# undef DEPRECATE
154 };
155
156# define OPTSET(name) \
157 private: \
158 void reset_optset_ ## name ();
159
160# define OPTVAR(optset, type, name, default_) \
161 public: \
162 type name; \
163 void reset_ ## name ();
164
165#define OPTION(optset, name, hasarg, optstring, description) \
166 public: \
167 bool name ## _given; \
168private: \
169 void set_ ## name (std::string arg); \
170 void real_set_ ## name (std::string arg); \
171 void reset_opt_ ## name ();
172
173# define OPTSET_REL(parent, child)
174# define HIDE(option)
175# define DEPRECATE(option, reason, deprecated_in, will_remove_in)
176
177# include "options_list.hh"
178
179# undef OPTSET
180# undef OPTVAR
181# undef OPTION
182# undef OPTSET_REL
183# undef HIDE
184# undef DEPRECATE
185};
186
187option::option_set<options>
188operator | (option::option_set<options> const & opts,
189 option::option_set<options> const & (*fun)());
190
191#endif
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