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 
17 template <typename model>
19 {
20 public:
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
35  : IdentifiedObjectAbstract{parent}
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
68 private:
69  id_type m_id{};
70 };
71 
72 template <typename model>
73 std::size_t hash_value(const Id<model>& id) noexcept
74 {
75  return id.val();
76 }
77 
78 template <typename T, typename U>
79 bool operator==(const T* obj, const Id<U>& id) noexcept
80 {
81  return obj->id() == id;
82 }
83 
84 template <typename T, typename U, typename = decltype(std::declval<T>().id())>
85 bool operator==(const T& obj, const Id<U>& id) noexcept
86 {
87  return obj.id() == id;
88 }
89 
90 namespace score
91 {
92 namespace IDocument
93 {
94 
103 template <typename T>
104 Path<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 id_base_t class.
Definition: Identifier.hpp:57
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: VisitorInterface.hpp:13