Tidying.
[libdcp.git] / src / reel_markers_asset.cc
1 /*
2     Copyright (C) 2019-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 #include "reel_markers_asset.h"
36 #include "raw_convert.h"
37 #include "dcp_assert.h"
38 #include <libxml++/libxml++.h>
39
40
41 using std::string;
42 using std::map;
43 using std::max;
44 using boost::optional;
45 using std::shared_ptr;
46 using namespace dcp;
47
48
49 ReelMarkersAsset::ReelMarkersAsset (Fraction edit_rate, int64_t intrinsic_duration, int64_t entry_point)
50         : ReelAsset (make_uuid(), edit_rate, intrinsic_duration, entry_point)
51 {
52
53 }
54
55
56 ReelMarkersAsset::ReelMarkersAsset (cxml::ConstNodePtr node)
57         : ReelAsset (node)
58 {
59         auto list = node->node_child ("MarkerList");
60         DCP_ASSERT (list);
61         for (auto i: list->node_children("Marker")) {
62                 set (marker_from_string(i->string_child("Label")), dcp::Time(i->number_child<int64_t>("Offset"), edit_rate().as_float(), edit_rate().numerator));
63         }
64 }
65
66
67 string
68 ReelMarkersAsset::cpl_node_name (Standard) const
69 {
70         return "MainMarkers";
71 }
72
73
74 void
75 ReelMarkersAsset::set (Marker m, Time t)
76 {
77         _markers[m] = t;
78 }
79
80
81 void
82 ReelMarkersAsset::unset (Marker m)
83 {
84         _markers.erase (m);
85 }
86
87
88 optional<Time>
89 ReelMarkersAsset::get (Marker m) const
90 {
91         map<Marker, Time>::const_iterator i = _markers.find (m);
92         if (i == _markers.end ()) {
93                 return optional<Time>();
94         }
95         return i->second;
96 }
97
98
99 xmlpp::Node*
100 ReelMarkersAsset::write_to_cpl (xmlpp::Node* node, Standard standard) const
101 {
102         int const tcr = edit_rate().numerator / edit_rate().denominator;
103         auto asset = write_to_cpl_asset (node, standard, optional<string>());
104         auto ml = asset->add_child("MarkerList");
105         for (auto const& i: _markers) {
106                 auto m = ml->add_child("Marker");
107                 m->add_child("Label")->add_child_text(marker_to_string(i.first));
108                 m->add_child("Offset")->add_child_text(raw_convert<string>(i.second.as_editable_units(tcr)));
109         }
110
111         return asset;
112 }
113
114 bool
115 ReelMarkersAsset::equals (shared_ptr<const ReelMarkersAsset> other, EqualityOptions opt, NoteHandler note) const
116 {
117         if (!asset_equals(other, opt, note)) {
118                 return false;
119         }
120
121         if (get(Marker::FFOC) != other->get(Marker::FFOC) ||
122             get(Marker::LFOC) != other->get(Marker::LFOC) ||
123             get(Marker::FFTC) != other->get(Marker::FFTC) ||
124             get(Marker::LFTC) != other->get(Marker::LFTC) ||
125             get(Marker::FFOI) != other->get(Marker::FFOI) ||
126             get(Marker::LFOI) != other->get(Marker::LFOI) ||
127             get(Marker::FFEC) != other->get(Marker::FFEC) ||
128             get(Marker::LFEC) != other->get(Marker::LFEC) ||
129             get(Marker::FFMC) != other->get(Marker::FFMC) ||
130             get(Marker::LFMC) != other->get(Marker::LFMC)) {
131                 return false;
132         }
133
134         return true;
135 }