AttributedStringBox.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #include "AttributedStringBox.h"
  8. namespace facebook {
  9. namespace react {
  10. AttributedStringBox::AttributedStringBox()
  11. : mode_(Mode::Value),
  12. value_(std::make_shared<AttributedString const>(AttributedString{})),
  13. opaquePointer_({}){};
  14. AttributedStringBox::AttributedStringBox(AttributedString const &value)
  15. : mode_(Mode::Value),
  16. value_(std::make_shared<AttributedString const>(value)),
  17. opaquePointer_({}){};
  18. AttributedStringBox::AttributedStringBox(
  19. std::shared_ptr<void> const &opaquePointer)
  20. : mode_(Mode::OpaquePointer), value_({}), opaquePointer_(opaquePointer) {}
  21. AttributedStringBox::Mode AttributedStringBox::getMode() const {
  22. return mode_;
  23. }
  24. AttributedString const &AttributedStringBox::getValue() const {
  25. assert(mode_ == AttributedStringBox::Mode::Value);
  26. assert(value_);
  27. return *value_;
  28. }
  29. std::shared_ptr<void> AttributedStringBox::getOpaquePointer() const {
  30. assert(mode_ == AttributedStringBox::Mode::OpaquePointer);
  31. assert(opaquePointer_);
  32. return opaquePointer_;
  33. }
  34. bool operator==(
  35. AttributedStringBox const &lhs,
  36. AttributedStringBox const &rhs) {
  37. if (lhs.getMode() != rhs.getMode()) {
  38. return false;
  39. }
  40. switch (lhs.getMode()) {
  41. case facebook::react::AttributedStringBox::Mode::Value:
  42. return lhs.getValue() == rhs.getValue();
  43. case facebook::react::AttributedStringBox::Mode::OpaquePointer:
  44. return lhs.getOpaquePointer() == rhs.getOpaquePointer();
  45. }
  46. }
  47. bool operator!=(
  48. AttributedStringBox const &lhs,
  49. AttributedStringBox const &rhs) {
  50. return !(lhs == rhs);
  51. }
  52. } // namespace react
  53. } // namespace facebook