C++11 tidying.
[dcpomatic.git] / src / lib / empty.cc
1 /*
2     Copyright (C) 2017-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "empty.h"
23 #include "film.h"
24 #include "playlist.h"
25 #include "content.h"
26 #include "content_part.h"
27 #include "dcp_content.h"
28 #include "dcpomatic_time_coalesce.h"
29 #include "piece.h"
30 #include <iostream>
31
32
33 using std::cout;
34 using std::list;
35 using std::shared_ptr;
36 using std::dynamic_pointer_cast;
37 using std::function;
38 using namespace dcpomatic;
39
40
41 Empty::Empty (shared_ptr<const Film> film, shared_ptr<const Playlist> playlist, function<bool (shared_ptr<const Content>)> part, DCPTime length)
42 {
43         list<DCPTimePeriod> full;
44         for (auto i: playlist->content()) {
45                 if (part(i)) {
46                         full.push_back (DCPTimePeriod(i->position(), i->end(film)));
47                 }
48         }
49
50         _periods = subtract (DCPTimePeriod(DCPTime(), length), coalesce(full));
51
52         if (!_periods.empty()) {
53                 _position = _periods.front().from;
54         }
55 }
56
57
58 void
59 Empty::set_position (DCPTime position)
60 {
61         _position = position;
62
63         for (auto i: _periods) {
64                 if (i.contains(_position)) {
65                         return;
66                 }
67         }
68
69         for (auto i: _periods) {
70                 if (i.from > _position) {
71                         _position = i.from;
72                         return;
73                 }
74         }
75 }
76
77
78 DCPTimePeriod
79 Empty::period_at_position () const
80 {
81         for (auto i: _periods) {
82                 if (i.contains(_position)) {
83                         return DCPTimePeriod (_position, i.to);
84                 }
85         }
86
87         DCPOMATIC_ASSERT (false);
88 }
89
90
91 bool
92 Empty::done () const
93 {
94         DCPTime latest;
95         for (auto i: _periods) {
96                 latest = max (latest, i.to);
97         }
98
99         return _position >= latest;
100 }