Vcsn  2.3
Be Rational
getargs.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 
5 #include <boost/range/adaptor/map.hpp>
6 #include <boost/range/algorithm/copy.hpp>
7 #include <boost/variant.hpp>
8 
10 #include <vcsn/misc/export.hh>
11 #include <vcsn/misc/map.hh>
12 #include <vcsn/misc/raise.hh>
13 
14 namespace vcsn LIBVCSN_API
15 {
16  namespace detail
17  {
18  template <typename C>
19  ATTRIBUTE_NORETURN
20  void invalid_argument(const std::string& kind, const std::string& key,
21  const C& map)
22  {
23  std::ostringstream o;
24  // Retrieve all keys
25  boost::copy(map | boost::adaptors::map_keys,
26  std::ostream_iterator<std::string>(o, " "));
27  raise("invalid ", kind, ": ", key, ", expected: ", o.str());
28  }
29  }
30 
32  template <typename Value>
33  struct getarg
34  {
35  // FIXME: Boost::ProgramOptions might be a better bet.
36  using value_t = Value;
37  using data_t = boost::variant<std::string, Value>;
38  using map_t = std::map<std::string, data_t>;
39 
40  getarg(const std::string& kind,
41  std::initializer_list<typename map_t::value_type> args)
42  : kind_{kind}
43  , map_{args}
44  {}
45 
47  const value_t& operator[](std::string s) const
48  {
49  while (true)
50  {
51  auto i = map_.find(s);
52  if (i == end(map_))
54  else if (auto* res = boost::get<value_t>(&i->second))
55  return *res;
56  else
57  s = boost::get<std::string>(i->second);
58  }
59  }
60 
62  std::string kind_;
65  };
66 }
const value_t & operator[](std::string s) const
The value associated to s.
Definition: getargs.hh:47
std::map< std::string, data_t > map_t
Definition: getargs.hh:38
ATTRIBUTE_NORETURN void invalid_argument(const std::string &kind, const std::string &key, const C &map)
Definition: getargs.hh:20
return res
Definition: multiply.hh:398
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
Definition: copy.hh:322
map_t map_
string -> (string | value).
Definition: getargs.hh:64
Definition: a-star.hh:8
Value value_t
Definition: getargs.hh:36
#define LIBVCSN_API
Definition: export.hh:8
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
Definition: tuple.hh:177
std::string kind_
The nature of the selection. Used in error messages.
Definition: getargs.hh:62
boost::variant< std::string, Value > data_t
Definition: getargs.hh:37
getarg(const std::string &kind, std::initializer_list< typename map_t::value_type > args)
Definition: getargs.hh:40
A mapping from strings to Values.
Definition: getargs.hh:33