LibPkg
env_checker.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 // Copyright � 2003-2005 Graham Shaw.
3 // Distribution and use are subject to the GNU Lesser General Public License,
4 // a copy of which may be found in the file !LibPkg.Copyright.
5 // Created on: 3 Jul 2018
6 // Author: Alan Buckley
7 
8 #ifndef LIBPKG_LIBPKG_ENV_CHECKER_H_
9 #define LIBPKG_LIBPKG_ENV_CHECKER_H_
10 
11 #include <string>
12 #include <map>
13 #include <vector>
14 #include <set>
15 
16 namespace pkg {
17 
18 class env_checker;
19 
22 {
27 };
28 
29 
31 class env_check
32 {
33 public:
34 
35  env_check(const std::string &name, const std::string &desc, const std::string &id, env_check_type type, int install_priority);
36  virtual ~env_check() {}
38  const std::string &name() const {return _name;}
40  const std::string &description() const {return _description;}
42  const std::string &id() const {return _id;}
44  bool detected() const {return _detected;}
46  bool available() const {return _available;}
47  void available(bool override) {_available = override;}
49  env_check_type type() const {return _type;}
52 private:
53  std::string _name;
54  std::string _id;
55  env_check_type _type;
56 protected:
57  std::string _description;
58  bool _detected;
59  bool _available;
61 };
62 
64 class unset_check : public env_check
65 {
66 public:
67  unset_check() : env_check("unset","Environment not set on the package", "u", Unset, 1)
68  {
69  _detected = _available = true;
70  }
71 };
72 
76 class unknown_check : public env_check
77 {
78 public:
79  unknown_check(const std::string &name, const std::string &id) : env_check(name,"Unknown value, upgrade the package client",id, Unknown, 1)
80  {
81  _detected = _available = true;
82  }
83 };
84 
85 
91 class pkg_env
92 {
93 public:
95  const std::string &name() const {return _name;}
97  bool available() const {return _available;}
98 
102  const std::string &id() const {return _id;}
103 
105  env_check_type type() const {return _type;}
106 
108  void reset_available();
109 
110  std::string env_names() const;
111  std::string module_names() const;
112 
113 
114 private:
115  friend env_checker;
116  pkg_env(const std::string &name, const std::vector<env_check *> &checks);
117  ~pkg_env() {};
118 
119  std::string _name;
120  std::string _id;
121  std::vector<env_check *> _checks;
122  bool _available;
123  int _install_priority;
124  env_check_type _type;
125 };
126 
131 {
132 public:
133  env_checker_ptr(const std::string &module_map_path);
134  env_checker_ptr(const env_checker_ptr &other);
135  ~env_checker_ptr();
136 
137  env_checker_ptr &operator=(const env_checker_ptr &other);
138  env_checker *operator->();
139 };
140 
145 {
146 public:
147  class watcher;
148  friend class watcher;
149  friend class env_checker_ptr;
150 
151 private:
152  static env_checker *_instance;
153  int _ref_count;
154  std::set<env_checker::watcher *> _watchers;
155  static unsigned int _next_module_id;
156  std::map<std::string, env_check *> _checks;
157  std::map<std::string, env_check *> _module_checks;
158  env_check *_unset_check;
159  pkg_env *_unset_env;
160  std::map<std::string, pkg_env *> _environments;
161  std::string _module_map_path;
162  std::map<std::string, std::string> _module_ids;
163 
164 public:
165  static env_checker *instance() {return _instance;}
166  void add_ref() {_ref_count++;}
167  void remove_ref();
168 
169  pkg_env *package_env(const std::string &env_list, const std::string &os_depends);
170 
171  typedef std::map<std::string, env_check *> check_map;
172 
173  const check_map &checks() const {return _checks;}
174  const check_map &module_checks() const {return _module_checks;}
175 
176  std::string get_module_id(const std::string &title);
177 
178  bool clear_environment_overrides();
179  bool override_environment(const std::set<std::string> &new_env, const std::set<std::string> &new_mods);
180 
181 private:
182  env_checker(const std::string &module_map_path);
183  virtual ~env_checker();
184  void initialise(const std::string &module_map_path);
185  void add_check(env_check *check);
186  void read_module_map();
187  void write_module_map();
193  void register_watcher(watcher& w);
194 
199  void deregister_watcher(watcher& w);
203  void notify();
204 };
205 
208 {
209 private:
211  std::set<env_checker*> _env_checkers;
212 public:
214  watcher();
215 
217  virtual ~watcher();
218 
222  void watch(env_checker &e);
223 
227  void unwatch(env_checker & e);
228 
232  virtual void handle_change(env_checker & e)=0;
233 };
234 
235 
236 } /* namespace lippkg */
237 
238 #endif /* LIBPKG_LIBPKG_ENV_CHECKER_H_ */
Class to check environment and convert a string environment specification into a pkg_env There is onl...
Definition: env_checker.h:144
Class for packages where the environment has not been set.
Definition: env_checker.h:64
void add_ref()
Definition: env_checker.h:166
env_check_type
Type for check.
Definition: env_checker.h:21
A class to represent the enviroment a package is designed for.
Definition: env_checker.h:91
int _install_priority
Definition: env_checker.h:60
std::map< std::string, env_check * > check_map
Definition: env_checker.h:171
System check.
Definition: env_checker.h:23
unknown_check(const std::string &name, const std::string &id)
Definition: env_checker.h:79
Module check.
Definition: env_checker.h:24
The namespace used to hold the package management library.
Definition: auto_dir.cc:12
Check unknown to this version of libpkg.
Definition: env_checker.h:25
No check defined on package.
Definition: env_checker.h:26
const std::string & description() const
A one line description for display purposes only.
Definition: env_checker.h:40
env_check_type type() const
Type for this check.
Definition: env_checker.h:49
const std::string & name() const
The full name of this environment.
Definition: env_checker.h:95
int install_priority()
Priority for this check, the higher the more important.
Definition: env_checker.h:51
bool available() const
Software override of status.
Definition: env_checker.h:46
A mixin class to allow an object to watch the environment checker.
Definition: env_checker.h:207
const check_map & module_checks() const
Definition: env_checker.h:174
const std::string & id() const
Short id consisting of a single letter optionally followed be a number.
Definition: env_checker.h:42
bool _detected
Definition: env_checker.h:58
std::string _description
Definition: env_checker.h:57
env_check(const std::string &name, const std::string &desc, const std::string &id, env_check_type type, int install_priority)
Construct a default environment check.
Definition: env_checks.cc:50
virtual ~env_check()
Definition: env_checker.h:36
const std::string & name() const
The name of the check, should be fairly short.
Definition: env_checker.h:38
static env_checker * instance()
Definition: env_checker.h:165
void available(bool override)
Definition: env_checker.h:47
bool _available
Definition: env_checker.h:59
int default_install_priority() const
Default install priority to use if the package doesn&#39;t specify one.
Definition: env_checker.h:100
Class for environment checks not recognised by the current version.
Definition: env_checker.h:76
Class to help manage the single env_checker instance.
Definition: env_checker.h:130
bool detected() const
Automatic detection result.
Definition: env_checker.h:44
bool available() const
Return true if this environment is compatible with the current machine.
Definition: env_checker.h:97
Base class for the environment checking classes.
Definition: env_checker.h:31
const check_map & checks() const
Definition: env_checker.h:173
unset_check()
Definition: env_checker.h:67
const std::string & id() const
Unique short Id string for use in maps and file caches.
Definition: env_checker.h:102
env_check_type type() const
Main type to describe this package, chosen from the checks in the environment.
Definition: env_checker.h:105

Reference Manual LibPkg Version 0.6.1 (28 Jan 2015)