Environment.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===-- Environment.h -------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLDB_UTILITY_ENVIRONMENT_H
  9. #define LLDB_UTILITY_ENVIRONMENT_H
  10. #include "llvm/ADT/StringMap.h"
  11. #include "llvm/Support/Allocator.h"
  12. #include "llvm/Support/FormatProviders.h"
  13. namespace lldb_private {
  14. class Environment : private llvm::StringMap<std::string> {
  15. using Base = llvm::StringMap<std::string>;
  16. public:
  17. class Envp {
  18. public:
  19. Envp(Envp &&RHS) = default;
  20. Envp &operator=(Envp &&RHS) = default;
  21. char *const *get() const { return Data; }
  22. operator char *const *() const { return get(); }
  23. private:
  24. explicit Envp(const Environment &Env);
  25. char *make_entry(llvm::StringRef Key, llvm::StringRef Value);
  26. Envp(const Envp &) = delete;
  27. Envp &operator=(const Envp &) = delete;
  28. friend class Environment;
  29. llvm::BumpPtrAllocator Allocator;
  30. char **Data;
  31. };
  32. using Base::const_iterator;
  33. using Base::iterator;
  34. using Base::value_type;
  35. using Base::begin;
  36. using Base::clear;
  37. using Base::count;
  38. using Base::empty;
  39. using Base::end;
  40. using Base::erase;
  41. using Base::find;
  42. using Base::insert;
  43. using Base::insert_or_assign;
  44. using Base::lookup;
  45. using Base::size;
  46. using Base::try_emplace;
  47. using Base::operator[];
  48. Environment() : Base() {}
  49. Environment(const Environment &RHS) : Base(RHS) {}
  50. Environment(Environment &&RHS) : Base(std::move(RHS)) {}
  51. Environment(char *const *Env)
  52. : Environment(const_cast<const char *const *>(Env)) {}
  53. Environment(const char *const *Env);
  54. Environment &operator=(Environment RHS) {
  55. Base::operator=(std::move(RHS));
  56. return *this;
  57. }
  58. std::pair<iterator, bool> insert(llvm::StringRef KeyEqValue) {
  59. auto Split = KeyEqValue.split('=');
  60. return insert(std::make_pair(Split.first, std::string(Split.second)));
  61. }
  62. void insert(const_iterator first, const_iterator last);
  63. Envp getEnvp() const { return Envp(*this); }
  64. static std::string compose(const value_type &KeyValue) {
  65. return (KeyValue.first() + "=" + KeyValue.second).str();
  66. }
  67. };
  68. } // namespace lldb_private
  69. namespace llvm {
  70. template <> struct format_provider<lldb_private::Environment> {
  71. static void format(const lldb_private::Environment &Env, raw_ostream &Stream,
  72. StringRef Style) {
  73. for (const auto &KV : Env)
  74. Stream << "env[" << KV.first() << "] = " << KV.second << "\n";
  75. }
  76. };
  77. } // namespace llvm
  78. #endif // LLDB_UTILITY_ENVIRONMENT_H