Another macOS std::list boost::thread SNAFU.
[dcpomatic.git] / src / lib / content_part.h
1
2 /*
3     Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
4
5     This file is part of DCP-o-matic.
6
7     DCP-o-matic is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     DCP-o-matic is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #ifndef DCPOMATIC_CONTENT_PART_H
23 #define DCPOMATIC_CONTENT_PART_H
24
25 #include "content.h"
26 #include "change_signaller.h"
27 #include <boost/weak_ptr.hpp>
28 #include <boost/thread/mutex.hpp>
29
30 class Content;
31 class Film;
32
33 class ContentPart
34 {
35 public:
36         explicit ContentPart (Content* parent)
37                 : _parent (parent)
38         {}
39
40 protected:
41         template <class T>
42         void
43         maybe_set (T& member, T new_value, int property) const
44         {
45                 ChangeSignaller<Content> cc (_parent, property);
46                 {
47                         boost::mutex::scoped_lock lm (_mutex);
48                         if (member == new_value) {
49                                 cc.abort ();
50                                 return;
51                         }
52                         member = new_value;
53                 }
54         }
55
56         template <class T>
57         void
58         maybe_set (boost::optional<T>& member, T new_value, int property) const
59         {
60                 ChangeSignaller<Content> cc (_parent, property);
61                 {
62                         boost::mutex::scoped_lock lm (_mutex);
63                         if (member && member.get() == new_value) {
64                                 cc.abort ();
65                                 return;
66                         }
67                         member = new_value;
68                 }
69         }
70
71         Content* _parent;
72         mutable boost::mutex _mutex;
73 };
74
75 #endif