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.

reflection.h 3.1 kB

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