/src/hermes/include/hermes/Support/OptValue.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and 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 | | |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// \file |
10 | | /// A simple replacement of llvh::Optional<> to be used with trivially copyable |
11 | | /// types in performance-sensitive context because llvh::Optional<> is not |
12 | | /// trivially copyable. |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #ifndef HERMES_SUPPORT_OPTVALUE_H |
16 | | #define HERMES_SUPPORT_OPTVALUE_H |
17 | | |
18 | | #include "hermes/Support/Compiler.h" |
19 | | |
20 | | #include "llvh/ADT/None.h" |
21 | | |
22 | | #include <cassert> |
23 | | #include <type_traits> |
24 | | |
25 | | namespace hermes { |
26 | | |
27 | | /// A simple trivially copyable replacement for llvh::Optional<>. Trivially |
28 | | /// copyable objects use a more efficient ABI passing convenient (can be passed |
29 | | /// directly in registers) and simplify some situations involving goto-s due to |
30 | | /// their trivial destructors. |
31 | | template <typename T> |
32 | | class OptValue { |
33 | | static_assert( |
34 | | std::is_trivially_copyable<T>::value, |
35 | | "OptValue<> can only be used with trivially copyable types"); |
36 | | T value_{}; |
37 | | bool hasValue_; |
38 | | |
39 | | public: |
40 | | typedef T value_type; |
41 | | |
42 | 8.29M | OptValue(llvh::NoneType) : hasValue_(false) {}hermes::OptValue<unsigned int>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 2.30M | OptValue(llvh::NoneType) : hasValue_(false) {} |
Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSourceLocation>::OptValue(llvh::NoneType) hermes::OptValue<hermes::vm::DictPropertyMap::PropertyPos>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 5.30M | OptValue(llvh::NoneType) : hasValue_(false) {} |
hermes::OptValue<hermes::vm::PropertyFlags>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 679k | OptValue(llvh::NoneType) : hasValue_(false) {} |
Unexecuted instantiation: hermes::OptValue<hermes::vm::HermesValue32>::OptValue(llvh::NoneType) Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::OptValue(llvh::NoneType) Unexecuted instantiation: hermes::OptValue<double>::OptValue(llvh::NoneType) hermes::OptValue<facebook::hermes::debugger::StepMode>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 113 | OptValue(llvh::NoneType) : hasValue_(false) {} |
hermes::OptValue<unsigned long>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 52 | OptValue(llvh::NoneType) : hasValue_(false) {} |
Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSearchResult>::OptValue(llvh::NoneType) hermes::OptValue<llvh::StringRef>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 2 | OptValue(llvh::NoneType) : hasValue_(false) {} |
hermes::OptValue<hermes::BuiltinMethod::Enum>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 1 | OptValue(llvh::NoneType) : hasValue_(false) {} |
hermes::OptValue<hermes::parser::TokenKind>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 1.96k | OptValue(llvh::NoneType) : hasValue_(false) {} |
Unexecuted instantiation: hermes::OptValue<hermes::parser::detail::JSParserImpl::EnumKind>::OptValue(llvh::NoneType) Unexecuted instantiation: hermes::OptValue<int>::OptValue(llvh::NoneType) hermes::OptValue<hermes::Subsystem>::OptValue(llvh::NoneType) Line | Count | Source | 42 | 196 | OptValue(llvh::NoneType) : hasValue_(false) {} |
|
43 | 15.7M | explicit OptValue() : hasValue_(false) {}hermes::OptValue<unsigned int>::OptValue() Line | Count | Source | 43 | 1.88M | explicit OptValue() : hasValue_(false) {} |
hermes::OptValue<hermes::vm::SymbolID>::OptValue() Line | Count | Source | 43 | 41 | explicit OptValue() : hasValue_(false) {} |
hermes::OptValue<hermes::vm::Metadata::ArrayData>::OptValue() Line | Count | Source | 43 | 316 | explicit OptValue() : hasValue_(false) {} |
Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::PauseReason>::OptValue() Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSearchResult>::OptValue() hermes::OptValue<char>::OptValue() Line | Count | Source | 43 | 3.84k | explicit OptValue() : hasValue_(false) {} |
hermes::OptValue<char16_t>::OptValue() Line | Count | Source | 43 | 13.8M | explicit OptValue() : hasValue_(false) {} |
|
44 | 16.3M | OptValue(const T &v) : value_(v), hasValue_(true) {}hermes::OptValue<hermes::hbc::DebugSourceLocation>::OptValue(hermes::hbc::DebugSourceLocation const&) Line | Count | Source | 44 | 2.22k | OptValue(const T &v) : value_(v), hasValue_(true) {} |
hermes::OptValue<hermes::vm::DictPropertyMap::PropertyPos>::OptValue(hermes::vm::DictPropertyMap::PropertyPos const&) Line | Count | Source | 44 | 4.71M | OptValue(const T &v) : value_(v), hasValue_(true) {} |
hermes::OptValue<hermes::vm::PropertyFlags>::OptValue(hermes::vm::PropertyFlags const&) Line | Count | Source | 44 | 1.27M | OptValue(const T &v) : value_(v), hasValue_(true) {} |
Unexecuted instantiation: hermes::OptValue<hermes::vm::HermesValue32>::OptValue(hermes::vm::HermesValue32 const&) Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::OptValue(hermes::vm::RegExpMatchRange const&) Unexecuted instantiation: hermes::OptValue<llvh::ArrayRef<hermes::vm::SymbolID> >::OptValue(llvh::ArrayRef<hermes::vm::SymbolID> const&) hermes::OptValue<hermes::vm::Metadata::ArrayData>::OptValue(hermes::vm::Metadata::ArrayData const&) Line | Count | Source | 44 | 8 | OptValue(const T &v) : value_(v), hasValue_(true) {} |
Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSearchResult>::OptValue(hermes::hbc::DebugSearchResult const&) hermes::OptValue<llvh::StringRef>::OptValue(llvh::StringRef const&) Line | Count | Source | 44 | 5.80k | OptValue(const T &v) : value_(v), hasValue_(true) {} |
hermes::OptValue<unsigned int>::OptValue(unsigned int const&) Line | Count | Source | 44 | 2.65M | OptValue(const T &v) : value_(v), hasValue_(true) {} |
hermes::OptValue<double>::OptValue(double const&) Line | Count | Source | 44 | 530k | OptValue(const T &v) : value_(v), hasValue_(true) {} |
Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::PauseReason>::OptValue(facebook::hermes::debugger::PauseReason const&) Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::StepMode>::OptValue(facebook::hermes::debugger::StepMode const&) hermes::OptValue<unsigned long>::OptValue(unsigned long const&) Line | Count | Source | 44 | 170 | OptValue(const T &v) : value_(v), hasValue_(true) {} |
Unexecuted instantiation: hermes::OptValue<hermes::BuiltinMethod::Enum>::OptValue(hermes::BuiltinMethod::Enum const&) hermes::OptValue<hermes::Subsystem>::OptValue(hermes::Subsystem const&) Line | Count | Source | 44 | 1.88k | OptValue(const T &v) : value_(v), hasValue_(true) {} |
hermes::OptValue<hermes::parser::TokenKind>::OptValue(hermes::parser::TokenKind const&) Line | Count | Source | 44 | 1.90k | OptValue(const T &v) : value_(v), hasValue_(true) {} |
Unexecuted instantiation: hermes::OptValue<hermes::parser::detail::JSParserImpl::EnumKind>::OptValue(hermes::parser::detail::JSParserImpl::EnumKind const&) Unexecuted instantiation: hermes::OptValue<int>::OptValue(int const&) hermes::OptValue<char>::OptValue(char const&) Line | Count | Source | 44 | 1.67k | OptValue(const T &v) : value_(v), hasValue_(true) {} |
hermes::OptValue<char16_t>::OptValue(char16_t const&) Line | Count | Source | 44 | 7.16M | OptValue(const T &v) : value_(v), hasValue_(true) {} |
|
45 | | |
46 | | OptValue(const OptValue &) = default; |
47 | | OptValue &operator=(const OptValue &) = default; |
48 | | ~OptValue() = default; |
49 | | |
50 | 29.3M | bool hasValue() const { |
51 | 29.3M | return hasValue_; |
52 | 29.3M | } hermes::OptValue<hermes::vm::DictPropertyMap::PropertyPos>::hasValue() const Line | Count | Source | 50 | 6.81M | bool hasValue() const { | 51 | 6.81M | return hasValue_; | 52 | 6.81M | } |
hermes::OptValue<unsigned int>::hasValue() const Line | Count | Source | 50 | 10.2M | bool hasValue() const { | 51 | 10.2M | return hasValue_; | 52 | 10.2M | } |
hermes::OptValue<hermes::hbc::DebugSourceLocation>::hasValue() const Line | Count | Source | 50 | 4.45k | bool hasValue() const { | 51 | 4.45k | return hasValue_; | 52 | 4.45k | } |
hermes::OptValue<hermes::vm::Metadata::ArrayData>::hasValue() const Line | Count | Source | 50 | 782k | bool hasValue() const { | 51 | 782k | return hasValue_; | 52 | 782k | } |
Unexecuted instantiation: hermes::OptValue<llvh::ArrayRef<hermes::vm::SymbolID> >::hasValue() const hermes::OptValue<hermes::vm::PropertyFlags>::hasValue() const Line | Count | Source | 50 | 1.27M | bool hasValue() const { | 51 | 1.27M | return hasValue_; | 52 | 1.27M | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::HermesValue32>::hasValue() const hermes::OptValue<double>::hasValue() const Line | Count | Source | 50 | 530k | bool hasValue() const { | 51 | 530k | return hasValue_; | 52 | 530k | } |
hermes::OptValue<llvh::StringRef>::hasValue() const Line | Count | Source | 50 | 4.88k | bool hasValue() const { | 51 | 4.88k | return hasValue_; | 52 | 4.88k | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::hasValue() const hermes::OptValue<unsigned long>::hasValue() const Line | Count | Source | 50 | 170 | bool hasValue() const { | 51 | 170 | return hasValue_; | 52 | 170 | } |
Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::StepMode>::hasValue() const Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::PauseReason>::hasValue() const Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSearchResult>::hasValue() const Unexecuted instantiation: hermes::OptValue<hermes::BuiltinMethod::Enum>::hasValue() const Unexecuted instantiation: hermes::OptValue<int>::hasValue() const hermes::OptValue<hermes::parser::TokenKind>::hasValue() const Line | Count | Source | 50 | 7.20k | bool hasValue() const { | 51 | 7.20k | return hasValue_; | 52 | 7.20k | } |
Unexecuted instantiation: hermes::OptValue<hermes::parser::detail::JSParserImpl::EnumKind>::hasValue() const hermes::OptValue<char>::hasValue() const Line | Count | Source | 50 | 2.56k | bool hasValue() const { | 51 | 2.56k | return hasValue_; | 52 | 2.56k | } |
hermes::OptValue<char16_t>::hasValue() const Line | Count | Source | 50 | 9.71M | bool hasValue() const { | 51 | 9.71M | return hasValue_; | 52 | 9.71M | } |
Unexecuted instantiation: hermes::OptValue<hermes::Subsystem>::hasValue() const |
53 | 33.0M | explicit operator bool() const { |
54 | 33.0M | return hasValue_; |
55 | 33.0M | } hermes::OptValue<hermes::vm::DictPropertyMap::PropertyPos>::operator bool() const Line | Count | Source | 53 | 8.55M | explicit operator bool() const { | 54 | 8.55M | return hasValue_; | 55 | 8.55M | } |
hermes::OptValue<unsigned int>::operator bool() const Line | Count | Source | 53 | 5.52M | explicit operator bool() const { | 54 | 5.52M | return hasValue_; | 55 | 5.52M | } |
hermes::OptValue<unsigned long>::operator bool() const Line | Count | Source | 53 | 222 | explicit operator bool() const { | 54 | 222 | return hasValue_; | 55 | 222 | } |
hermes::OptValue<hermes::hbc::DebugSourceLocation>::operator bool() const Line | Count | Source | 53 | 2.26k | explicit operator bool() const { | 54 | 2.26k | return hasValue_; | 55 | 2.26k | } |
hermes::OptValue<hermes::vm::Metadata::ArrayData>::operator bool() const Line | Count | Source | 53 | 5.20M | explicit operator bool() const { | 54 | 5.20M | return hasValue_; | 55 | 5.20M | } |
Unexecuted instantiation: hermes::OptValue<llvh::ArrayRef<hermes::vm::SymbolID> >::operator bool() const hermes::OptValue<hermes::vm::PropertyFlags>::operator bool() const Line | Count | Source | 53 | 1.95M | explicit operator bool() const { | 54 | 1.95M | return hasValue_; | 55 | 1.95M | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::HermesValue32>::operator bool() const hermes::OptValue<double>::operator bool() const Line | Count | Source | 53 | 530k | explicit operator bool() const { | 54 | 530k | return hasValue_; | 55 | 530k | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::operator bool() const Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::StepMode>::operator bool() const hermes::OptValue<hermes::BuiltinMethod::Enum>::operator bool() const Line | Count | Source | 53 | 1 | explicit operator bool() const { | 54 | 1 | return hasValue_; | 55 | 1 | } |
hermes::OptValue<llvh::StringRef>::operator bool() const Line | Count | Source | 53 | 5.65k | explicit operator bool() const { | 54 | 5.65k | return hasValue_; | 55 | 5.65k | } |
hermes::OptValue<hermes::parser::TokenKind>::operator bool() const Line | Count | Source | 53 | 1.77k | explicit operator bool() const { | 54 | 1.77k | return hasValue_; | 55 | 1.77k | } |
hermes::OptValue<char>::operator bool() const Line | Count | Source | 53 | 2.85k | explicit operator bool() const { | 54 | 2.85k | return hasValue_; | 55 | 2.85k | } |
hermes::OptValue<char16_t>::operator bool() const Line | Count | Source | 53 | 11.2M | explicit operator bool() const { | 54 | 11.2M | return hasValue_; | 55 | 11.2M | } |
hermes::OptValue<hermes::Subsystem>::operator bool() const Line | Count | Source | 53 | 398 | explicit operator bool() const { | 54 | 398 | return hasValue_; | 55 | 398 | } |
|
56 | | |
57 | 19.9M | const T &getValue() const { |
58 | 19.9M | assert(hasValue()); |
59 | 19.9M | return value_; |
60 | 19.9M | } hermes::OptValue<unsigned int>::getValue() const Line | Count | Source | 57 | 5.49M | const T &getValue() const { | 58 | 5.49M | assert(hasValue()); | 59 | 5.49M | return value_; | 60 | 5.49M | } |
hermes::OptValue<hermes::hbc::DebugSourceLocation>::getValue() const Line | Count | Source | 57 | 4.45k | const T &getValue() const { | 58 | 4.45k | assert(hasValue()); | 59 | 4.45k | return value_; | 60 | 4.45k | } |
hermes::OptValue<hermes::vm::DictPropertyMap::PropertyPos>::getValue() const Line | Count | Source | 57 | 4.71M | const T &getValue() const { | 58 | 4.71M | assert(hasValue()); | 59 | 4.71M | return value_; | 60 | 4.71M | } |
hermes::OptValue<hermes::vm::Metadata::ArrayData>::getValue() const Line | Count | Source | 57 | 782k | const T &getValue() const { | 58 | 782k | assert(hasValue()); | 59 | 782k | return value_; | 60 | 782k | } |
Unexecuted instantiation: hermes::OptValue<llvh::ArrayRef<hermes::vm::SymbolID> >::getValue() const hermes::OptValue<hermes::vm::PropertyFlags>::getValue() const Line | Count | Source | 57 | 1.27M | const T &getValue() const { | 58 | 1.27M | assert(hasValue()); | 59 | 1.27M | return value_; | 60 | 1.27M | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::HermesValue32>::getValue() const hermes::OptValue<double>::getValue() const Line | Count | Source | 57 | 530k | const T &getValue() const { | 58 | 530k | assert(hasValue()); | 59 | 530k | return value_; | 60 | 530k | } |
hermes::OptValue<llvh::StringRef>::getValue() const Line | Count | Source | 57 | 4.88k | const T &getValue() const { | 58 | 4.88k | assert(hasValue()); | 59 | 4.88k | return value_; | 60 | 4.88k | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::getValue() const hermes::OptValue<unsigned long>::getValue() const Line | Count | Source | 57 | 170 | const T &getValue() const { | 58 | 170 | assert(hasValue()); | 59 | 170 | return value_; | 60 | 170 | } |
Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::StepMode>::getValue() const Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::PauseReason>::getValue() const Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSearchResult>::getValue() const Unexecuted instantiation: hermes::OptValue<hermes::BuiltinMethod::Enum>::getValue() const Unexecuted instantiation: hermes::OptValue<int>::getValue() const hermes::OptValue<hermes::parser::TokenKind>::getValue() const Line | Count | Source | 57 | 1.77k | const T &getValue() const { | 58 | 1.77k | assert(hasValue()); | 59 | 1.77k | return value_; | 60 | 1.77k | } |
Unexecuted instantiation: hermes::OptValue<hermes::parser::detail::JSParserImpl::EnumKind>::getValue() const hermes::OptValue<char>::getValue() const Line | Count | Source | 57 | 1.57k | const T &getValue() const { | 58 | 1.57k | assert(hasValue()); | 59 | 1.57k | return value_; | 60 | 1.57k | } |
hermes::OptValue<char16_t>::getValue() const Line | Count | Source | 57 | 7.16M | const T &getValue() const { | 58 | 7.16M | assert(hasValue()); | 59 | 7.16M | return value_; | 60 | 7.16M | } |
Unexecuted instantiation: hermes::OptValue<hermes::Subsystem>::getValue() const |
61 | 18.5M | const T &operator*() const { |
62 | 18.5M | return getValue(); |
63 | 18.5M | } hermes::OptValue<unsigned int>::operator*() const Line | Count | Source | 61 | 5.24M | const T &operator*() const { | 62 | 5.24M | return getValue(); | 63 | 5.24M | } |
hermes::OptValue<hermes::vm::DictPropertyMap::PropertyPos>::operator*() const Line | Count | Source | 61 | 4.71M | const T &operator*() const { | 62 | 4.71M | return getValue(); | 63 | 4.71M | } |
hermes::OptValue<hermes::vm::Metadata::ArrayData>::operator*() const Line | Count | Source | 61 | 782k | const T &operator*() const { | 62 | 782k | return getValue(); | 63 | 782k | } |
Unexecuted instantiation: hermes::OptValue<llvh::ArrayRef<hermes::vm::SymbolID> >::operator*() const hermes::OptValue<hermes::vm::PropertyFlags>::operator*() const Line | Count | Source | 61 | 636k | const T &operator*() const { | 62 | 636k | return getValue(); | 63 | 636k | } |
hermes::OptValue<llvh::StringRef>::operator*() const Line | Count | Source | 61 | 4.88k | const T &operator*() const { | 62 | 4.88k | return getValue(); | 63 | 4.88k | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::operator*() const hermes::OptValue<unsigned long>::operator*() const Line | Count | Source | 61 | 170 | const T &operator*() const { | 62 | 170 | return getValue(); | 63 | 170 | } |
Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::StepMode>::operator*() const Unexecuted instantiation: hermes::OptValue<facebook::hermes::debugger::PauseReason>::operator*() const hermes::OptValue<hermes::hbc::DebugSourceLocation>::operator*() const Line | Count | Source | 61 | 2.14k | const T &operator*() const { | 62 | 2.14k | return getValue(); | 63 | 2.14k | } |
Unexecuted instantiation: hermes::OptValue<hermes::BuiltinMethod::Enum>::operator*() const hermes::OptValue<hermes::parser::TokenKind>::operator*() const Line | Count | Source | 61 | 1.77k | const T &operator*() const { | 62 | 1.77k | return getValue(); | 63 | 1.77k | } |
Unexecuted instantiation: hermes::OptValue<hermes::parser::detail::JSParserImpl::EnumKind>::operator*() const hermes::OptValue<char>::operator*() const Line | Count | Source | 61 | 1.57k | const T &operator*() const { | 62 | 1.57k | return getValue(); | 63 | 1.57k | } |
hermes::OptValue<char16_t>::operator*() const Line | Count | Source | 61 | 7.16M | const T &operator*() const { | 62 | 7.16M | return getValue(); | 63 | 7.16M | } |
Unexecuted instantiation: hermes::OptValue<hermes::Subsystem>::operator*() const |
64 | | |
65 | 638k | const T *operator->() const { |
66 | 638k | return &getValue(); |
67 | 638k | } hermes::OptValue<hermes::hbc::DebugSourceLocation>::operator->() const Line | Count | Source | 65 | 2.30k | const T *operator->() const { | 66 | 2.30k | return &getValue(); | 67 | 2.30k | } |
hermes::OptValue<hermes::vm::PropertyFlags>::operator->() const Line | Count | Source | 65 | 636k | const T *operator->() const { | 66 | 636k | return &getValue(); | 67 | 636k | } |
Unexecuted instantiation: hermes::OptValue<hermes::vm::HermesValue32>::operator->() const Unexecuted instantiation: hermes::OptValue<llvh::StringRef>::operator->() const Unexecuted instantiation: hermes::OptValue<hermes::vm::RegExpMatchRange>::operator->() const Unexecuted instantiation: hermes::OptValue<hermes::hbc::DebugSearchResult>::operator->() const |
68 | | }; |
69 | | static_assert( |
70 | | std::is_trivially_copyable<OptValue<int>>::value, |
71 | | "OptValue<int> must be trivially copyable"); |
72 | | |
73 | | /// Specialization for bool that improves codegen by collapsing compares. |
74 | | template <> |
75 | | class OptValue<bool> { |
76 | | // -1 = none, 0 = false, 1 = true |
77 | | int value_; |
78 | | |
79 | | public: |
80 | | typedef bool value_type; |
81 | 2.14k | OptValue(llvh::NoneType) : value_(-1) {} |
82 | 0 | explicit OptValue() : value_(-1) {} |
83 | 641k | OptValue(bool v) : value_(v ? 1 : 0) {} |
84 | | |
85 | | OptValue(const OptValue &) = default; |
86 | | OptValue &operator=(const OptValue &) = default; |
87 | | ~OptValue() = default; |
88 | | |
89 | 1.28M | bool hasValue() const { |
90 | 1.28M | return value_ >= 0; |
91 | 1.28M | } |
92 | 0 | explicit operator bool() const { |
93 | 0 | return hasValue(); |
94 | 0 | } |
95 | | |
96 | 641k | bool getValue() const { |
97 | 641k | assert(hasValue()); |
98 | 641k | return value_ > 0; |
99 | 641k | } |
100 | | |
101 | 0 | bool operator*() const { |
102 | 0 | return getValue(); |
103 | 0 | } |
104 | | }; |
105 | | static_assert( |
106 | | std::is_trivially_copyable<OptValue<bool>>::value, |
107 | | "OptValue<bool> must be trivially copyable"); |
108 | | |
109 | | template <typename T, typename U> |
110 | 1.77k | bool operator==(const OptValue<T> &a, const OptValue<U> &b) { |
111 | 1.77k | if (a && b) |
112 | 0 | return *a == *b; |
113 | 1.77k | return a.hasValue() == b.hasValue(); |
114 | 1.77k | } |
115 | | |
116 | | template <typename T, typename U> |
117 | | bool operator!=(const OptValue<T> &a, const OptValue<U> &b) { |
118 | | return !(a == b); |
119 | | } |
120 | | } // namespace hermes |
121 | | |
122 | | namespace llvh { |
123 | | template <typename T> |
124 | | struct isPodLike; |
125 | | template <typename T> |
126 | | struct isPodLike<hermes::OptValue<T>> { |
127 | | // An OptValue<T> is pod-like if T is. |
128 | | static const bool value = isPodLike<T>::value; |
129 | | }; |
130 | | } // namespace llvh |
131 | | |
132 | | #endif // HERMES_SUPPORT_OPTVALUE_H |