1 #pragma once
2 
3 #include <jsi/jsi.h>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 #include "HostFunctionHandler.h"
8 #include "JSIStoreValueUser.h"
9 #include "SharedParent.h"
10 #include "WorkletsCache.h"
11 
12 namespace reanimated {
13 
14 class HostFunctionWrapper;
15 class AnimatedSensorModule;
16 
17 class ValueWrapper {
18   friend AnimatedSensorModule;
19 
20  public:
ValueWrapper()21   ValueWrapper() {}
ValueWrapper(ValueType _type)22   explicit ValueWrapper(ValueType _type) : type(_type) {}
getType()23   ValueType getType() const {
24     return type;
25   }
26 
~ValueWrapper()27   virtual ~ValueWrapper() {}
28 
29   static inline bool asBoolean(
30       const std::unique_ptr<ValueWrapper> &valueContainer);
31   static inline double asNumber(
32       const std::unique_ptr<ValueWrapper> &valueContainer);
33   static inline const std::string &asString(
34       const std::unique_ptr<ValueWrapper> &valueContainer);
35   static inline const std::shared_ptr<HostFunctionHandler> &asHostFunction(
36       const std::unique_ptr<ValueWrapper> &valueContainer);
37   static inline const std::shared_ptr<FrozenObject> &asFrozenObject(
38       const std::unique_ptr<ValueWrapper> &valueContainer);
39   static inline const std::shared_ptr<RemoteObject> &asRemoteObject(
40       const std::unique_ptr<ValueWrapper> &valueContainer);
41   static inline std::vector<std::shared_ptr<ShareableValue>> &asFrozenArray(
42       const std::unique_ptr<ValueWrapper> &valueContainer);
43   static inline const std::shared_ptr<MutableValue> &asMutableValue(
44       const std::unique_ptr<ValueWrapper> &valueContainer);
45 
46   static const HostFunctionWrapper *asHostFunctionWrapper(
47       const std::unique_ptr<ValueWrapper> &valueContainer);
48 
49  protected:
50   ValueType type;
51 };
52 
53 class BooleanValueWrapper : public ValueWrapper {
54  public:
BooleanValueWrapper(const bool _value)55   explicit BooleanValueWrapper(const bool _value)
56       : ValueWrapper(ValueType::BoolType), value(_value) {}
57   bool value;
58 };
59 
60 class NumberValueWrapper : public ValueWrapper {
61  public:
NumberValueWrapper(const double _value)62   explicit NumberValueWrapper(const double _value)
63       : ValueWrapper(ValueType::NumberType), value(_value) {}
64   double value;
65 };
66 
67 class StringValueWrapper : public ValueWrapper {
68  public:
StringValueWrapper(const std::string & _value)69   explicit StringValueWrapper(const std::string &_value)
70       : ValueWrapper(ValueType::StringType), value(_value) {}
71   std::string value;
72 };
73 
74 class HostFunctionWrapper : public ValueWrapper {
75  public:
HostFunctionWrapper(const std::shared_ptr<HostFunctionHandler> & _value)76   explicit HostFunctionWrapper(
77       const std::shared_ptr<HostFunctionHandler> &_value)
78       : ValueWrapper(ValueType::HostFunctionType), value(_value) {}
79   std::shared_ptr<HostFunctionHandler> value;
80 };
81 
82 class FrozenObjectWrapper : public ValueWrapper {
83  public:
FrozenObjectWrapper(const std::shared_ptr<FrozenObject> & _value)84   explicit FrozenObjectWrapper(const std::shared_ptr<FrozenObject> &_value)
85       : ValueWrapper(ValueType::FrozenObjectType), value(_value) {}
86   std::shared_ptr<FrozenObject> value;
87 };
88 
89 class RemoteObjectWrapper : public ValueWrapper {
90  public:
RemoteObjectWrapper(const std::shared_ptr<RemoteObject> & _value)91   explicit RemoteObjectWrapper(const std::shared_ptr<RemoteObject> &_value)
92       : ValueWrapper(ValueType::RemoteObjectType), value(_value) {}
93   std::shared_ptr<RemoteObject> value;
94 };
95 
96 class FrozenArrayWrapper : public ValueWrapper {
97  public:
FrozenArrayWrapper()98   FrozenArrayWrapper() : ValueWrapper(ValueType::FrozenArrayType) {}
FrozenArrayWrapper(const std::vector<std::shared_ptr<ShareableValue>> & _value)99   explicit FrozenArrayWrapper(
100       const std::vector<std::shared_ptr<ShareableValue>> &_value)
101       : ValueWrapper(ValueType::FrozenArrayType), value(_value) {}
102   std::vector<std::shared_ptr<ShareableValue>> value;
103 };
104 
105 class MutableValueWrapper : public ValueWrapper {
106  public:
MutableValueWrapper(const std::shared_ptr<MutableValue> & _value)107   explicit MutableValueWrapper(const std::shared_ptr<MutableValue> &_value)
108       : ValueWrapper(ValueType::MutableValueType), value(_value) {}
109   std::shared_ptr<MutableValue> value;
110 };
111 
asBoolean(const std::unique_ptr<ValueWrapper> & valueContainer)112 inline bool ValueWrapper::asBoolean(
113     const std::unique_ptr<ValueWrapper> &valueContainer) {
114   return static_cast<BooleanValueWrapper *>(valueContainer.get())->value;
115 }
116 
asNumber(const std::unique_ptr<ValueWrapper> & valueContainer)117 inline double ValueWrapper::asNumber(
118     const std::unique_ptr<ValueWrapper> &valueContainer) {
119   return static_cast<NumberValueWrapper *>(valueContainer.get())->value;
120 }
121 
asString(const std::unique_ptr<ValueWrapper> & valueContainer)122 inline const std::string &ValueWrapper::asString(
123     const std::unique_ptr<ValueWrapper> &valueContainer) {
124   return static_cast<StringValueWrapper *>(valueContainer.get())->value;
125 }
126 
asHostFunction(const std::unique_ptr<ValueWrapper> & valueContainer)127 inline const std::shared_ptr<HostFunctionHandler> &ValueWrapper::asHostFunction(
128     const std::unique_ptr<ValueWrapper> &valueContainer) {
129   return static_cast<HostFunctionWrapper *>(valueContainer.get())->value;
130 }
131 
asFrozenObject(const std::unique_ptr<ValueWrapper> & valueContainer)132 inline const std::shared_ptr<FrozenObject> &ValueWrapper::asFrozenObject(
133     const std::unique_ptr<ValueWrapper> &valueContainer) {
134   return static_cast<FrozenObjectWrapper *>(valueContainer.get())->value;
135 }
136 
asRemoteObject(const std::unique_ptr<ValueWrapper> & valueContainer)137 inline const std::shared_ptr<RemoteObject> &ValueWrapper::asRemoteObject(
138     const std::unique_ptr<ValueWrapper> &valueContainer) {
139   return static_cast<RemoteObjectWrapper *>(valueContainer.get())->value;
140 }
141 
142 inline std::vector<std::shared_ptr<ShareableValue>>
asFrozenArray(const std::unique_ptr<ValueWrapper> & valueContainer)143     &ValueWrapper::asFrozenArray(
144         const std::unique_ptr<ValueWrapper> &valueContainer) {
145   return static_cast<FrozenArrayWrapper *>(valueContainer.get())->value;
146 }
147 
asMutableValue(const std::unique_ptr<ValueWrapper> & valueContainer)148 inline const std::shared_ptr<MutableValue> &ValueWrapper::asMutableValue(
149     const std::unique_ptr<ValueWrapper> &valueContainer) {
150   return static_cast<MutableValueWrapper *>(valueContainer.get())->value;
151 }
152 
asHostFunctionWrapper(const std::unique_ptr<ValueWrapper> & valueContainer)153 inline const HostFunctionWrapper *ValueWrapper::asHostFunctionWrapper(
154     const std::unique_ptr<ValueWrapper> &valueContainer) {
155   return static_cast<HostFunctionWrapper *>(valueContainer.get());
156 }
157 
158 } // namespace reanimated
159