Ocean
JSDevice.h
Go to the documentation of this file.
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 #ifndef META_OCEAN_INTERACTION_JS_DEVICE_H
9 #define META_OCEAN_INTERACTION_JS_DEVICE_H
10 
12 
15 #include "ocean/devices/Tracker.h"
16 
17 #include <v8.h>
18 
19 namespace Ocean
20 {
21 
22 namespace Interaction
23 {
24 
25 namespace JavaScript
26 {
27 
28 class JSContext;
29 
30 /**
31  * This class is a lightweight helper class for device objects to allow event callback handling.
32  * @ingroup interactionjs
33  */
34 class OCEAN_INTERACTION_JS_EXPORT JSDevice
35 {
36  public:
37 
38  /**
39  * Creates a new device helper object.
40  */
42 
43  /**
44  * Move constructor.
45  * @param jsDevice The device object to be moved
46  */
47  JSDevice(JSDevice&& jsDevice);
48 
49  /**
50  * Creates a new device helper object.
51  */
52  explicit JSDevice(const Devices::DeviceRef& device);
53 
54  /**
55  * Destructs a device helper object.
56  */
58 
59  /**
60  * Returns the internal device object.
61  * @return Device object
62  */
63  inline const Devices::DeviceRef& device() const;
64 
65  /**
66  * Sets the internal device object.
67  * Beware: If the internal device has been defined once, you can not change it
68  * @param device Internal device to set
69  */
70  void setDevice(const Devices::DeviceRef& device);
71 
72  /**
73  * Sets the JavaScript function for found object events.
74  * @param function JavaScript function to be called on new found object events
75  */
76  void setFoundObjectEventFunction(v8::Local<v8::Function> function);
77 
78  /**
79  * Sets the JavaScript function for lost object events.
80  * @param function JavaScript function to be called on new lost object events
81  */
82  void setLostObjectEventFunction(v8::Local<v8::Function> function);
83 
84  /**
85  * Move operator.
86  * @param jsDevice The device object to be moved
87  * @return The reference to this object
88  */
89  JSDevice& operator=(JSDevice&& jsDevice);
90 
91  protected:
92 
93  /**
94  * Disabled copy constructor.
95  * @param device Object which would be copied
96  */
97  JSDevice(const JSDevice& device) = delete;
98 
99  /**
100  * Releases all event subscriptions.
101  */
102  void release();
103 
104  /**
105  * Callback function for tracker object events.
106  * @param tracker Event sender
107  * @param found State determining whether an object has been found or lost
108  * @param objectIds The ids of all objects which have been found or lost recently
109  * @param timestamp Event timestamp
110  */
111  void onObjectEvent(const Devices::Tracker* tracker, const bool found, const Devices::Tracker::ObjectIdSet& objectIds, const Timestamp& timestamp);
112 
113  /**
114  * Disabled copy operator.
115  * @param device Object which would be copied
116  * @return Reference to this object
117  */
118  JSDevice& operator=(const JSDevice& device) = delete;
119 
120  /**
121  * Creates JavaScript parameters of one given values.
122  * @param object Object id
123  * @param timestamp Timestamp
124  */
125  static std::vector<v8::Handle<v8::Value>> parameter(const Devices::Tracker::ObjectId object, const Timestamp timestamp);
126 
127  protected:
128 
129  /// Internal device object.
131 
132  /// Sample event subscription id.
134 
135  /// Tracker object event subscription id.
137 
138  /// JavaScript function name for sample events.
140 
141  /// JavaScript function name for found object events.
143 
144  // JavaScript function name for lost object events.
146 
147  /// The context owning this device.
148  std::weak_ptr<JSContext> ownerContext_;
149 };
150 
152 {
153  return device_;
154 }
155 
156 }
157 
158 }
159 
160 }
161 
162 #endif // META_OCEAN_INTERACTION_JS_DEVICE_H
This class manages the lifetime of an event subscription for sample events.
Definition: Measurement.h:154
unsigned int ObjectId
Definition of an object id.
Definition: Measurement.h:46
std::unordered_set< ObjectId > ObjectIdSet
Definition of an unordered set holding object ids.
Definition: Measurement.h:56
This class manages the lifetime of an event subscription for tracker object events.
Definition: devices/Tracker.h:141
This class implements the base class for all tracker devices.
Definition: devices/Tracker.h:39
This class is a lightweight helper class for device objects to allow event callback handling.
Definition: JSDevice.h:35
JSDevice(const Devices::DeviceRef &device)
Creates a new device helper object.
JSDevice & operator=(const JSDevice &device)=delete
Disabled copy operator.
Devices::Tracker::TrackerObjectEventSubscription trackerObjectEventSubscription_
Tracker object event subscription id.
Definition: JSDevice.h:136
std::string foundObjectEventFunctionName_
JavaScript function name for found object events.
Definition: JSDevice.h:142
Devices::DeviceRef device_
Internal device object.
Definition: JSDevice.h:130
~JSDevice()
Destructs a device helper object.
void setDevice(const Devices::DeviceRef &device)
Sets the internal device object.
std::string sampleEventFunctionName_
JavaScript function name for sample events.
Definition: JSDevice.h:139
JSDevice()
Creates a new device helper object.
void release()
Releases all event subscriptions.
void setLostObjectEventFunction(v8::Local< v8::Function > function)
Sets the JavaScript function for lost object events.
void onObjectEvent(const Devices::Tracker *tracker, const bool found, const Devices::Tracker::ObjectIdSet &objectIds, const Timestamp &timestamp)
Callback function for tracker object events.
const Devices::DeviceRef & device() const
Returns the internal device object.
Definition: JSDevice.h:151
JSDevice & operator=(JSDevice &&jsDevice)
Move operator.
JSDevice(const JSDevice &device)=delete
Disabled copy constructor.
Devices::Tracker::SampleEventSubscription sampleEventSubscription_
Sample event subscription id.
Definition: JSDevice.h:133
std::weak_ptr< JSContext > ownerContext_
The context owning this device.
Definition: JSDevice.h:148
JSDevice(JSDevice &&jsDevice)
Move constructor.
void setFoundObjectEventFunction(v8::Local< v8::Function > function)
Sets the JavaScript function for found object events.
static std::vector< v8::Handle< v8::Value > > parameter(const Devices::Tracker::ObjectId object, const Timestamp timestamp)
Creates JavaScript parameters of one given values.
std::string lostObjectEventFunctionName_
Definition: JSDevice.h:145
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15