proxygen
folly::experimental::EnvironmentState Struct Reference

#include <EnvUtil.h>

Public Types

using EnvType = std::unordered_map< std::string, std::string >
 

Public Member Functions

 EnvironmentState (EnvType const &env)
 
 EnvironmentState (EnvType &&env)
 
EnvType const & operator* () const
 
EnvType const * operator-> () const
 
EnvTypeoperator* ()
 
EnvTypeoperator-> ()
 
void setAsCurrentEnvironment ()
 
std::vector< std::stringtoVector () const
 
std::unique_ptr< char *, void(*)(char **)> toPointerArray () const
 

Static Public Member Functions

static EnvironmentState fromCurrentEnvironment ()
 
static EnvironmentState empty ()
 

Private Member Functions

 EnvironmentState ()
 

Private Attributes

EnvType env_
 

Detailed Description

Definition at line 33 of file EnvUtil.h.

Member Typedef Documentation

Definition at line 34 of file EnvUtil.h.

Constructor & Destructor Documentation

folly::experimental::EnvironmentState::EnvironmentState ( EnvType const &  env)
inlineexplicit

Definition at line 49 of file EnvUtil.h.

49 : env_(env) {}
folly::experimental::EnvironmentState::EnvironmentState ( EnvType &&  env)
inlineexplicit

Definition at line 50 of file EnvUtil.h.

50 : env_(std::move(env)) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::experimental::EnvironmentState::EnvironmentState ( )
inlineprivate

Definition at line 87 of file EnvUtil.h.

87 {}

Member Function Documentation

static EnvironmentState folly::experimental::EnvironmentState::empty ( )
inlinestatic

Definition at line 45 of file EnvUtil.h.

45  {
46  return {};
47  }
EnvironmentState EnvironmentState::fromCurrentEnvironment ( )
static

Definition at line 26 of file EnvUtil.cpp.

References folly::data(), gmock_leak_test::environ, folly::Range< Iter >::find(), folly::gen::move, folly::Range< Iter >::npos, string, folly::Range< Iter >::subpiece(), folly::Range< Iter >::toString(), and folly::value().

26  {
27  std::unordered_map<std::string, std::string> data;
28  for (auto it = environ; it && *it; ++it) {
29  std::string key, value;
30  folly::StringPiece entry(*it);
31  auto equalsPosition = entry.find('=');
32  if (equalsPosition == entry.npos) {
33  throw MalformedEnvironment{to<std::string>(
34  "Environment contains an non key-value-pair string \"", entry, "\"")};
35  }
36  key = entry.subpiece(0, equalsPosition).toString();
37  value = entry.subpiece(equalsPosition + 1).toString();
38  if (data.count(key)) {
39  throw MalformedEnvironment{to<std::string>(
40  "Environment contains duplicate value for \"", key, "\"")};
41  }
42  data.emplace(std::move(key), std::move(value));
43  }
44  return EnvironmentState{std::move(data)};
45 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
const char * string
Definition: Conv.cpp:212
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
EnvType const& folly::experimental::EnvironmentState::operator* ( ) const
inline

Definition at line 53 of file EnvUtil.h.

References env_.

53  {
54  return env_;
55  }
EnvType& folly::experimental::EnvironmentState::operator* ( )
inline

Definition at line 61 of file EnvUtil.h.

References env_.

61  {
62  return env_;
63  }
EnvType const* folly::experimental::EnvironmentState::operator-> ( ) const
inline

Definition at line 56 of file EnvUtil.h.

References env_.

56  {
57  return &env_;
58  }
EnvType* folly::experimental::EnvironmentState::operator-> ( )
inline

Definition at line 64 of file EnvUtil.h.

References env_, setAsCurrentEnvironment(), toPointerArray(), and toVector().

64  {
65  return &env_;
66  }
void EnvironmentState::setAsCurrentEnvironment ( )

Definition at line 47 of file EnvUtil.cpp.

References clearenv(), and env_.

Referenced by operator->().

47  {
48  PCHECK(0 == clearenv());
49  for (const auto& kvp : env_) {
50  PCHECK(0 == setenv(kvp.first.c_str(), kvp.second.c_str(), (int)true));
51  }
52 }
int clearenv()
Definition: Stdlib.cpp:150
std::unique_ptr< char *, void(*)(char **)> EnvironmentState::toPointerArray ( ) const

Definition at line 62 of file EnvUtil.cpp.

References env_, ptr, string, and folly::value().

Referenced by operator->().

63  {
64  size_t totalStringLength{};
65  for (auto const& pair : env_) {
66  totalStringLength += pair.first.size() + pair.second.size() +
67  2 /* intermediate '=' and the terminating NUL */;
68  }
69  size_t allocationRequired =
70  (totalStringLength / sizeof(char*) + 1) + env_.size() + 1;
71  char** raw = new char*[allocationRequired];
72  char** ptrBase = raw;
73  char* stringBase = reinterpret_cast<char*>(&raw[env_.size() + 1]);
74  char* const stringEnd = reinterpret_cast<char*>(&raw[allocationRequired]);
75  for (auto const& pair : env_) {
76  std::string const& key = pair.first;
77  std::string const& value = pair.second;
78  *ptrBase = stringBase;
79  size_t lengthIncludingNullTerminator = key.size() + 1 + value.size() + 1;
80  CHECK_GT(stringEnd - lengthIncludingNullTerminator, stringBase);
81  memcpy(stringBase, key.c_str(), key.size());
82  stringBase += key.size();
83  *stringBase++ = '=';
84  memcpy(stringBase, value.c_str(), value.size() + 1);
85  stringBase += value.size() + 1;
86  ++ptrBase;
87  }
88  *ptrBase = nullptr;
89  CHECK_EQ(env_.size(), ptrBase - raw);
90  return {raw, [](char** ptr) { delete[] ptr; }};
91 }
void * ptr
const char * string
Definition: Conv.cpp:212
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
std::vector< std::string > EnvironmentState::toVector ( ) const

Definition at line 54 of file EnvUtil.cpp.

References env_.

Referenced by operator->().

54  {
55  std::vector<std::string> result;
56  for (auto const& pair : env_) {
57  result.emplace_back(to<std::string>(pair.first, "=", pair.second));
58  }
59  return result;
60 }

Member Data Documentation

EnvType folly::experimental::EnvironmentState::env_
private

Definition at line 88 of file EnvUtil.h.

Referenced by operator*(), operator->(), setAsCurrentEnvironment(), toPointerArray(), and toVector().


The documentation for this struct was generated from the following files: