StateFS
 All Classes Files Functions Variables Typedefs Enumerations Groups Pages
config.hpp
1 #ifndef _STATEFS_CONFIG_HPP_
2 #define _STATEFS_CONFIG_HPP_
3 
4 #include <cor/sexp.hpp>
5 #include <cor/notlisp.hpp>
6 
7 #include <boost/variant.hpp>
8 #include <fstream>
9 
10 namespace config
11 {
12 
13 static inline std::string cfg_extension()
14 {
15  return ".conf";
16 }
17 
18 static inline std::string cfg_provider_prefix()
19 {
20  return "provider";
21 }
22 
23 static inline std::string cfg_loader_prefix()
24 {
25  return "loader";
26 }
27 
28 namespace nl = cor::notlisp;
29 
30 typedef boost::variant<long, unsigned long, double, std::string> property_type;
31 typedef std::unordered_map<std::string, property_type> property_map_type;
32 
33 void to_property(nl::expr_ptr expr, property_type &dst);
34 std::string to_string(property_type const &p);
35 std::string to_nl_string(property_type const &p);
36 
37 long to_integer(property_type const &src);
38 
39 class Property : public nl::ObjectExpr
40 {
41 public:
42  enum Access
43  {
44  Read = 1,
45  Write = 2,
46  Subscribe = 4
47  };
48 
49  Property(std::string const &name,
50  property_type const &defval,
51  unsigned access = Read);
52 
53  std::string defval() const;
54 
55  unsigned access() const
56  {
57  return access_;
58  }
59 
60  int mode(int umask = 0027) const;
61 
62 private:
63  property_type defval_;
64  unsigned access_;
65 };
66 
67 class Namespace : public nl::ObjectExpr
68 {
69 public:
70  typedef std::shared_ptr<Property> prop_type;
71  typedef std::list<prop_type> storage_type;
72  Namespace(std::string const &name, storage_type &&props);
73 
74  storage_type props_;
75 };
76 
77 class Library : public nl::ObjectExpr
78 {
79 public:
80  Library(std::string const &name, std::string const &path);
81 
82  std::string path;
83 };
84 
85 class Plugin : public Library
86 {
87 public:
88  typedef std::shared_ptr<Namespace> ns_type;
89  typedef std::list<ns_type> storage_type;
90  Plugin(std::string const &name
91  , std::string const &path
92  , property_map_type &&info
93  , storage_type &&namespaces);
94 
95  std::time_t mtime_;
96  property_map_type info_;
97  storage_type namespaces_;
98 };
99 
100 class Loader : public Library
101 {
102 public:
103  Loader(std::string const &name, std::string const &path);
104 };
105 
106 nl::env_ptr mk_parse_env();
107 
108 template <typename CharT, typename ReceiverT>
109 void parse(std::basic_istream<CharT> &input, ReceiverT receiver)
110 {
111  using namespace nl;
112 
113  env_ptr env(mk_parse_env());
114 
115  Interpreter config(env);
116  cor::error_tracer([&input, &config]()
117  { cor::sexp::parse(input, config); });
118 
119  ListAccessor res(config.results());
120  rest_casted<Library>(res, receiver);
121 }
122 
123 typedef std::function<void (std::string const &
124  , std::shared_ptr<Library>) > config_receiver_fn;
125 
126 bool from_file(std::string const &, config_receiver_fn);
127 
129 {
130 public:
131  virtual void provider_add(std::shared_ptr<Plugin>) =0;
132  virtual void provider_rm(std::shared_ptr<Plugin>) =0;
133  virtual void loader_add(std::shared_ptr<Loader>) =0;
134  virtual void loader_rm(std::shared_ptr<Loader>) =0;
135 };
136 
137 
138 }
139 
140 
141 #endif // _STATEFS_CONFIG_HPP_