Loading...
Searching...
No Matches
IdentifiedObject.hpp
1#pragma once
2#include <score/model/IdentifiedObjectAbstract.hpp>
3#include <score/model/Identifier.hpp>
4#include <score/model/path/Path.hpp>
5
17template <typename model>
19{
20public:
21 static const constexpr bool identified_object_tag = true;
22 using model_type = model;
23 using id_type = Id<model>;
25
26 IdentifiedObject(id_type id, const QString& name, QObject* parent) noexcept
27 : IdentifiedObjectAbstract{name, parent}
28 , m_id{std::move(id)}
29 {
30 m_id.m_ptr = this;
31 }
32
33 template <typename Visitor>
34 IdentifiedObject(Visitor&& v, QObject* parent) noexcept
36 {
37 using vis_type = typename std::remove_reference_t<Visitor>::type;
39 m_id.m_ptr = this;
40 }
41
42 ~IdentifiedObject() override = default;
43
44 const id_type& id() const noexcept { return m_id; }
45
46 int32_t id_val() const noexcept final override { return m_id.val(); }
47
48 void setId(const id_type& id) noexcept
49 {
50 m_id = id;
51 m_path_cache.unsafePath().vec().clear();
52 m_id.m_ptr = this;
53 }
54
55 void setId(id_type&& id) noexcept
56 {
57 m_id = std::move(id);
58 m_path_cache.unsafePath().vec().clear();
59 m_id.m_ptr = this;
60 }
61
62 void resetCache() const noexcept override { m_path_cache.unsafePath().vec().clear(); }
63
64 mutable Path<model> m_path_cache;
65 // TODO see
66 // http://stackoverflow.com/questions/32987869/befriending-of-function-template-with-enable-if
67 // to put in private
68private:
69 id_type m_id{};
70};
71
72template <typename model>
73std::size_t hash_value(const Id<model>& id) noexcept
74{
75 return id.val();
76}
77
78template <typename T, typename U>
79bool operator==(const T* obj, const Id<U>& id) noexcept
80{
81 return obj->id() == id;
82}
83
84template <typename T, typename U, typename = decltype(std::declval<T>().id())>
85bool operator==(const T& obj, const Id<U>& id) noexcept
86{
87 return obj.id() == id;
88}
89
90namespace score
91{
92namespace IDocument
93{
94
103template <typename T>
104Path<T> path(const IdentifiedObject<T>& obj)
105{
106 static_assert(!std::is_pointer<T>::value, "Don't pass a pointer to path");
107 if(obj.m_path_cache.valid())
108 return obj.m_path_cache;
109
110 obj.m_path_cache
111 = Path<T>{score::IDocument::unsafe_path(safe_cast<const QObject&>(obj)), {}};
112 return obj.m_path_cache;
113}
114}
115}
Base class for IdentifiedObject.
Definition IdentifiedObjectAbstract.hpp:16
The IdentifiedObject class.
Definition IdentifiedObject.hpp:19
The Path class is a typesafe wrapper around ObjectPath.
Definition Path.hpp:52
The id_base_t class.
Definition Identifier.hpp:57
Base toolkit upon which the software is built.
Definition Application.cpp:90
Definition VisitorInterface.hpp:13