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