libcamera  v0.0.0
Supporting cameras in Linux since 2019
object.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2019, Google Inc.
4  *
5  * object.h - Base object
6  */
7 #ifndef __LIBCAMERA_OBJECT_H__
8 #define __LIBCAMERA_OBJECT_H__
9 
10 #include <list>
11 #include <memory>
12 #include <vector>
13 
14 #include <libcamera/bound_method.h>
15 
16 namespace libcamera {
17 
18 class Message;
19 template<typename... Args>
20 class Signal;
21 class SignalBase;
22 class Thread;
23 
24 class Object
25 {
26 public:
27  Object(Object *parent = nullptr);
28  virtual ~Object();
29 
30  void postMessage(std::unique_ptr<Message> msg);
31 
32  template<typename T, typename R, typename... FuncArgs, typename... Args,
33  typename std::enable_if_t<std::is_base_of<Object, T>::value> * = nullptr>
34  R invokeMethod(R (T::*func)(FuncArgs...), ConnectionType type,
35  Args... args)
36  {
37  T *obj = static_cast<T *>(this);
38  auto *method = new BoundMethodMember<T, R, FuncArgs...>(obj, this, func, type);
39  return method->activate(args..., true);
40  }
41 
42  Thread *thread() const { return thread_; }
43  void moveToThread(Thread *thread);
44 
45  Object *parent() const { return parent_; }
46 
47 protected:
48  virtual void message(Message *msg);
49 
50 private:
51  friend class SignalBase;
52  friend class Thread;
53 
54  void notifyThreadMove();
55 
56  void connect(SignalBase *signal);
57  void disconnect(SignalBase *signal);
58 
59  Object *parent_;
60  std::vector<Object *> children_;
61 
62  Thread *thread_;
63  std::list<SignalBase *> signals_;
64  unsigned int pendingMessages_;
65 };
66 
67 } /* namespace libcamera */
68 
69 #endif /* __LIBCAMERA_OBJECT_H__ */
Method bind and invocation.
ConnectionType
Connection type for asynchronous communication.
Definition: bound_method.h:19
A message that can be posted to a Thread.
Definition: message.h:22
Base object to support automatic signal disconnection.
Definition: object.h:25
Object(Object *parent=nullptr)
Construct an Object instance.
Definition: object.cpp:61
R invokeMethod(R(T::*func)(FuncArgs...), ConnectionType type, Args... args)
Invoke a method asynchronously on an Object instance.
Definition: object.h:34
Object * parent() const
Retrieve the object's parent.
Definition: object.h:45
virtual void message(Message *msg)
Message handler for the object.
Definition: object.cpp:133
void moveToThread(Thread *thread)
Move the object and all its children to a different thread.
Definition: object.cpp:197
Thread * thread() const
Retrieve the thread the object is bound to.
Definition: object.h:42
virtual ~Object()
Destroy an Object instance.
Definition: object.cpp:77
void postMessage(std::unique_ptr< Message > msg)
Post a message to the object's thread.
Definition: object.cpp:116
A thread of execution.
Definition: thread.h:31