std::shared_ptr
[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/thread/mutex.hpp>
28
29 class Content;
30 class Film;
31
32 class ContentPart
33 {
34 public:
35         explicit ContentPart (Content* parent)
36                 : _parent (parent)
37         {}
38
39 protected:
40         template <class T>
41         void
42         maybe_set (T& member, T new_value, int property) const
43         {
44                 ChangeSignaller<Content> cc (_parent, property);
45                 {
46                         boost::mutex::scoped_lock lm (_mutex);
47                         if (member == new_value) {
48                                 cc.abort ();
49                                 return;
50                         }
51                         member = new_value;
52                 }
53         }
54
55         template <class T>
56         void
57         maybe_set (boost::optional<T>& member, T new_value, int property) const
58         {
59                 ChangeSignaller<Content> cc (_parent, property);
60                 {
61                         boost::mutex::scoped_lock lm (_mutex);
62                         if (member && member.get() == new_value) {
63                                 cc.abort ();
64                                 return;
65                         }
66                         member = new_value;
67                 }
68         }
69
70         Content* _parent;
71         mutable boost::mutex _mutex;
72 };
73
74 #endif