Add new LocalTime constructor.
[libdcp.git] / src / ref.h
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/ref.h
21  *  @brief Ref class.
22  */
23
24 #ifndef LIBDCP_REF_H
25 #define LIBDCP_REF_H
26
27 #include "exceptions.h"
28 #include "asset.h"
29 #include "util.h"
30 #include <boost/shared_ptr.hpp>
31 #include <string>
32
33 namespace dcp {
34
35 /** @class Ref
36  *  @brief A reference to an asset which is identified by a universally-unique identifier (UUID).
37  *
38  *  This class is a `pointer' to a thing.  It will always know the
39  *  UUID of the thing, and it may have a shared_ptr to the C++ object
40  *  which represents the thing.
41  *
42  *  If the Ref does not have a shared_ptr it may be given one by
43  *  calling resolve() with a list of assets.  The shared_ptr will be
44  *  set up using any object on the list which has a matching ID.
45  */
46 class Ref
47 {
48 public:
49         /** Initialise a Ref with an ID but no shared_ptr */
50         Ref (std::string id)
51                 : _id (id)
52         {}
53
54         /** Initialise a Ref with a shared_ptr to an asset */
55         Ref (boost::shared_ptr<Asset> asset)
56                 : _id (asset->id ())
57                 , _asset (asset)
58         {}
59
60         /** Set the ID of this Ref */
61         void set_id (std::string id)
62         {
63                 _id = id;
64         }
65
66         void resolve (std::list<boost::shared_ptr<Asset> > assets);
67
68         /** @return the ID of the thing that we are pointing to */
69         std::string id () const {
70                 return _id;
71         }
72
73         /** @return a shared_ptr to the thing; an UnresolvedRefError is thrown
74          *  if the shared_ptr is not known.
75          */
76         boost::shared_ptr<Asset> asset () const {
77                 if (!_asset) {
78                         throw UnresolvedRefError (_id);
79                 }
80
81                 return _asset;
82         }
83
84         /** operator-> to access the shared_ptr; an UnresolvedRefError is thrown
85          *  if the shared_ptr is not known.
86          */
87         Asset * operator->() const {
88                 if (!_asset) {
89                         throw UnresolvedRefError (_id);
90                 }
91
92                 return _asset.get ();
93         }
94
95         /** @return true if a shared_ptr is known for this Ref */
96         bool resolved () const {
97                 return static_cast<bool>(_asset);
98         }
99
100 private:
101         std::string _id;             ///< ID; will always be known
102         boost::shared_ptr<Asset> _asset; ///< shared_ptr to the thing, may be null.
103 };
104
105 }
106
107 #endif