Loading...
Searching...
No Matches
ObjectPath.hpp
1#pragma once
2#include <score/model/path/ObjectIdentifier.hpp>
3#include <score/tools/SafeCast.hpp>
4
5#include <QObject>
6#include <QPointer>
7#include <QString>
8
9#include <score_lib_base_export.h>
10
11#include <initializer_list>
12#include <type_traits>
13#include <vector>
14namespace score
15{
16struct DocumentContext;
17}
18class QObject;
19
36class SCORE_LIB_BASE_EXPORT ObjectPath
37{
38 friend ObjectIdentifierVector::iterator begin(ObjectPath& path) noexcept
39 {
40 return path.m_objectIdentifiers.begin();
41 }
42
43 friend ObjectIdentifierVector::iterator end(ObjectPath& path) noexcept
44 {
45 return path.m_objectIdentifiers.end();
46 }
47
48 friend bool operator==(const ObjectPath& lhs, const ObjectPath& rhs) noexcept
49 {
50 return lhs.m_objectIdentifiers == rhs.m_objectIdentifiers;
51 }
52
53 friend bool operator!=(const ObjectPath& lhs, const ObjectPath& rhs) noexcept
54 {
55 return lhs.m_objectIdentifiers != rhs.m_objectIdentifiers;
56 }
57
58public:
59 ObjectPath() noexcept { }
60 ~ObjectPath() noexcept = default;
61 QString toString() const noexcept;
62 static ObjectPath fromString(const QString& str) noexcept;
63 QObject* findObject(const score::DocumentContext& ctx) const noexcept;
64
65 explicit ObjectPath(std::vector<ObjectIdentifier> vec) noexcept
66 : m_objectIdentifiers{std::move(vec)}
67 {
68 }
69
70 ObjectPath(std::initializer_list<ObjectIdentifier> lst) noexcept
71 : m_objectIdentifiers(lst)
72 {
73 }
74
75 ObjectPath(const ObjectPath& obj) noexcept
76 : m_objectIdentifiers{obj.m_objectIdentifiers}
77 {
78 }
79
80 ObjectPath(ObjectPath&& obj) noexcept
81 : m_objectIdentifiers{std::move(obj.m_objectIdentifiers)}
82 {
83 }
84
85 ObjectPath& operator=(ObjectPath&& obj) noexcept
86 {
87 m_objectIdentifiers = std::move(obj.m_objectIdentifiers);
88 m_cache.clear();
89 return *this;
90 }
91
92 ObjectPath& operator=(const ObjectPath& obj) noexcept
93 {
94 m_objectIdentifiers = obj.m_objectIdentifiers;
95 m_cache.clear();
96 return *this;
97 }
98
99 static ObjectPath
100 pathBetweenObjects(const QObject* const parent_obj, const QObject* target_object);
101
111 template <class T>
112 T& find(const score::DocumentContext& ctx) const
113 {
114 // First see if the pointer is still loaded in the cache.
115 if(!m_cache.isNull())
116 {
117 return *safe_cast<T*>(m_cache.data());
118 }
119 else // Load it by hand
120 {
121 auto ptr = safe_cast<typename std::remove_const<T>::type*>(find_impl(ctx));
122 m_cache = ptr;
123 return *ptr;
124 }
125 }
126
132 template <class T>
133 T* try_find(const score::DocumentContext& ctx) const noexcept
134 {
135 try
136 {
137 if(!m_cache.isNull())
138 {
139 return safe_cast<T*>(m_cache.data());
140 }
141 else // Load it by hand
142 {
143 auto ptr
144 = static_cast<typename std::remove_const<T>::type*>(find_impl_unsafe(ctx));
145 m_cache = ptr;
146 return ptr;
147 }
148 }
149 catch(...)
150 {
151 return nullptr;
152 }
153 }
154
155 const ObjectIdentifierVector& vec() const noexcept { return m_objectIdentifiers; }
156
157 ObjectIdentifierVector& vec() noexcept { return m_objectIdentifiers; }
158
159 void resetCache() const noexcept { m_cache = {}; }
160
161private:
162 // Throws
163 QObject* find_impl(const score::DocumentContext& ctx) const;
164
165 // Returns nullptr
166 QObject* find_impl_unsafe(const score::DocumentContext& ctx) const noexcept;
167
168 ObjectIdentifierVector m_objectIdentifiers;
169 mutable QPointer<QObject> m_cache;
170};
171
172SCORE_LIB_BASE_EXPORT void
173replacePathPart(const ObjectPath& src, const ObjectPath& target, ObjectPath& toChange);
174inline uint qHash(const ObjectPath& obj, uint seed)
175{
176 return qHash(obj.toString(), seed);
177}
178
179namespace std
180{
181template <>
182struct SCORE_LIB_BASE_EXPORT hash<ObjectIdentifier>
183{
184 std::size_t operator()(const ObjectIdentifier& path) const;
185};
186template <>
187struct SCORE_LIB_BASE_EXPORT hash<ObjectPath>
188{
189 std::size_t operator()(const ObjectPath& path) const;
190};
191}
The ObjectIdentifier class.
Definition ObjectIdentifier.hpp:21
The ObjectPath class.
Definition ObjectPath.hpp:37
T * try_find(const score::DocumentContext &ctx) const noexcept
Tries to find an object.
Definition ObjectPath.hpp:133
T & find(const score::DocumentContext &ctx) const
find the object described by the ObjectPath
Definition ObjectPath.hpp:112
Base toolkit upon which the software is built.
Definition Application.cpp:113
STL namespace.
Definition DocumentContext.hpp:18