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.

string_constant.h 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2020 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
  15. #define ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
  16. #include "absl/meta/type_traits.h"
  17. #include "absl/strings/string_view.h"
  18. namespace absl
  19. {
  20. ABSL_NAMESPACE_BEGIN
  21. namespace strings_internal
  22. {
  23. // StringConstant<T> represents a compile time string constant.
  24. // It can be accessed via its `absl::string_view value` static member.
  25. // It is guaranteed that the `string_view` returned has constant `.data()`,
  26. // constant `.size()` and constant `value[i]` for all `0 <= i < .size()`
  27. //
  28. // The `T` is an opaque type. It is guaranteed that different string constants
  29. // will have different values of `T`. This allows users to associate the string
  30. // constant with other static state at compile time.
  31. //
  32. // Instances should be made using the `MakeStringConstant()` factory function
  33. // below.
  34. template<typename T>
  35. struct StringConstant
  36. {
  37. private:
  38. static constexpr bool TryConstexprEval(absl::string_view view)
  39. {
  40. return view.empty() || 2 * view[0] != 1;
  41. }
  42. public:
  43. static constexpr absl::string_view value = T{}();
  44. constexpr absl::string_view operator()() const
  45. {
  46. return value;
  47. }
  48. // Check to be sure `view` points to constant data.
  49. // Otherwise, it can't be constant evaluated.
  50. static_assert(TryConstexprEval(value), "The input string_view must point to constant data.");
  51. };
  52. #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  53. template<typename T>
  54. constexpr absl::string_view StringConstant<T>::value;
  55. #endif
  56. // Factory function for `StringConstant` instances.
  57. // It supports callables that have a constexpr default constructor and a
  58. // constexpr operator().
  59. // It must return an `absl::string_view` or `const char*` pointing to constant
  60. // data. This is validated at compile time.
  61. template<typename T>
  62. constexpr StringConstant<T> MakeStringConstant(T)
  63. {
  64. return {};
  65. }
  66. } // namespace strings_internal
  67. ABSL_NAMESPACE_END
  68. } // namespace absl
  69. #endif // ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_