TimeValueSerialization.hpp
1 #pragma once
2 #include <Process/TimeValue.hpp>
3 
5 #include <score/serialization/IsTemplate.hpp>
6 #include <score/serialization/JSONVisitor.hpp>
7 
8 #include <QDebug>
9 
10 inline QDebug operator<<(QDebug d, const TimeVal& tv)
11 {
12  if(!tv.infinite())
13  {
14  d << tv.msec() << "ms";
15  }
16  else
17  {
18  d << "infinite";
19  }
20 
21  return d;
22 }
23 
24 template <>
26 {
27  static void readFrom(DataStream::Serializer& s, const TimeVal& tv)
28  {
29  s.stream() << tv.impl;
30  }
31 
32  static void writeTo(DataStream::Deserializer& s, TimeVal& tv)
33  {
34  s.stream() >> tv.impl;
35  }
36 };
37 
38 template <>
40 {
41  static void readFrom(JSONObject::Serializer& s, const TimeVal& tv)
42  {
43  if(Q_UNLIKELY(tv.impl > ossia::time_value::infinite_min))
44  {
45  s.stream.Int64(ossia::time_value::infinity);
46  }
47  else if(Q_UNLIKELY(tv.impl < 0))
48  {
49  qDebug() << "Warning: saving a time_value < 0. This likely indicates a bug: "
50  << tv.impl;
51  s.stream.Int64(0);
52  }
53  else
54  {
55  s.stream.Int64(tv.impl);
56  }
57  }
58 
59  static void writeTo(JSONObject::Deserializer& s, TimeVal& tv)
60  {
61  using namespace std;
62  using namespace std::literals;
63  if(Q_LIKELY(s.base.IsInt64()))
64  {
65  tv.impl = s.base.GetInt64();
66  }
67  else if(s.base.IsUint64())
68  {
69  // Note: there is likely a rapidjson bug there...
70  qDebug() << "Warning: loading a value > to the maximum of an int64_t: "
71  << s.base.GetUint64();
72  tv.impl = ossia::time_value::infinity;
73  }
74  /*
75  else if (s.base.IsDouble())
76  {
77  // Assuming old file format, which was in milliseconds
78  tv = TimeVal::fromMsecs(s.base.GetDouble());
79  }
80  */
81  else
82  {
83  qDebug() << "Warning: could not load a TimeVal";
84  tv.impl = 0;
85  }
86  }
87 };
Definition: VisitorInterface.hpp:53
Definition: DataStreamVisitor.hpp:27
Definition: DataStreamVisitor.hpp:202
Definition: VisitorInterface.hpp:61
Definition: JSONVisitor.hpp:52
Definition: JSONVisitor.hpp:423
Definition: VisitorInterface.hpp:13
Definition: TimeValue.hpp:21