Vcsn  2.8
Be Rational
configuration.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <cstdlib>
5 #include <iostream>
6 #include <memory>
7 
8 #include <boost/any.hpp>
9 
10 #include <yaml-cpp/yaml.h>
11 
12 #include <vcsn/misc/export.hh>
13 #include <vcsn/misc/raise.hh>
14 
15 namespace vcsn LIBVCSN_API
16 {
17  namespace detail
18  {
22  class config
23  {
24  public:
25  using Node = YAML::Node;
26 
28  class value
29  {
30  public:
31  class BadNode {};
32  value(Node n);
33  value(const config&);
34  value(const value& other);
35 
37  value&
38  operator=(value rhs);
39 
40 
41  template <typename T>
42  T as() const
43  {
44  if (!node_.IsScalar())
45  throw BadNode{};
46  return node_.as<T>();
47  }
48 
50  std::string str() const;
51 
52  value operator[](const std::string& key) const;
53  std::vector<std::string> keys() const;
54 
56  bool is_valid(const std::string& key) const;
58  void remove(const std::string& key);
59 
60  using iterator = Node::iterator;
61  iterator begin();
62  iterator end();
63 
65  void merge(const value& from);
66 
67  std::ostream& print(std::ostream& out) const;
68  friend void swap(value& first, value& second);
69 
70  private:
71  std::unique_ptr<std::vector<std::string>> gen_keys() const;
72 
73  mutable std::unique_ptr<const std::vector<std::string>> keys_;
75  };
76 
78  config();
79 
81  value operator[](const std::string& key);
82 
83  private:
85  };
86 
87  inline
88  std::ostream& operator<<(std::ostream& out, const config::value& v)
89  {
90  return v.print(out);
91  }
92  }
93 
95  inline
97  {
98  static auto conf = detail::config{};
99  return conf;
100  }
101 
104  std::string configuration(const std::string& key);
105 }
bool is_valid(const std::string &key) const
Check that this node refers to a key that exists in the tree.
#define LIBVCSN_API
Definition: export.hh:8
config()
Load all the configuration files.
std::ostream & operator<<(std::ostream &out, const config::value &v)
The class returned by [] operators.
friend void swap(value &first, value &second)
std::unique_ptr< std::vector< std::string > > gen_keys() const
A YAML configuration.
std::string str() const
Get the node value as a string.
std::string configuration(const std::string &key)
Get the string mapped by key (e.g., "configuration.version", "dot.styles").
Definition: a-star.hh:8
value & operator=(value rhs)
Assign a new value to this key.
std::vector< std::string > keys() const
detail::config & get_config()
Get the configuration singleton.
value operator[](const std::string &key) const
std::unique_ptr< const std::vector< std::string > > keys_
std::ostream & print(std::ostream &out) const
void merge(const value &from)
Merge a value into another one - and modify the first.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:86