You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

commandlineflag.h 7.9 kB

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