Sen API
Sen Libraries
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1// === object.h ========================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_CORE_OBJ_OBJECT_H
9#define SEN_CORE_OBJ_OBJECT_H
10
11// sen
17#include "sen/core/meta/event.h"
20#include "sen/core/meta/type.h"
21#include "sen/core/meta/var.h"
24
25// std
26#include <cstdint>
27#include <memory>
28#include <string_view>
29
30namespace sen
31{
32class NativeObject;
33} // namespace sen
34
35namespace sen::impl
36{
37class ProxyObject;
38template <typename... T>
39class EventBuffer;
40void writeAllPropertiesToStream(const Object* instance, OutputStream& out);
41} // namespace sen::impl
42
43namespace sen
44{
45
48
50enum class Emit
51{
54};
55
56SEN_STRONG_TYPE(ConnId, uint32_t)
57
58template <>
59struct ShouldBePassedByValue<ConnId>: std::true_type
60{
61};
62
64SEN_STRONG_TYPE(ObjectId, uint32_t)
65
66template <>
67struct ShouldBePassedByValue<ObjectId>: std::true_type
68{
69};
70
71// Forward declarations
72class ClassType;
73
75class SEN_EXPORT Object: public std::enable_shared_from_this<Object>
76{
77 SEN_NOCOPY_NOMOVE(Object)
78
79public:
80 Object() = default;
81 virtual ~Object() = default;
82
83public:
85 [[nodiscard]] virtual const std::string& getName() const noexcept = 0;
86
88 [[nodiscard]] virtual ObjectId getId() const noexcept = 0;
89
91 // [[nodiscard]] virtual const ClassType* getClass() const noexcept = 0;
92 [[nodiscard]] virtual ConstTypeHandle<ClassType> getClass() const noexcept = 0;
93
95 [[nodiscard]] virtual NativeObject* asNativeObject() noexcept { return nullptr; }
96
98 [[nodiscard]] virtual const NativeObject* asNativeObject() const noexcept { return nullptr; }
99
101 [[nodiscard]] virtual impl::ProxyObject* asProxyObject() noexcept { return nullptr; }
102
104 [[nodiscard]] virtual const impl::ProxyObject* asProxyObject() const noexcept { return nullptr; }
105
107 [[nodiscard]] virtual const std::string& getLocalName() const noexcept = 0;
108
110 [[nodiscard]] virtual TimeStamp getLastCommitTime() const noexcept = 0;
111
112public: // Reflection-based interface (do not use for real-time applications)
114 [[nodiscard]] virtual Var getPropertyUntyped(const Property* prop) const = 0;
115
120 virtual void invokeUntyped(const Method* method, const VarList& args, MethodCallback<Var>&& onDone = {}) = 0;
121
126 [[nodiscard]] virtual ConnectionGuard onPropertyChangedUntyped(const Property* prop,
127 EventCallback<VarList>&& callback) = 0;
128
133 [[nodiscard]] virtual ConnectionGuard onEventUntyped(const Event* ev, EventCallback<VarList>&& callback) = 0;
134
138 virtual void invokeAllPropertyCallbacks() = 0;
139
140protected:
141 friend class ConnectionGuard;
142 virtual void senImplRemoveTypedConnection(ConnId id) = 0;
143 virtual void senImplRemoveUntypedConnection(ConnId id, MemberHash memberHash) = 0;
144
145protected:
146 template <typename... T>
147 friend class impl::EventBuffer;
148
149 virtual void senImplEventEmitted(MemberHash id, std::function<VarList()>&& argsGetter, const EventInfo& info) = 0;
150
152 [[nodiscard]] ObjectId senImplMakeId(std::string_view objectName) const;
153
155 [[nodiscard]] ConnectionGuard senImplMakeConnectionGuard(ConnId id, MemberHash member, bool typed);
156
158 static void senImplValidateName(std::string_view name);
159
161 [[nodiscard]] static std::string senImplComputeLocalName(std::string_view name, std::string_view prefix);
162
163protected:
164 friend void impl::writeAllPropertiesToStream(const Object* instance, OutputStream& out);
165
168
171
174};
175
177
178} // namespace sen
179
180SEN_STRONG_TYPE_HASHABLE(::sen::ConnId)
181SEN_STRONG_TYPE_HASHABLE(::sen::ObjectId)
182
183#endif // SEN_CORE_OBJ_OBJECT_H
Represents an event.
Definition core/include/sen/core/meta/event.h:34
Represents a method.
Definition method.h:96
An object instantiated in this process. This is the base class for all user-implemented objects.
Definition native_object.h:63
virtual const std::string & getName() const noexcept=0
The name given to the object upon construction.
virtual void senImplWriteDynamicPropertiesToStream(OutputStream &out) const =0
Writes all dynamic properties to out.
virtual void invokeAllPropertyCallbacks()=0
Helper method that invokes all the registered property callbacks, even if/when the property has not c...
virtual void senImplRemoveUntypedConnection(ConnId id, MemberHash memberHash)=0
virtual ~Object()=default
ObjectId senImplMakeId(std::string_view objectName) const
Creates a unique id.
virtual NativeObject * asNativeObject() noexcept
Helper that checks if the object is local (without dynamic casts).
Definition object.h:95
virtual void senImplRemoveTypedConnection(ConnId id)=0
virtual TimeStamp getLastCommitTime() const noexcept=0
The point in time when the last commit was called.
Object()=default
virtual ConnectionGuard onPropertyChangedUntyped(const Property *prop, EventCallback< VarList > &&callback)=0
Reflection-based interface for detecting property changes.
virtual ConstTypeHandle< ClassType > getClass() const noexcept=0
Reflection information.
friend class ConnectionGuard
Definition object.h:141
virtual ObjectId getId() const noexcept=0
Global unique object identification.
virtual const NativeObject * asNativeObject() const noexcept
Helper that checks if the object is local (without dynamic casts).
Definition object.h:98
virtual ConnectionGuard onEventUntyped(const Event *ev, EventCallback< VarList > &&callback)=0
Reflection-based interface for reacting to events.
virtual void senImplEventEmitted(MemberHash id, std::function< VarList()> &&argsGetter, const EventInfo &info)=0
virtual const std::string & getLocalName() const noexcept=0
An alias used as an alternate way of identifying this object locally.
virtual impl::ProxyObject * asProxyObject() noexcept
Helper that checks if the object is a proxy (without dynamic casts).
Definition object.h:101
virtual void senImplWriteStaticPropertiesToStream(OutputStream &out) const =0
Writes all static properties to out.
virtual void invokeUntyped(const Method *method, const VarList &args, MethodCallback< Var > &&onDone={})=0
Reflection- and variant- based interface for (asynchronously) invoking a method on this object....
ConnectionGuard senImplMakeConnectionGuard(ConnId id, MemberHash member, bool typed)
Creates a guard for a callback.
virtual const impl::ProxyObject * asProxyObject() const noexcept
Helper that checks if the object is a proxy (without dynamic casts).
Definition object.h:104
static void senImplValidateName(std::string_view name)
Throws std::exception if the name is not valid.
virtual void senImplWriteAllPropertiesToStream(OutputStream &out) const =0
Writes all properties to out.
virtual Var getPropertyUntyped(const Property *prop) const =0
Variant-based property getter.
static std::string senImplComputeLocalName(std::string_view name, std::string_view prefix)
The name, with a prefix (if any).
Represents a property.
Definition property.h:79
A point in time.
Definition timestamp.h:26
Emit
How to emit an event.
Definition object.h:51
Callback< MethodCallInfo, MethodResult< R > > MethodCallback
A method callback.
Definition callback.h:212
Callback< EventInfo, Args... > EventCallback
An event callback.
Definition callback.h:208
@ onCommit
When commit is called.
Definition object.h:53
@ now
Directly when it happens.
Definition object.h:52
Information about an event emission.
Definition callback.h:50
std::vector< Var > VarList
A list of vars to represent sequences.
Definition var.h:104
TypeHandle< const T > ConstTypeHandle
Definition type.h:319
Definition assert.h:17
OutputStreamTemplate< LittleEndian > OutputStream
Definition output_stream.h:64
#define SEN_STRONG_TYPE_HASHABLE(name)
Makes your strong type std::hashable. NOLINTNEXTLINE.
Definition strong_type.h:188
#define SEN_STRONG_TYPE(name, native_type)
Helper macro to define strong types. NOLINTNEXTLINE.
Definition strong_type.h:179
The hash of a member.
Definition type.h:39
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34
Can hold any supported value type. Wraps std::variant to allow recursion and implements some helpers.
Definition var.h:119