Remove dynamic exceptions
[anna.git] / include / anna / core / util / Environment.hpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #ifndef anna_core_util_Environment_hpp
10 #define anna_core_util_Environment_hpp
11
12 #include <string>
13 #include <map>
14
15 #include <anna/core/RuntimeException.hpp>
16 #include <anna/core/Singleton.hpp>
17
18
19 namespace anna {
20
21 /**
22   Environment variables reader
23 */
24 class Environment : public Singleton <Environment> {
25 public:
26
27   /**
28   * Destructor.
29   */
30   ~Environment() {;}
31
32   /**
33     Parses the environment data (all variables available) when process was started.
34     @param envp Environment array passed on main function as third argument.
35     Cache data is cleared if NULL passed, allowing to get current environment values for variables.
36   */
37   void initialize(char **envp = NULL) ;
38
39   /**
40     Return associated value (could be empty). This value could be cached at initialization (envp array from main function), if not, would be
41     cached here.
42
43     @param variableName Environment variable name.
44     @param exceptionIfMissing When enabled, an exception is launched for missing variables. Empty string in other case (default behaviour).
45
46     @return Environment value.
47   */
48   std::string getValue(const char* variableName, bool exceptionIfMissing = false) noexcept(false);
49   std::string getValue(const std::string &variableName, bool exceptionIfMissing = false) noexcept(false);
50
51   /**
52     Sets an environment variable. If an empty variable name is provided, or environment set operation fails,
53     an exception will be launched.
54
55     @param name Variable name.
56     @param value Variable value
57     @param overwrite Overwrite an existing variable name/value or keep old value if exists
58   */
59   void setVariable(const std::string &name, const std::string &value, bool overwrite = true) noexcept(false);
60
61   /**
62     Unsets an environment variable (different than set empty string). If an empty variable name is provided,
63     or environment set operation fails, an exception will be launched.
64
65     @param name Variable name. If empty, nothing is done.
66   */
67   void unsetVariable(const std::string &name) noexcept(false);
68
69
70 private:
71
72   std::map < std::string /* variable name */, std::string /* variable value */ > a_vars;
73
74   Environment() {;}
75
76   friend class Singleton <Environment>;
77 };
78
79 }
80
81 #endif
82