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.

registry.h 4.1 kB

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