SetMaxDuration.hpp
1 #pragma once
2 #include <Process/TimeValue.hpp>
3 #include <Process/TimeValueSerialization.hpp>
4 
5 #include <Scenario/Commands/ScenarioCommandFactory.hpp>
6 #include <Scenario/Document/Interval/IntervalModel.hpp>
7 
8 #include <score/command/Command.hpp>
9 #include <score/model/path/Path.hpp>
10 
11 #include <tests/helpers/ForwardDeclaration.hpp>
12 
13 namespace Scenario
14 {
15 class IntervalModel;
16 namespace Command
17 {
23 class SetMaxDuration final : public score::Command
24 {
25  SCORE_COMMAND_DECL(CommandFactoryName(), SetMaxDuration, "Set interval maximum")
26 public:
27  static const constexpr auto corresponding_member
28  = &IntervalDurations::maxDuration; // used by state machine
29  // (MoveState.hpp)
30 
31  SetMaxDuration(const IntervalModel& cst, TimeVal newval, bool isInfinite)
32  : m_path{cst}
33  , m_oldVal{cst.duration.maxDuration()}
34  , m_newVal{std::move(newval)}
35  , m_newInfinite{isInfinite}
36  , m_oldInfinite{cst.duration.isMaxInfinite()}
37  {
38  }
39 
40  void update(const IntervalModel& cst, const TimeVal& newval, bool isInfinite)
41  {
42  m_newVal = newval;
43  auto& cstrDuration = cst.duration;
44  if(m_newVal < cstrDuration.defaultDuration())
45  m_newVal = cstrDuration.defaultDuration();
46  }
47 
48  void undo(const score::DocumentContext& ctx) const override
49  {
50  m_path.find(ctx).duration.setMaxInfinite(m_oldInfinite);
51  m_path.find(ctx).duration.setMaxDuration(m_oldVal);
52  }
53 
54  void redo(const score::DocumentContext& ctx) const override
55  {
56  m_path.find(ctx).duration.setMaxInfinite(m_newInfinite);
57  m_path.find(ctx).duration.setMaxDuration(m_newVal);
58  }
59 
60 protected:
61  void serializeImpl(DataStreamInput& s) const override
62  {
63  s << m_path << m_oldVal << m_newVal << m_newInfinite << m_oldInfinite;
64  }
65  void deserializeImpl(DataStreamOutput& s) override
66  {
67  s >> m_path >> m_oldVal >> m_newVal >> m_newInfinite >> m_oldInfinite;
68  }
69 
70 private:
71  Path<IntervalModel> m_path;
72 
73  TimeVal m_oldVal;
74  TimeVal m_newVal;
75  bool m_newInfinite{};
76  bool m_oldInfinite{};
77 };
78 }
79 }
The Path class is a typesafe wrapper around ObjectPath.
Definition: Path.hpp:52
The SetMaxDuration class.
Definition: SetMaxDuration.hpp:24
Definition: IntervalModel.hpp:50
The Command class.
Definition: Command.hpp:34
Main plug-in of score.
Definition: score-plugin-dataflow/Dataflow/PortItem.hpp:14
Definition: DataStreamHelpers.hpp:99
Definition: DataStreamHelpers.hpp:103
Definition: TimeValue.hpp:21
Definition: DocumentContext.hpp:18