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