commandlineflag.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. class CommandLineFlag {
  59. public:
  60. constexpr CommandLineFlag() = default;
  61. // Not copyable/assignable.
  62. CommandLineFlag(const CommandLineFlag&) = delete;
  63. CommandLineFlag& operator=(const CommandLineFlag&) = delete;
  64. // absl::CommandLineFlag::IsOfType()
  65. //
  66. // Return true iff flag has type T.
  67. template <typename T>
  68. inline bool IsOfType() const {
  69. return TypeId() == base_internal::FastTypeId<T>();
  70. }
  71. // absl::CommandLineFlag::TryGet()
  72. //
  73. // Attempts to retrieve the flag value. Returns value on success,
  74. // absl::nullopt otherwise.
  75. template <typename T>
  76. absl::optional<T> TryGet() const {
  77. if (IsRetired() || !IsOfType<T>()) {
  78. return absl::nullopt;
  79. }
  80. // Implementation notes:
  81. //
  82. // We are wrapping a union around the value of `T` to serve three purposes:
  83. //
  84. // 1. `U.value` has correct size and alignment for a value of type `T`
  85. // 2. The `U.value` constructor is not invoked since U's constructor does
  86. // not do it explicitly.
  87. // 3. The `U.value` destructor is invoked since U's destructor does it
  88. // explicitly. This makes `U` a kind of RAII wrapper around non default
  89. // constructible value of T, which is destructed when we leave the
  90. // scope. We do need to destroy U.value, which is constructed by
  91. // CommandLineFlag::Read even though we left it in a moved-from state
  92. // after std::move.
  93. //
  94. // All of this serves to avoid requiring `T` being default constructible.
  95. union U {
  96. T value;
  97. U() {}
  98. ~U() { value.~T(); }
  99. };
  100. U u;
  101. Read(&u.value);
  102. // allow retired flags to be "read", so we can report invalid access.
  103. if (IsRetired()) {
  104. return absl::nullopt;
  105. }
  106. return std::move(u.value);
  107. }
  108. // absl::CommandLineFlag::Name()
  109. //
  110. // Returns name of this flag.
  111. virtual absl::string_view Name() const = 0;
  112. // absl::CommandLineFlag::Filename()
  113. //
  114. // Returns name of the file where this flag is defined.
  115. virtual std::string Filename() const = 0;
  116. // absl::CommandLineFlag::Help()
  117. //
  118. // Returns help message associated with this flag.
  119. virtual std::string Help() const = 0;
  120. // absl::CommandLineFlag::IsRetired()
  121. //
  122. // Returns true iff this object corresponds to retired flag.
  123. virtual bool IsRetired() const;
  124. // absl::CommandLineFlag::DefaultValue()
  125. //
  126. // Returns the default value for this flag.
  127. virtual std::string DefaultValue() const = 0;
  128. // absl::CommandLineFlag::CurrentValue()
  129. //
  130. // Returns the current value for this flag.
  131. virtual std::string CurrentValue() const = 0;
  132. // absl::CommandLineFlag::ParseFrom()
  133. //
  134. // Sets the value of the flag based on specified string `value`. If the flag
  135. // was successfully set to new value, it returns true. Otherwise, sets `error`
  136. // to indicate the error, leaves the flag unchanged, and returns false.
  137. bool ParseFrom(absl::string_view value, std::string* error);
  138. protected:
  139. ~CommandLineFlag() = default;
  140. private:
  141. friend class flags_internal::PrivateHandleAccessor;
  142. // Sets the value of the flag based on specified string `value`. If the flag
  143. // was successfully set to new value, it returns true. Otherwise, sets `error`
  144. // to indicate the error, leaves the flag unchanged, and returns false. There
  145. // are three ways to set the flag's value:
  146. // * Update the current flag value
  147. // * Update the flag's default value
  148. // * Update the current flag value if it was never set before
  149. // The mode is selected based on `set_mode` parameter.
  150. virtual bool ParseFrom(absl::string_view value,
  151. flags_internal::FlagSettingMode set_mode,
  152. flags_internal::ValueSource source,
  153. std::string& error) = 0;
  154. // Returns id of the flag's value type.
  155. virtual flags_internal::FlagFastTypeId TypeId() const = 0;
  156. // Interface to save flag to some persistent state. Returns current flag state
  157. // or nullptr if flag does not support saving and restoring a state.
  158. virtual std::unique_ptr<flags_internal::FlagStateInterface> SaveState() = 0;
  159. // Copy-construct a new value of the flag's type in a memory referenced by
  160. // the dst based on the current flag's value.
  161. virtual void Read(void* dst) const = 0;
  162. // To be deleted. Used to return true if flag's current value originated from
  163. // command line.
  164. virtual bool IsSpecifiedOnCommandLine() const = 0;
  165. // Validates supplied value usign validator or parseflag routine
  166. virtual bool ValidateInputValue(absl::string_view value) const = 0;
  167. // Checks that flags default value can be converted to string and back to the
  168. // flag's value type.
  169. virtual void CheckDefaultValueParsingRoundtrip() const = 0;
  170. };
  171. ABSL_NAMESPACE_END
  172. } // namespace absl
  173. #endif // ABSL_FLAGS_COMMANDLINEFLAG_H_