LibPkg
env_checker.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 //
3 // Copyright 2003-2020 Graham Shaw
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 // Created on: 3 Jul 2018
18 // Author: Alan Buckley
19 
20 #ifndef LIBPKG_LIBPKG_ENV_CHECKER_H_
21 #define LIBPKG_LIBPKG_ENV_CHECKER_H_
22 
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include <set>
27 
28 namespace pkg {
29 
30 class env_checker;
31 
34 {
39 };
40 
41 
43 class env_check
44 {
45 public:
46 
47  env_check(const std::string &name, const std::string &desc, const std::string &id, env_check_type type, int install_priority);
48  virtual ~env_check() {}
50  const std::string &name() const {return _name;}
52  const std::string &description() const {return _description;}
54  const std::string &id() const {return _id;}
56  bool detected() const {return _detected;}
58  bool available() const {return _available;}
59  void available(bool override) {_available = override;}
61  env_check_type type() const {return _type;}
64 private:
65  std::string _name;
66  std::string _id;
67  env_check_type _type;
68 protected:
69  std::string _description;
70  bool _detected;
71  bool _available;
73 };
74 
76 class unset_check : public env_check
77 {
78 public:
79  unset_check() : env_check("unset","Environment not set on the package", "u", Unset, 1)
80  {
81  _detected = _available = true;
82  }
83 };
84 
88 class unknown_check : public env_check
89 {
90 public:
91  unknown_check(const std::string &name, const std::string &id) : env_check(name,"Unknown value, upgrade the package client",id, Unknown, 1)
92  {
93  _detected = _available = true;
94  }
95 };
96 
97 
103 class pkg_env
104 {
105 public:
107  const std::string &name() const {return _name;}
109  bool available() const {return _available;}
110 
114  const std::string &id() const {return _id;}
115 
117  env_check_type type() const {return _type;}
118 
120  void reset_available();
121 
122  std::string env_names() const;
123  std::string module_names() const;
124 
125 
126 private:
127  friend env_checker;
128  pkg_env(const std::string &name, const std::vector<env_check *> &checks);
129  ~pkg_env() {};
130 
131  std::string _name;
132  std::string _id;
133  std::vector<env_check *> _checks;
134  bool _available;
135  int _install_priority;
136  env_check_type _type;
137 };
138 
143 {
144 public:
145  env_checker_ptr(const std::string &module_map_path);
146  env_checker_ptr(const env_checker_ptr &other);
147  ~env_checker_ptr();
148 
149  env_checker_ptr &operator=(const env_checker_ptr &other);
150  env_checker *operator->();
151 };
152 
157 {
158 public:
159  class watcher;
160  friend class watcher;
161  friend class env_checker_ptr;
162 
163 private:
164  static env_checker *_instance;
165  int _ref_count;
166  std::set<env_checker::watcher *> _watchers;
167  static unsigned int _next_module_id;
168  std::map<std::string, env_check *> _checks;
169  std::map<std::string, env_check *> _module_checks;
170  env_check *_unset_check;
171  pkg_env *_unset_env;
172  std::map<std::string, pkg_env *> _environments;
173  std::string _module_map_path;
174  std::map<std::string, std::string> _module_ids;
175 
176 public:
177  static env_checker *instance() {return _instance;}
178  void add_ref() {_ref_count++;}
179  void remove_ref();
180 
181  pkg_env *package_env(const std::string &env_list, const std::string &os_depends);
182 
183  typedef std::map<std::string, env_check *> check_map;
184 
185  const check_map &checks() const {return _checks;}
186  const check_map &module_checks() const {return _module_checks;}
187 
188  std::string get_module_id(const std::string &title);
189 
190  bool clear_environment_overrides();
191  bool override_environment(const std::set<std::string> &new_env, const std::set<std::string> &new_mods);
192 
193 private:
194  env_checker(const std::string &module_map_path);
195  virtual ~env_checker();
196  void initialise(const std::string &module_map_path);
197  void add_check(env_check *check);
198  void read_module_map();
199  void write_module_map();
205  void register_watcher(watcher& w);
206 
211  void deregister_watcher(watcher& w);
215  void notify();
216 };
217 
220 {
221 private:
223  std::set<env_checker*> _env_checkers;
224 public:
226  watcher();
227 
229  virtual ~watcher();
230 
234  void watch(env_checker &e);
235 
239  void unwatch(env_checker & e);
240 
244  virtual void handle_change(env_checker & e)=0;
245 };
246 
247 
248 } /* namespace lippkg */
249 
250 #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:156
Class for packages where the environment has not been set.
Definition: env_checker.h:76
void add_ref()
Definition: env_checker.h:178
env_check_type
Type for check.
Definition: env_checker.h:33
A class to represent the enviroment a package is designed for.
Definition: env_checker.h:103
int _install_priority
Definition: env_checker.h:72
std::map< std::string, env_check * > check_map
Definition: env_checker.h:183
System check.
Definition: env_checker.h:35
unknown_check(const std::string &name, const std::string &id)
Definition: env_checker.h:91
Module check.
Definition: env_checker.h:36
The namespace used to hold the package management library.
Definition: auto_dir.cc:23
Check unknown to this version of libpkg.
Definition: env_checker.h:37
No check defined on package.
Definition: env_checker.h:38
const std::string & description() const
A one line description for display purposes only.
Definition: env_checker.h:52
env_check_type type() const
Type for this check.
Definition: env_checker.h:61
const std::string & name() const
The full name of this environment.
Definition: env_checker.h:107
int install_priority()
Priority for this check, the higher the more important.
Definition: env_checker.h:63
bool available() const
Software override of status.
Definition: env_checker.h:58
A mixin class to allow an object to watch the environment checker.
Definition: env_checker.h:219
const check_map & module_checks() const
Definition: env_checker.h:186
const std::string & id() const
Short id consisting of a single letter optionally followed be a number.
Definition: env_checker.h:54
bool _detected
Definition: env_checker.h:70
std::string _description
Definition: env_checker.h:69
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:62
virtual ~env_check()
Definition: env_checker.h:48
const std::string & name() const
The name of the check, should be fairly short.
Definition: env_checker.h:50
static env_checker * instance()
Definition: env_checker.h:177
void available(bool override)
Definition: env_checker.h:59
bool _available
Definition: env_checker.h:71
int default_install_priority() const
Default install priority to use if the package doesn&#39;t specify one.
Definition: env_checker.h:112
Class for environment checks not recognised by the current version.
Definition: env_checker.h:88
Class to help manage the single env_checker instance.
Definition: env_checker.h:142
bool detected() const
Automatic detection result.
Definition: env_checker.h:56
bool available() const
Return true if this environment is compatible with the current machine.
Definition: env_checker.h:109
Base class for the environment checking classes.
Definition: env_checker.h:43
const check_map & checks() const
Definition: env_checker.h:185
unset_check()
Definition: env_checker.h:79
const std::string & id() const
Unique short Id string for use in maps and file caches.
Definition: env_checker.h:114
env_check_type type() const
Main type to describe this package, chosen from the checks in the environment.
Definition: env_checker.h:117

Reference Manual LibPkg Version 0.9.0 (4 Sep 2020)