registry.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. ABSL_NAMESPACE_BEGIN
  26. namespace flags_internal {
  27. // Executes specified visitor for each non-retired flag in the registry.
  28. // Requires the caller hold the registry lock.
  29. void ForEachFlagUnlocked(std::function<void(CommandLineFlag&)> visitor);
  30. // Executes specified visitor for each non-retired flag in the registry. While
  31. // callback are executed, the registry is locked and can't be changed.
  32. void ForEachFlag(std::function<void(CommandLineFlag&)> visitor);
  33. //-----------------------------------------------------------------------------
  34. bool RegisterCommandLineFlag(CommandLineFlag&);
  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. public:
  74. void Retire(const char* flag_name) {
  75. flags_internal::Retire(flag_name, base_internal::FastTypeId<T>(), buf_);
  76. }
  77. private:
  78. alignas(kRetiredFlagObjAlignment) char buf_[kRetiredFlagObjSize];
  79. };
  80. } // namespace flags_internal
  81. ABSL_NAMESPACE_END
  82. } // namespace absl
  83. #endif // ABSL_FLAGS_INTERNAL_REGISTRY_H_