Vcsn  2.5.dev
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  {
21  class config
22  {
23  public:
24  using Node = YAML::Node;
25 
28  {
29  public:
30  class BadNode {};
31  config_value(Node n);
32  config_value(const config&);
33  config_value(const config_value& other);
34 
38 
39 
40  template <typename T>
41  T as() const
42  {
43  if (!node_.IsScalar())
44  throw BadNode{};
45  return node_.as<T>();
46  }
47 
49  std::string str() const;
50 
51  config_value operator[](const std::string& key) const;
52  std::vector<std::string> keys() const;
53 
55  bool is_valid(const std::string& key) const;
57  void remove(const std::string& key);
58 
59  using iterator = Node::iterator;
60  iterator begin();
61  iterator end();
62 
64  void merge(const config_value& from);
65 
66  std::ostream& print(std::ostream& out) const;
67  friend void swap(config_value& first, config_value& second);
68 
69  private:
70  std::unique_ptr<std::vector<std::string>> gen_keys() const;
71 
72  mutable std::unique_ptr<const std::vector<std::string>> keys_;
74  };
75 
77  config();
78 
80  config_value operator[](const std::string& key);
81 
82  private:
84  };
85 
86  inline
87  std::ostream& operator<<(std::ostream& out, const config::config_value& v)
88  {
89  return v.print(out);
90  }
91  }
92 
94  inline
96  {
97  static auto conf = detail::config{};
98  return conf;
99  }
100 
104  std::string configuration(const std::string& key);
105 }
The class returned by [] operators.
std::ostream & operator<<(std::ostream &out, const config::config_value &v)
friend void swap(config_value &first, config_value &second)
std::vector< std::string > keys() const
std::ostream & print(std::ostream &out) const
std::string configuration(const std::string &key)
Main interface to the configuration function key it the &#39;path&#39; to the desired key using periofs as a ...
std::unique_ptr< std::vector< std::string > > gen_keys() const
void merge(const config_value &from)
Merge a value into another one - and modify the first.
detail::config & get_config()
Get the configuration singleton.
config_value & operator=(config_value rhs)
Assign a new value to this key.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:86
#define LIBVCSN_API
Definition: export.hh:8
std::string str() const
Get the node value as a string.
config_value operator[](const std::string &key) const
Definition: a-star.hh:8
std::unique_ptr< const std::vector< std::string > > keys_
The YAML configuration (acts as the top of the configuration tree).
config()
Load all the configuration files.
bool is_valid(const std::string &key) const
Check that this node refers to a key that exists in the tree.