reflection.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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: reflection.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This file defines the routines to access and operate on an Abseil Flag's
  21. // reflection handle.
  22. #ifndef ABSL_FLAGS_REFLECTION_H_
  23. #define ABSL_FLAGS_REFLECTION_H_
  24. #include <string>
  25. #include "absl/base/config.h"
  26. #include "absl/container/flat_hash_map.h"
  27. #include "absl/flags/commandlineflag.h"
  28. #include "absl/flags/internal/commandlineflag.h"
  29. namespace absl {
  30. ABSL_NAMESPACE_BEGIN
  31. namespace flags_internal {
  32. class FlagSaverImpl;
  33. } // namespace flags_internal
  34. // FindCommandLineFlag()
  35. //
  36. // Returns the reflection handle of an Abseil flag of the specified name, or
  37. // `nullptr` if not found. This function will emit a warning if the name of a
  38. // 'retired' flag is specified.
  39. absl::CommandLineFlag* FindCommandLineFlag(absl::string_view name);
  40. // Returns current state of the Flags registry in a form of mapping from flag
  41. // name to a flag reflection handle.
  42. absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags();
  43. //------------------------------------------------------------------------------
  44. // FlagSaver
  45. //------------------------------------------------------------------------------
  46. //
  47. // A FlagSaver object stores the state of flags in the scope where the FlagSaver
  48. // is defined, allowing modification of those flags within that scope and
  49. // automatic restoration of the flags to their previous state upon leaving the
  50. // scope.
  51. //
  52. // A FlagSaver can be used within tests to temporarily change the test
  53. // environment and restore the test case to its previous state.
  54. //
  55. // Example:
  56. //
  57. // void MyFunc() {
  58. // absl::FlagSaver fs;
  59. // ...
  60. // absl::SetFlag(FLAGS_myFlag, otherValue);
  61. // ...
  62. // } // scope of FlagSaver left, flags return to previous state
  63. //
  64. // This class is thread-safe.
  65. class FlagSaver {
  66. public:
  67. FlagSaver();
  68. ~FlagSaver();
  69. FlagSaver(const FlagSaver&) = delete;
  70. void operator=(const FlagSaver&) = delete;
  71. private:
  72. flags_internal::FlagSaverImpl* impl_;
  73. };
  74. //-----------------------------------------------------------------------------
  75. ABSL_NAMESPACE_END
  76. } // namespace absl
  77. #endif // ABSL_FLAGS_REFLECTION_H_