libdcp
ref.h
Go to the documentation of this file.
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 
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 
65 class Ref
66 {
67 public:
69  explicit Ref (std::string id)
70  : _id (id)
71  {}
72 
74  explicit Ref (std::shared_ptr<Asset> asset)
75  : _id (asset->id ())
76  , _asset (asset)
77  {}
78 
80  void set_id (std::string id)
81  {
82  _id = id;
83  }
84 
88  void resolve (std::vector<std::shared_ptr<Asset>> assets);
89 
91  std::string id () const {
92  return _id;
93  }
94 
98  std::shared_ptr<Asset> asset () const {
99  if (!_asset) {
100  throw UnresolvedRefError (_id);
101  }
102 
103  return _asset;
104  }
105 
109  Asset * operator->() const {
110  if (!_asset) {
111  throw UnresolvedRefError (_id);
112  }
113 
114  return _asset.get ();
115  }
116 
118  bool resolved () const {
119  return static_cast<bool>(_asset);
120  }
121 
122 private:
123  std::string _id;
124  std::shared_ptr<Asset> _asset;
125 };
126 
127 
128 }
129 
130 
131 #endif
Asset class.
Parent class for DCP assets, i.e. picture, sound, subtitles, closed captions, CPLs,...
Definition: asset.h:70
A reference to an asset which is identified by a universally-unique identifier (UUID)
Definition: ref.h:66
Ref(std::shared_ptr< Asset > asset)
Definition: ref.h:74
Ref(std::string id)
Definition: ref.h:69
bool resolved() const
Definition: ref.h:118
Asset * operator->() const
Definition: ref.h:109
std::shared_ptr< Asset > _asset
shared_ptr to the thing, may be null.
Definition: ref.h:124
std::shared_ptr< Asset > asset() const
Definition: ref.h:98
std::string _id
ID; will always be known.
Definition: ref.h:123
std::string id() const
Definition: ref.h:91
void set_id(std::string id)
Definition: ref.h:80
void resolve(std::vector< std::shared_ptr< Asset >> assets)
Definition: ref.cc:49
An exception caused by a reference (by UUID) to something which is not known.
Definition: exceptions.h:176
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
Utility methods and classes.