commandlineflag.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Copyright 2020 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: commandlineflag.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This header file defines the `CommandLineFlag`, which acts as a type-erased
  21. // handle for accessing metadata about the Abseil Flag in question.
  22. //
  23. // Because an actual Abseil flag is of an unspecified type, you should not
  24. // manipulate or interact directly with objects of that type. Instead, use the
  25. // CommandLineFlag type as an intermediary.
  26. #ifndef ABSL_FLAGS_COMMANDLINEFLAG_H_
  27. #define ABSL_FLAGS_COMMANDLINEFLAG_H_
  28. #include <memory>
  29. #include <string>
  30. #include "absl/base/config.h"
  31. #include "absl/base/internal/fast_type_id.h"
  32. #include "absl/flags/internal/commandlineflag.h"
  33. #include "absl/strings/string_view.h"
  34. #include "absl/types/optional.h"
  35. namespace absl {
  36. ABSL_NAMESPACE_BEGIN
  37. namespace flags_internal {
  38. class PrivateHandleAccessor;
  39. } // namespace flags_internal
  40. // CommandLineFlag
  41. //
  42. // This type acts as a type-erased handle for an instance of an Abseil Flag and
  43. // holds reflection information pertaining to that flag. Use CommandLineFlag to
  44. // access a flag's name, location, help string etc.
  45. //
  46. // To obtain an absl::CommandLineFlag, invoke `absl::FindCommandLineFlag()`
  47. // passing it the flag name string.
  48. //
  49. // Example:
  50. //
  51. // // Obtain reflection handle for a flag named "flagname".
  52. // const absl::CommandLineFlag* my_flag_data =
  53. // absl::FindCommandLineFlag("flagname");
  54. //
  55. // // Now you can get flag info from that reflection handle.
  56. // std::string flag_location = my_flag_data->Filename();
  57. // ...
  58. // These are only used as constexpr global objects.
  59. // They do not use a virtual destructor to simplify their implementation.
  60. // They are not destroyed except at program exit, so leaks do not matter.
  61. #if defined(__GNUC__) && !defined(__clang__)
  62. #pragma GCC diagnostic push
  63. #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  64. #endif
  65. class CommandLineFlag {
  66. public:
  67. constexpr CommandLineFlag() = default;
  68. // Not copyable/assignable.
  69. CommandLineFlag(const CommandLineFlag&) = delete;
  70. CommandLineFlag& operator=(const CommandLineFlag&) = delete;
  71. // absl::CommandLineFlag::IsOfType()
  72. //
  73. // Return true iff flag has type T.
  74. template <typename T>
  75. inline bool IsOfType() const {
  76. return TypeId() == base_internal::FastTypeId<T>();
  77. }
  78. // absl::CommandLineFlag::TryGet()
  79. //
  80. // Attempts to retrieve the flag value. Returns value on success,
  81. // absl::nullopt otherwise.
  82. template <typename T>
  83. absl::optional<T> TryGet() const {
  84. if (IsRetired() || !IsOfType<T>()) {
  85. return absl::nullopt;
  86. }
  87. // Implementation notes:
  88. //
  89. // We are wrapping a union around the value of `T` to serve three purposes:
  90. //
  91. // 1. `U.value` has correct size and alignment for a value of type `T`
  92. // 2. The `U.value` constructor is not invoked since U's constructor does
  93. // not do it explicitly.
  94. // 3. The `U.value` destructor is invoked since U's destructor does it
  95. // explicitly. This makes `U` a kind of RAII wrapper around non default
  96. // constructible value of T, which is destructed when we leave the
  97. // scope. We do need to destroy U.value, which is constructed by
  98. // CommandLineFlag::Read even though we left it in a moved-from state
  99. // after std::move.
  100. //
  101. // All of this serves to avoid requiring `T` being default constructible.
  102. union U {
  103. T value;
  104. U() {}
  105. ~U() { value.~T(); }
  106. };
  107. U u;
  108. Read(&u.value);
  109. // allow retired flags to be "read", so we can report invalid access.
  110. if (IsRetired()) {
  111. return absl::nullopt;
  112. }
  113. return std::move(u.value);
  114. }
  115. // absl::CommandLineFlag::Name()
  116. //
  117. // Returns name of this flag.
  118. virtual absl::string_view Name() const = 0;
  119. // absl::CommandLineFlag::Filename()
  120. //
  121. // Returns name of the file where this flag is defined.
  122. virtual std::string Filename() const = 0;
  123. // absl::CommandLineFlag::Help()
  124. //
  125. // Returns help message associated with this flag.
  126. virtual std::string Help() const = 0;
  127. // absl::CommandLineFlag::IsRetired()
  128. //
  129. // Returns true iff this object corresponds to retired flag.
  130. virtual bool IsRetired() const;
  131. // absl::CommandLineFlag::DefaultValue()
  132. //
  133. // Returns the default value for this flag.
  134. virtual std::string DefaultValue() const = 0;
  135. // absl::CommandLineFlag::CurrentValue()
  136. //
  137. // Returns the current value for this flag.
  138. virtual std::string CurrentValue() const = 0;
  139. // absl::CommandLineFlag::ParseFrom()
  140. //
  141. // Sets the value of the flag based on specified string `value`. If the flag
  142. // was successfully set to new value, it returns true. Otherwise, sets `error`
  143. // to indicate the error, leaves the flag unchanged, and returns false.
  144. bool ParseFrom(absl::string_view value, std::string* error);
  145. protected:
  146. ~CommandLineFlag() = default;
  147. private:
  148. friend class flags_internal::PrivateHandleAccessor;
  149. // Sets the value of the flag based on specified string `value`. If the flag
  150. // was successfully set to new value, it returns true. Otherwise, sets `error`
  151. // to indicate the error, leaves the flag unchanged, and returns false. There
  152. // are three ways to set the flag's value:
  153. // * Update the current flag value
  154. // * Update the flag's default value
  155. // * Update the current flag value if it was never set before
  156. // The mode is selected based on `set_mode` parameter.
  157. virtual bool ParseFrom(absl::string_view value,
  158. flags_internal::FlagSettingMode set_mode,
  159. flags_internal::ValueSource source,
  160. std::string& error) = 0;
  161. // Returns id of the flag's value type.
  162. virtual flags_internal::FlagFastTypeId TypeId() const = 0;
  163. // Interface to save flag to some persistent state. Returns current flag state
  164. // or nullptr if flag does not support saving and restoring a state.
  165. virtual std::unique_ptr<flags_internal::FlagStateInterface> SaveState() = 0;
  166. // Copy-construct a new value of the flag's type in a memory referenced by
  167. // the dst based on the current flag's value.
  168. virtual void Read(void* dst) const = 0;
  169. // To be deleted. Used to return true if flag's current value originated from
  170. // command line.
  171. virtual bool IsSpecifiedOnCommandLine() const = 0;
  172. // Validates supplied value using validator or parseflag routine
  173. virtual bool ValidateInputValue(absl::string_view value) const = 0;
  174. // Checks that flags default value can be converted to string and back to the
  175. // flag's value type.
  176. virtual void CheckDefaultValueParsingRoundtrip() const = 0;
  177. };
  178. #if defined(__GNUC__) && !defined(__clang__)
  179. #pragma GCC diagnostic pop
  180. #endif
  181. ABSL_NAMESPACE_END
  182. } // namespace absl
  183. #endif // ABSL_FLAGS_COMMANDLINEFLAG_H_