AttributedStringBox.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <memory>
  9. #include <react/attributedstring/AttributedString.h>
  10. namespace facebook {
  11. namespace react {
  12. /*
  13. * Represents an object storing a shared `AttributedString` or a shared pointer
  14. * to some opaque platform-specific object that can be used as an attributed
  15. * string. The class serves two main purposes:
  16. * - Represent type-erased attributed string entity (which can be
  17. * platform-specific or platform-independent);
  18. * - Represent a container that can be copied with constant complexity.
  19. */
  20. class AttributedStringBox final {
  21. public:
  22. enum class Mode { Value, OpaquePointer };
  23. /*
  24. * Default constructor constructs an empty string.
  25. */
  26. AttributedStringBox();
  27. /*
  28. * Custom explicit constructors.
  29. */
  30. explicit AttributedStringBox(AttributedString const &value);
  31. explicit AttributedStringBox(std::shared_ptr<void> const &opaquePointer);
  32. /*
  33. * Movable, Copyable, Assignable.
  34. */
  35. AttributedStringBox(AttributedStringBox const &other) = default;
  36. AttributedStringBox(AttributedStringBox &&other) noexcept = default;
  37. AttributedStringBox &operator=(AttributedStringBox const &other) = default;
  38. AttributedStringBox &operator=(AttributedStringBox &&other) = default;
  39. /*
  40. * Getters.
  41. */
  42. Mode getMode() const;
  43. AttributedString const &getValue() const;
  44. std::shared_ptr<void> getOpaquePointer() const;
  45. private:
  46. Mode mode_;
  47. std::shared_ptr<AttributedString const> value_;
  48. std::shared_ptr<void> opaquePointer_;
  49. };
  50. bool operator==(AttributedStringBox const &lhs, AttributedStringBox const &rhs);
  51. bool operator!=(AttributedStringBox const &lhs, AttributedStringBox const &rhs);
  52. } // namespace react
  53. } // namespace facebook
  54. template <>
  55. struct std::hash<facebook::react::AttributedStringBox> {
  56. size_t operator()(
  57. facebook::react::AttributedStringBox const &attributedStringBox) const {
  58. switch (attributedStringBox.getMode()) {
  59. case facebook::react::AttributedStringBox::Mode::Value:
  60. return std::hash<facebook::react::AttributedString>()(
  61. attributedStringBox.getValue());
  62. case facebook::react::AttributedStringBox::Mode::OpaquePointer:
  63. return std::hash<std::shared_ptr<void>>()(
  64. attributedStringBox.getOpaquePointer());
  65. }
  66. }
  67. };