1 /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
2  *
3  *  This program is free software: you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation, either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  *  If you would like to incorporate Link into a proprietary software application,
17  *  please contact <link-devs@ableton.com>.
18  */
19 
20 #pragma once
21 
22 #include <ableton/discovery/Payload.hpp>
23 #include <ableton/link/NodeId.hpp>
24 #include <ableton/link/SessionId.hpp>
25 #include <ableton/link/StartStopState.hpp>
26 #include <ableton/link/Timeline.hpp>
27 
28 namespace ableton
29 {
30 namespace link
31 {
32 
33 struct NodeState
34 {
35   using Payload =
36     decltype(discovery::makePayload(Timeline{}, SessionMembership{}, StartStopState{}));
37 
38   NodeId ident() const
39   {
40     return nodeId;
41   }
42 
43   friend bool operator==(const NodeState& lhs, const NodeState& rhs)
44   {
45     return std::tie(lhs.nodeId, lhs.sessionId, lhs.timeline, lhs.startStopState)
46            == std::tie(rhs.nodeId, rhs.sessionId, rhs.timeline, rhs.startStopState);
47   }
48 
49   friend Payload toPayload(const NodeState& state)
50   {
51     return discovery::makePayload(
52       state.timeline, SessionMembership{state.sessionId}, state.startStopState);
53   }
54 
55   template <typename It>
56   static NodeState fromPayload(NodeId nodeId, It begin, It end)
57   {
58     using namespace std;
59     auto nodeState = NodeState{move(nodeId), {}, {}, {}};
60     discovery::parsePayload<Timeline, SessionMembership, StartStopState>(move(begin),
61       move(end), [&nodeState](Timeline tl) { nodeState.timeline = move(tl); },
62       [&nodeState](SessionMembership membership) {
63         nodeState.sessionId = move(membership.sessionId);
64       },
65       [&nodeState](StartStopState ststst) { nodeState.startStopState = move(ststst); });
66     return nodeState;
67   }
68 
69   NodeId nodeId;
70   SessionId sessionId;
71   Timeline timeline;
72   StartStopState startStopState;
73 };
74 
75 } // namespace link
76 } // namespace ableton
77