Bv2.1 7.2.3: Check that subtitle <StartTime> exists and is 0.
[libdcp.git] / src / ref.h
1 /*
2     Copyright (C) 2014 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 /** @file  src/ref.h
35  *  @brief Ref class.
36  */
37
38 #ifndef LIBDCP_REF_H
39 #define LIBDCP_REF_H
40
41 #include "exceptions.h"
42 #include "asset.h"
43 #include "util.h"
44 #include <memory>
45 #include <string>
46
47 namespace dcp {
48
49 /** @class Ref
50  *  @brief A reference to an asset which is identified by a universally-unique identifier (UUID).
51  *
52  *  This class is a `pointer' to a thing.  It will always know the
53  *  UUID of the thing, and it may have a shared_ptr to the C++ object
54  *  which represents the thing.
55  *
56  *  If the Ref does not have a shared_ptr it may be given one by
57  *  calling resolve() with a list of assets.  The shared_ptr will be
58  *  set up using any object on the list which has a matching ID.
59  */
60 class Ref
61 {
62 public:
63         /** Initialise a Ref with an ID but no shared_ptr */
64         explicit Ref (std::string id)
65                 : _id (id)
66         {}
67
68         /** Initialise a Ref with a shared_ptr to an asset */
69         explicit Ref (std::shared_ptr<Asset> asset)
70                 : _id (asset->id ())
71                 , _asset (asset)
72         {}
73
74         /** Set the ID of this Ref */
75         void set_id (std::string id)
76         {
77                 _id = id;
78         }
79
80         void resolve (std::list<std::shared_ptr<Asset> > assets);
81
82         /** @return the ID of the thing that we are pointing to */
83         std::string id () const {
84                 return _id;
85         }
86
87         /** @return a shared_ptr to the thing; an UnresolvedRefError is thrown
88          *  if the shared_ptr is not known.
89          */
90         std::shared_ptr<Asset> asset () const {
91                 if (!_asset) {
92                         throw UnresolvedRefError (_id);
93                 }
94
95                 return _asset;
96         }
97
98         /** operator-> to access the shared_ptr; an UnresolvedRefError is thrown
99          *  if the shared_ptr is not known.
100          */
101         Asset * operator->() const {
102                 if (!_asset) {
103                         throw UnresolvedRefError (_id);
104                 }
105
106                 return _asset.get ();
107         }
108
109         /** @return true if a shared_ptr is known for this Ref */
110         bool resolved () const {
111                 return static_cast<bool>(_asset);
112         }
113
114 private:
115         std::string _id;             ///< ID; will always be known
116         std::shared_ptr<Asset> _asset; ///< shared_ptr to the thing, may be null.
117 };
118
119 }
120
121 #endif