registry.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Copyright 2019 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. #ifndef Y_ABSL_FLAGS_INTERNAL_REGISTRY_H_
  16. #define Y_ABSL_FLAGS_INTERNAL_REGISTRY_H_
  17. #include <functional>
  18. #include "y_absl/base/config.h"
  19. #include "y_absl/flags/commandlineflag.h"
  20. #include "y_absl/flags/internal/commandlineflag.h"
  21. #include "y_absl/strings/string_view.h"
  22. // --------------------------------------------------------------------
  23. // Global flags registry API.
  24. namespace y_absl {
  25. Y_ABSL_NAMESPACE_BEGIN
  26. namespace flags_internal {
  27. // Executes specified visitor for each non-retired flag in the registry. While
  28. // callback are executed, the registry is locked and can't be changed.
  29. void ForEachFlag(std::function<void(CommandLineFlag&)> visitor);
  30. //-----------------------------------------------------------------------------
  31. bool RegisterCommandLineFlag(CommandLineFlag&, const char* filename);
  32. void FinalizeRegistry();
  33. //-----------------------------------------------------------------------------
  34. // Retired registrations:
  35. //
  36. // Retired flag registrations are treated specially. A 'retired' flag is
  37. // provided only for compatibility with automated invocations that still
  38. // name it. A 'retired' flag:
  39. // - is not bound to a C++ FLAGS_ reference.
  40. // - has a type and a value, but that value is intentionally inaccessible.
  41. // - does not appear in --help messages.
  42. // - is fully supported by _all_ flag parsing routines.
  43. // - consumes args normally, and complains about type mismatches in its
  44. // argument.
  45. // - emits a complaint but does not die (e.g. LOG(ERROR)) if it is
  46. // accessed by name through the flags API for parsing or otherwise.
  47. //
  48. // The registrations for a flag happen in an unspecified order as the
  49. // initializers for the namespace-scope objects of a program are run.
  50. // Any number of weak registrations for a flag can weakly define the flag.
  51. // One non-weak registration will upgrade the flag from weak to non-weak.
  52. // Further weak registrations of a non-weak flag are ignored.
  53. //
  54. // This mechanism is designed to support moving dead flags into a
  55. // 'graveyard' library. An example migration:
  56. //
  57. // 0: Remove references to this FLAGS_flagname in the C++ codebase.
  58. // 1: Register as 'retired' in old_lib.
  59. // 2: Make old_lib depend on graveyard.
  60. // 3: Add a redundant 'retired' registration to graveyard.
  61. // 4: Remove the old_lib 'retired' registration.
  62. // 5: Eventually delete the graveyard registration entirely.
  63. //
  64. // Retire flag with name "name" and type indicated by ops.
  65. void Retire(const char* name, FlagFastTypeId type_id, char* buf);
  66. constexpr size_t kRetiredFlagObjSize = 3 * sizeof(void*);
  67. constexpr size_t kRetiredFlagObjAlignment = alignof(void*);
  68. // Registered a retired flag with name 'flag_name' and type 'T'.
  69. template <typename T>
  70. class RetiredFlag {
  71. public:
  72. void Retire(const char* flag_name) {
  73. flags_internal::Retire(flag_name, base_internal::FastTypeId<T>(), buf_);
  74. }
  75. private:
  76. alignas(kRetiredFlagObjAlignment) char buf_[kRetiredFlagObjSize];
  77. };
  78. } // namespace flags_internal
  79. Y_ABSL_NAMESPACE_END
  80. } // namespace y_absl
  81. #endif // Y_ABSL_FLAGS_INTERNAL_REGISTRY_H_