Geogram  Version 1.9.1
A programming library of geometric algorithms
environment.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_ENVIRONMENT
41 #define GEOGRAM_BASIC_ENVIRONMENT
42 
43 #include <geogram/basic/common.h>
45 #include <geogram/basic/counted.h>
46 #include <string>
47 #include <vector>
48 #include <map>
49 
57 namespace GEO {
58 
59  class Environment;
60 
61  /************************************************************************/
62 
83  class GEOGRAM_API VariableObserver {
84  public:
92  VariableObserver(const std::string& var_name);
93 
100  virtual void value_changed(const std::string& new_value) = 0;
101 
107  virtual ~VariableObserver();
108 
113  const std::string& observed_variable() const {
114  return observed_variable_;
115  }
116 
117  private:
118  std::string observed_variable_;
119  Environment* environment_;
120  };
121 
122  /************************************************************************/
123 
129  class GEOGRAM_API VariableObserverList {
130  public:
135  block_notify_(false) {
136  }
137 
142  void notify_observers(const std::string& value);
143 
151 
157 
158  private:
160  typedef std::vector<VariableObserver*> Observers;
161  Observers observers_;
162  bool block_notify_;
163  };
164 
165  /************************************************************************/
166 
211  class GEOGRAM_API Environment : public Counted {
212  public:
220 
225  static void terminate();
226 
236  virtual bool add_environment(Environment* env);
237 
244  bool has_value(const std::string& name) const;
245 
257  virtual bool get_value(
258  const std::string& name, std::string& value
259  ) const;
260 
269  std::string get_value(const std::string& name) const;
270 
286  virtual bool set_value(
287  const std::string& name, const std::string& value
288  );
289 
297  virtual Environment* find_environment(const std::string& name);
298 
312  virtual bool add_observer(
313  const std::string& name, VariableObserver* observer
314  );
315 
329  virtual bool remove_observer(
330  const std::string& name, VariableObserver* observer
331  );
332 
344  virtual bool notify_observers(
345  const std::string& name, bool recursive = false
346  );
347 
348  protected:
354  ~Environment() override;
355 
368  virtual bool get_local_value(
369  const std::string& name, std::string& value
370  ) const = 0;
371 
383  virtual bool set_local_value(
384  const std::string& name, const std::string& value
385  ) = 0;
386 
400  const std::string& name, const std::string& value,
401  bool recursive
402  );
403 
415  const std::string& name, const std::string& value
416  );
417 
418  private:
421 
423  typedef std::vector<Environment_var> Environments;
424 
426  typedef std::map<std::string, VariableObserverList> ObserverMap;
427 
428  static Environment_var instance_;
429  Environments environments_;
430  ObserverMap observers_;
431  };
432 
433  /************************************************************************/
434 
443  protected:
445  ~SystemEnvironment() override;
446 
453  const std::string& name, const std::string& value
454  ) override;
455 
462  const std::string& name, std::string& value
463  ) const override;
464  };
465 }
466 
467 #endif
Base class for reference-counted objects.
Definition: counted.h:71
Application environment.
Definition: environment.h:211
virtual bool set_value(const std::string &name, const std::string &value)
Sets a variable value.
virtual bool get_value(const std::string &name, std::string &value) const
Retrieves the value of a variable.
virtual bool set_local_value(const std::string &name, const std::string &value)=0
Sets a variable value locally.
~Environment() override
Environment destructor.
virtual bool notify_observers(const std::string &name, bool recursive=false)
Notifies observers.
virtual bool add_environment(Environment *env)
Adds a child environment.
virtual bool add_observer(const std::string &name, VariableObserver *observer)
Attaches an observer to a variable.
bool notify_local_observers(const std::string &name, const std::string &value)
Notifies local observers.
bool has_value(const std::string &name) const
Tests if a variable exists.
virtual Environment * find_environment(const std::string &name)
Finds the environment that declares a variable as a local name.
static void terminate()
Cleans up the environment.
bool notify_observers(const std::string &name, const std::string &value, bool recursive)
Notifies observers.
std::string get_value(const std::string &name) const
Retrieves the value of a variable.
static Environment * instance()
Gets the root environment.
virtual bool get_local_value(const std::string &name, std::string &value) const =0
Retrieves a variable value locally.
virtual bool remove_observer(const std::string &name, VariableObserver *observer)
Detaches an observer from a variable.
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
System environment.
Definition: environment.h:442
~SystemEnvironment() override
bool set_local_value(const std::string &name, const std::string &value) override
Sets a variable value locally.
bool get_local_value(const std::string &name, std::string &value) const override
Retrieves a variable value locally.
List of VariableObservers.
Definition: environment.h:129
VariableObserverList()
Creates an empty list of variable observers.
Definition: environment.h:134
void notify_observers(const std::string &value)
Notifies all observers in the list.
void remove_observer(VariableObserver *observer)
Removes an observer from the list.
void add_observer(VariableObserver *observer)
Adds an observer to the list. This adds observer observer at the end of the list only if it is not al...
Observes Environment variables.
Definition: environment.h:83
virtual ~VariableObserver()
Deletes the observer.
VariableObserver(const std::string &var_name)
Creates a new variable observer.
virtual void value_changed(const std::string &new_value)=0
Receives a change notification.
const std::string & observed_variable() const
Gets the observed variable.
Definition: environment.h:113
Base class of reference-counted objects, to be used with smart pointers.
Common include file, providing basic definitions. Should be included before anything else by all head...
Global Vorpaline namespace.
Definition: basic.h:55
Pointers with automatic reference counting.