CurveSegmentData.hpp
1 #pragma once
2 #include <Curve/Palette/CurvePoint.hpp>
3 
4 #include <score/model/IdentifiedObject.hpp>
5 #include <score/plugins/UuidKey.hpp>
6 #include <score/serialization/DataStreamFwd.hpp>
7 #include <score/tools/IdentifierGeneration.hpp>
8 
9 #include <ossia/detail/algorithms.hpp>
10 
11 #include <QVariant>
12 
13 namespace Curve
14 {
15 class SegmentFactory;
16 class SegmentModel;
17 class Category_k;
18 struct SegmentData;
19 /* TODO it would maybe faster to have them on the heap and use QPointer for
20  * caching ...
21 class SegmentId
22 {
23  public:
24  using value_type = std::optional<int32_t>;
25  explicit SegmentId() = default;
26  explicit SegmentId(value_type val) : m_id {val} { }
27  SegmentId(const SegmentId&) = delete;
28  SegmentId(SegmentId&& ) = delete;
29  SegmentId& operator=(const SegmentId&) = delete;
30  SegmentId& operator=(SegmentId&& ) = delete;
31 
32  friend bool operator== (const id_base_t& lhs, const id_base_t& rhs)
33  { return lhs.m_id == rhs.m_id; }
34 
35  friend bool operator!= (const id_base_t& lhs, const id_base_t& rhs)
36  { return lhs.m_id != rhs.m_id; }
37 
38  friend bool operator< (const id_base_t& lhs, const id_base_t& rhs)
39  { return *lhs.val() < *rhs.val(); }
40 
41  explicit operator bool() const
42  { return bool(m_id); }
43 
44  explicit operator value_type() const
45  { return m_id; }
46 
47  const value_type& val() const
48  { return m_id; }
49 
50  void setVal(value_type&& val)
51  { m_id = val; }
52 
53  void unset()
54  { m_id = value_type(); }
55 
56  private:
57  mutable CurveSegmentData* m_ptr{};
58  value_type m_id {};
59 };*/
60 
61 // An object wrapper useful for saving / loading
63 {
64  SegmentData() = default;
65  SegmentData(const SegmentData&) = default;
66  SegmentData(SegmentData&&) = default;
67  SegmentData& operator=(const SegmentData&) = default;
68  SegmentData& operator=(SegmentData&&) = default;
69 
71  Id<SegmentModel> i, Curve::Point s, Curve::Point e, OptionalId<SegmentModel> prev,
72  OptionalId<SegmentModel> foll, const UuidKey<Curve::SegmentFactory>& t,
73  QVariant data)
74  : id(std::move(i))
75  , start(s)
76  , end(e)
77  , previous(std::move(prev))
78  , following(std::move(foll))
79  , type(t)
80  , specificSegmentData(std::move(data))
81  {
82  }
83 
85 
86  Curve::Point start, end;
87  OptionalId<SegmentModel> previous, following;
88 
90  QVariant specificSegmentData;
91 
92  double x() const { return start.x(); }
93 };
94 
95 inline bool operator<(const SegmentData& lhs, const SegmentData& rhs)
96 {
97  return lhs.x() < rhs.x();
98 }
99 
100 inline bool operator<=(const SegmentData& lhs, const SegmentData& rhs)
101 {
102  return lhs.x() <= rhs.x();
103 }
104 
105 // REFACTORME with SettableIdentifierGeneration...
106 template <typename Container>
107 Id<SegmentModel> getSegmentId(const Container& ids)
108 {
109  Id<SegmentModel> id{};
110  auto end = ids.end();
111  do
112  {
114  } while(ids.find(id) != end);
115 
116  return id;
117 }
118 
119 inline Id<SegmentModel> getSegmentId(const std::vector<SegmentData>& ids)
120 {
121  Id<SegmentModel> id{};
122 
123  auto end = ids.end();
124  do
125  {
127  } while(ossia::find_if(ids, [&](const auto& other) { return other.id == id; }) != end);
128 
129  return id;
130 }
131 
132 inline Id<SegmentModel> getSegmentId(const std::vector<Id<SegmentModel>>& ids)
133 {
134  Id<SegmentModel> id{};
135 
136  auto end = ids.end();
137  do
138  {
140  } while(ossia::find_if(ids, [&](const auto& other) { return other == id; }) != end);
141 
142  return id;
143 }
144 
145 // We don't want crashes on invalid ids search
147 {
148 public:
149  std::size_t operator()(const Id<SegmentModel>& id) const { return id.val(); }
150 };
151 
152 enum Segments
153 {
154  Hashed
155 };
156 }
157 
158 SCORE_SERIALIZE_DATASTREAM_DECLARE(, Curve::SegmentData)
159 Q_DECLARE_METATYPE(Curve::SegmentData)
160 W_REGISTER_ARGTYPE(Curve::SegmentData)
161 
162 #define CURVE_SEGMENT_FACTORY_METADATA(Export, Model, Uuid) \
163  template <> \
164  struct Export Metadata<ConcreteKey_k, Model> \
165  { \
166  static const auto& get() \
167  { \
168  static const UuidKey<Curve::SegmentFactory> k{Uuid}; \
169  return k; \
170  } \
171  };
172 
173 #define CURVE_SEGMENT_METADATA(Export, Model, Uuid, ObjectKey, PrettyName, Category) \
174  OBJECTKEY_METADATA(Export, Model, ObjectKey) \
175  CURVE_SEGMENT_FACTORY_METADATA(Export, Model, Uuid) \
176  template <> \
177  struct Export Metadata<PrettyName_k, Model> \
178  { \
179  static auto get() { return QObject::tr(PrettyName); } \
180  }; \
181  template <> \
182  struct Export Metadata<Curve::Category_k, Model> \
183  { \
184  static auto get() { return QObject::tr(Category); } \
185  };
Metadata to categorize objects: curves, audio, etc.
Definition: lib/score/tools/Metadata.hpp:61
Definition: CurveSegmentData.hpp:147
The id_base_t class.
Definition: Identifier.hpp:57
Utilities and base classes for 1D curves.
Definition: FocusDispatcher.hpp:12
Definition: CurveSegmentData.hpp:63
static int32_t getRandomId()
getNextId
Definition: IdentifierGeneration.cpp:37