string_constant.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 Y_ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
  15. #define Y_ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
  16. #include "y_absl/meta/type_traits.h"
  17. #include "y_absl/strings/string_view.h"
  18. namespace y_absl {
  19. Y_ABSL_NAMESPACE_BEGIN
  20. namespace strings_internal {
  21. // StringConstant<T> represents a compile time string constant.
  22. // It can be accessed via its `y_absl::string_view value` static member.
  23. // It is guaranteed that the `string_view` returned has constant `.data()`,
  24. // constant `.size()` and constant `value[i]` for all `0 <= i < .size()`
  25. //
  26. // The `T` is an opaque type. It is guaranteed that different string constants
  27. // will have different values of `T`. This allows users to associate the string
  28. // constant with other static state at compile time.
  29. //
  30. // Instances should be made using the `MakeStringConstant()` factory function
  31. // below.
  32. template <typename T>
  33. struct StringConstant {
  34. private:
  35. static constexpr bool TryConstexprEval(y_absl::string_view view) {
  36. return view.empty() || 2 * view[0] != 1;
  37. }
  38. public:
  39. static constexpr y_absl::string_view value = T{}();
  40. constexpr y_absl::string_view operator()() const { return value; }
  41. // Check to be sure `view` points to constant data.
  42. // Otherwise, it can't be constant evaluated.
  43. static_assert(TryConstexprEval(value),
  44. "The input string_view must point to constant data.");
  45. };
  46. #ifdef Y_ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
  47. template <typename T>
  48. constexpr y_absl::string_view StringConstant<T>::value;
  49. #endif
  50. // Factory function for `StringConstant` instances.
  51. // It supports callables that have a constexpr default constructor and a
  52. // constexpr operator().
  53. // It must return an `y_absl::string_view` or `const char*` pointing to constant
  54. // data. This is validated at compile time.
  55. template <typename T>
  56. constexpr StringConstant<T> MakeStringConstant(T) {
  57. return {};
  58. }
  59. } // namespace strings_internal
  60. Y_ABSL_NAMESPACE_END
  61. } // namespace y_absl
  62. #endif // Y_ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_