Ocean
Loading...
Searching...
No Matches
MotionManager.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_DEVICES_IOS_MOTION_MANAGER_H
9#define META_OCEAN_DEVICES_IOS_MOTION_MANAGER_H
10
12
13#include "ocean/base/Callback.h"
14#include "ocean/base/Lock.h"
16
17#include <CoreMotion/CoreMotion.h>
18
19namespace Ocean
20{
21
22namespace Devices
23{
24
25namespace IOS
26{
27
28/**
29 * This class implements a simple wrapper for the CMMotionManager object as an application must not create more than one instance of a CMMotionManager object.
30 * This class is implemented as a singleton and the actual CMMotionManager object can be set explicitly (if already existing in the application), or will be created automatically.<br>
31 * The manager is thread-safe.
32 * @ingroup devicesios
33 */
34class OCEAN_DEVICES_IOS_EXPORT MotionManager : public Singleton<MotionManager>
35{
36 friend class Singleton<MotionManager>;
37
38 public:
39
40 /**
41 * Definition of a listener id for a registered callbacks.
42 */
43 typedef unsigned int ListenerId;
44
45 /**
46 * Definition of a callback function to subscribe for new device motion samples.
47 */
49
50 protected:
51
52 /**
53 * Type of an unordered map mapping listener ids to callback functions.
54 */
55 typedef std::unordered_map<ListenerId, DeviceMotionCallback> DeviceMotionListenerMap;
56
57 public:
58
59 /**
60 * Returns the instance of the CMMotionManager object hold by this singleton.
61 * In the case the object hasn't been set explicitly an object will be created the first time this function is called.
62 * @return The CMMotionManager object
63 * @see setObject();
64 */
65 CMMotionManager* object();
66
67 /**
68 * Sets the instance of the CMMotionManager object explicitly as the application holds the instance already.
69 * Do not call this function twice.
70 * @param object The CMMotionManager object to be set explicitly, must be valid
71 * @return True, if this singleton does not hold an own object already and if the provided object is valids
72 */
73 bool setObject(CMMotionManager* object);
74
75 /**
76 * Register a new listener.
77 * @param callback Callback function that occurs each time a new DeviceMotion sample is provided
78 * @return Unique id for the registered callback
79 */
81
82 /**
83 * Unregister a given listener.
84 * @param listenerId Id returned from addListener, must be valid
85 */
86 void removeListener(const ListenerId listenerId);
87
88 /**
89 * Returns an invalid listener id.
90 * @return Invalid listener id
91 */
92 static constexpr inline ListenerId invalidListenerId();
93
94 protected:
95
96 /**
97 * Protected default constructor.
98 */
99 MotionManager() = default;
100
101 /**
102 * Internally starts the main DeviceMotion callback with CMMotionMap.
103 */
104 void start();
105
106 protected:
107
108 /// The instance of the CMMotionManager object this singleton is responsible for.
109 CMMotionManager* motionManager_ = nullptr;
110
111 /// Map storing the set of registered listeners.
113
114 /// Counter used to produce a new id when addListener is called.
115 unsigned int nextListenerId_ = invalidListenerId();
116
117 /// The manager's lock.
119};
120
122{
123 return 0u;
124}
125
126}
127
128}
129
130}
131
132#endif // META_OCEAN_DEVICES_IOS_MOTION_MANAGER_H
This class implements a container for callback functions.
Definition Callback.h:3456
This class implements a simple wrapper for the CMMotionManager object as an application must not crea...
Definition MotionManager.h:35
DeviceMotionListenerMap deviceMotionListenerMap_
Map storing the set of registered listeners.
Definition MotionManager.h:112
CMMotionManager * object()
Returns the instance of the CMMotionManager object hold by this singleton.
void start()
Internally starts the main DeviceMotion callback with CMMotionMap.
std::unordered_map< ListenerId, DeviceMotionCallback > DeviceMotionListenerMap
Type of an unordered map mapping listener ids to callback functions.
Definition MotionManager.h:55
bool setObject(CMMotionManager *object)
Sets the instance of the CMMotionManager object explicitly as the application holds the instance alre...
Callback< void, CMDeviceMotion * > DeviceMotionCallback
Definition of a callback function to subscribe for new device motion samples.
Definition MotionManager.h:48
static constexpr ListenerId invalidListenerId()
Returns an invalid listener id.
Definition MotionManager.h:121
ListenerId addListener(const DeviceMotionCallback &callback)
Register a new listener.
MotionManager()=default
Protected default constructor.
Lock lock_
The manager's lock.
Definition MotionManager.h:118
unsigned int ListenerId
Definition of a listener id for a registered callbacks.
Definition MotionManager.h:43
void removeListener(const ListenerId listenerId)
Unregister a given listener.
This class implements a recursive lock object.
Definition Lock.h:31
This template class is the base class for all singleton objects.
Definition Singleton.h:71
The namespace covering the entire Ocean framework.
Definition Accessor.h:15