LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
environment_variable.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_UTILS_ENVIRONMENT_VARIABLE_HPP_INCLUDED
28 #define LBANN_UTILS_ENVIRONMENT_VARIABLE_HPP_INCLUDED
29 
31 
32 #include <string>
33 
34 namespace lbann {
35 namespace utils {
36 
39 {
40 public:
41  std::string get(std::string const& var_name) const;
42 };
43 
48 template <typename AccessPolicy = GetEnvAccessor>
50 {
51 public:
53 
56  EnvVariable(std::string const& var_name);
57 
59  EnvVariable(std::string&& var_name);
60 
62 
63 
69  bool exists() const;
70 
72 
73 
76  std::string const& name() const noexcept;
77 
79  std::string raw_value() const;
80 
82  template <typename T>
83  T value() const;
84 
86 
87 private:
89  std::string name_;
90 };
91 
94 
95 // Implementation
96 
97 template <typename AccessPolicy>
98 inline EnvVariable<AccessPolicy>::EnvVariable(std::string const& var_name)
99  : name_{var_name}
100 {}
101 
102 template <typename AccessPolicy>
103 inline EnvVariable<AccessPolicy>::EnvVariable(std::string&& var_name)
104  : name_{std::move(var_name)}
105 {}
106 
107 template <typename AccessPolicy>
109 {
110  return raw_value().size() > 0;
111 }
112 
113 template <typename AccessPolicy>
114 inline std::string const& EnvVariable<AccessPolicy>::name() const noexcept
115 {
116  return name_;
117 }
118 
119 template <typename AccessPolicy>
120 inline std::string EnvVariable<AccessPolicy>::raw_value() const
121 {
122  AccessPolicy access;
123  return access.get(name_);
124 }
125 
126 template <typename AccessPolicy>
127 template <typename T>
129 {
130  return from_string<T>(raw_value());
131 }
132 
133 } // namespace utils
134 } // namespace lbann
135 #endif /* LBANN_UTILS_ENVIRONMENT_VARIABLE_HPP_INCLUDED */
bool exists() const
Test if the variable exists in the environment.
std::string raw_value() const
Get the string value of the environment variable.
An environment variable.
std::string const & name() const noexcept
Get the name of the environment variable.
T value() const
Get the value of the environment variable as a certain type.
Access environment variables using getenv.
std::string name_
The name of the variable.
EnvVariable(std::string const &var_name)
Construct from a string.
int exists(const char *filename)