Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnvCheck.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_SUBSTRATE_ENVCHECK_H
21 #define GALOIS_SUBSTRATE_ENVCHECK_H
22 
23 #include <cassert>
24 #include <string>
25 
26 #include "galois/config.h"
27 
28 namespace galois {
29 namespace substrate {
30 
31 namespace internal {
32 
33 template <typename T>
34 struct ConvByType {};
35 
36 template <>
37 struct ConvByType<int> {
38  static void go(const char* varVal, int& ret) {
39  assert(varVal);
40  ret = std::atoi(varVal);
41  }
42 };
43 
44 template <>
45 struct ConvByType<double> {
46  static void go(const char* varVal, double& ret) {
47  assert(varVal);
48  ret = std::atof(varVal);
49  }
50 };
51 
52 template <>
53 struct ConvByType<std::string> {
54  static void go(const char* varVal, std::string& ret) {
55  assert(varVal);
56  ret = varVal;
57  }
58 };
59 
60 template <typename T>
61 bool genericGetEnv(const char* varName, T& ret) {
62 
63  char* varVal = getenv(varName);
64  if (varVal) {
65  ConvByType<T>::go(varVal, ret);
66  return true;
67  } else {
68  return false;
69  }
70 }
71 
72 } // end namespace internal
73 
75 bool EnvCheck(const char* varName);
76 bool EnvCheck(const std::string& varName);
77 
85 template <typename T>
86 bool EnvCheck(const char* varName, T& retVal) {
87  return internal::genericGetEnv(varName, retVal);
88 }
89 
90 template <typename T>
91 bool EnvCheck(const std::string& varName, T& retVal) {
92  return EnvCheck(varName.c_str(), retVal);
93 }
94 
95 } // end namespace substrate
96 } // end namespace galois
97 
98 #endif
bool EnvCheck(const char *varName)
Return true if the Enviroment variable is set.
Definition: EnvCheck.cpp:24