File: Environment.cpp

package info (click to toggle)
vecgeom 1.2.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,016 kB
  • sloc: cpp: 88,803; ansic: 6,888; python: 1,035; sh: 582; sql: 538; makefile: 23
file content (118 lines) | stat: -rw-r--r-- 3,497 bytes parent folder | download
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file Environment.cpp
//---------------------------------------------------------------------------//
#include "VecGeom/management/Environment.h"

#include <cassert>
#include <cstdlib>
#include <mutex>
#include <iostream>

namespace vecgeom {

//---------------------------------------------------------------------------//
// FREE FUNCTIONS
//---------------------------------------------------------------------------//
/*!
 * Access a static global environment variable.
 *
 * This static variable should be shared among Celeritas objects.
 */
Environment &environment()
{
  static Environment environ;
  return environ;
}

//---------------------------------------------------------------------------//
/*!
 * Thread-safe access to global modified environment variables.
 */
std::string const &getenv(std::string const &key)
{
  static std::mutex getenv_mutex;
  std::lock_guard<std::mutex> scoped_lock{getenv_mutex};
  return environment()[key];
}

//---------------------------------------------------------------------------//
/*!
 * Write the accessed environment variables to a stream.
 */
std::ostream &operator<<(std::ostream &os, Environment const &env)
{
  os << "{\n";
  for (auto const &kvref : env.ordered_environment()) {
    Environment::value_type const &kv = kvref;
    os << "  " << kv.first << ": '" << kv.second << "',\n";
  }
  os << '}';
  return os;
}

//---------------------------------------------------------------------------//
// MEMBER FUNCTIONS
//---------------------------------------------------------------------------//
/*!
 * Set a value from the system environment.
 */
auto Environment::load_from_getenv(key_type const &key) -> mapped_type const &
{
  std::string value;
  if (char const *sys_value = std::getenv(key.c_str())) {
    // Variable is set in the user environment
    value = sys_value;
  }

  // Insert value and ordering. Note that since the elements are never
  // erased, pointers to the keys are guaranteed to always be valid.
  auto [iter, inserted] = vars_.emplace(key, std::move(value));
  assert(inserted);
  ordered_.push_back(std::ref(*iter));

  assert(ordered_.size() == vars_.size());
  return iter->second;
}

//---------------------------------------------------------------------------//
/*!
 * Set a single environment variable that hasn't yet been set.
 *
 * Existing environment variables will *not* be overwritten.
 */
void Environment::insert(value_type const &value)
{
  auto [iter, inserted] = vars_.insert(value);
  if (inserted) {
    ordered_.push_back(std::ref(*iter));
  }
  assert(ordered_.size() == vars_.size());
}

//---------------------------------------------------------------------------//
/*!
 * Remove all entries.
 */
void Environment::clear()
{
  vars_.clear();
  ordered_.clear();
}

//---------------------------------------------------------------------------//
/*!
 * Insert but don't override from another environment.
 */
void Environment::merge(Environment const &other)
{
  for (auto const &kv : other.ordered_environment()) {
    this->insert(kv);
  }
}

//---------------------------------------------------------------------------//
} // namespace vecgeom