-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environment.h
136 lines (121 loc) · 4.26 KB
/
Environment.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Environment.h
*
* Created on: Oct 10, 2016
* Author: paul
*/
#ifndef SRC_ENVIRONMENT_H_
#define SRC_ENVIRONMENT_H_
#include <map>
#include <vector>
/**
* Environment variable manager for iash.
* <p>
* Using environment variables is one method that iash provides to allow
* different Command instances to communicate with each other. Internally,
* environment variables are stored as string instances, but Environment
* provides functions to convert numeric and boolean data types to and from
* the string format.
* <p>
* As of version 0.5, iash has the following protected environment variables,
* which can be read but not modified:
* <ul>
* <li>`IASH_APP_NAME`: (since v0.1) The name of the application</li>
* <li>`CWD`: (since v0.5) The absolute path to the iash current working
* directory</li>
* </ul>
*
* @since 0.5
*/
class Environment {
public:
friend class iash;
/**
* Creates an Environment instance with the given application name. iash
* instantiates its own Environment instance, so there is no need to
* construct one manually.
*
* @param appName the name of the application
*/
Environment (const std::string appName = "iash");
virtual ~Environment ();
/**
* Sets the environment variable `key` to `value`. `key` will automatically
* be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @param value the new value of the environment variable
*/
void set (const std::string &key, const std::string &value);
/**
* Sets the environment variable `key` to the boolean value `value`. `key`
* will automatically be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @param value the new value of the environment variable
*/
void set (const std::string &key, const bool value);
/**
* Sets the environment variable `key` to the integer value `value`. `key`
* will automatically be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @param value the new value of the environment variable
*/
void set (const std::string &key, const int value);
/**
* Sets the environment variable `key` to the floating-point value `value`.
* `key` will automatically be converted to upper case, if it is not
* already.
*
* @param key the name of the environment variable
* @param value the new value of the environment variable
*/
void set (const std::string &key, const double value);
/**
* Gets the environment variable `key` as a string. `key` will automatically
* be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @return the value of the environment variable, as a string
*/
const std::string& getString (const std::string &key) const;
/**
* Gets the environment variable `key` as a boolean value. `key` will
* automatically be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @return the value of the environment variable, as a string
*/
bool getBool (const std::string &key) const;
/**
* Gets the environment variable `key` as an integer. `key` will
* automatically be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @return the value of the environment variable, as a string
*/
int getInt (const std::string &key) const;
/**
* Gets the environment variable `key` as a floating-point value. `key` will
* automatically be converted to upper case, if it is not already.
*
* @param key the name of the environment variable
* @return the value of the environment variable, as a string
*/
double getDouble (const std::string &key) const;
/**
* Removes the environment variable `key`. `key` will automatically be
* converted to upper case, if it is not already.
*
* @param key the name of the environment variable to delete
*/
void rm (const std::string &key);
private:
void setProtected (const std::string &key, const std::string &value);
bool isBuiltinVar (const std::string &key) const;
static std::string convName (const std::string &key);
std::map<std::string,std::string> m_envMap;
std::vector<std::string> m_builtins;
};
#endif /* SRC_ENVIRONMENT_H_ */