ProteoWizard
Environment.hpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6//
7// Copyright 2011 Vanderbilt University - Nashville, TN 37232
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22
23#include <stdexcept>
25#include <cstdlib>
26
27
28namespace pwiz {
29namespace util {
30namespace env {
31
32
33template <typename T>
34T get(const char* name, const T& defaultValue)
35{
36 if (!name)
37 throw std::runtime_error("[env::get()] null variable name");
38
39 T value(defaultValue);
40 char* result = ::getenv(name);
41 if (result)
42 value = boost::lexical_cast<T>(result);
43 return value;
44}
45
46
47template <typename T>
48T get(const std::string& name, const T& defaultValue)
49{
50 if (name.empty())
51 throw std::runtime_error("[env::get()] empty variable name");
52
53 return get(name.c_str(), defaultValue);
54}
55
56
57/// explicit single-argument overload
58inline std::string get(const std::string& name) {return get<std::string>(name, std::string());}
59
60
61} // namespace env
62} // namespace util
63} // namespace pwiz
T defaultValue()
T get(const char *name, const T &defaultValue)