No-op: whitespace.
[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 "object.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 object 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 objects.  The shared_ptr will be
44  *  set up using any object on the list which has a matching ID.
45  */
46 template<class T>
47 class Ref
48 {
49 public:
50         /** Initialise a Ref with an ID but no shared_ptr */
51         Ref (std::string id)
52                 : _id (id)
53         {}
54
55         /** Initialise a Ref with a shared_ptr to an object */
56         Ref (boost::shared_ptr<T> object)
57                 : _id (object->id ())
58                 , _object (object)
59         {}
60
61         /** Set the ID of this Ref */
62         void set_id (std::string id)
63         {
64                 _id = id;
65         }
66
67         /** Look through a list of objects and copy a shared_ptr to any object
68          *  which matches the ID of this one.
69          */
70         void resolve (std::list<boost::shared_ptr<Object> > objects)
71         {
72                 typename std::list<boost::shared_ptr<Object> >::iterator i = objects.begin();
73                 while (i != objects.end() && !ids_equal ((*i)->id(), _id)) {
74                         ++i;
75                 }
76
77                 if (i != objects.end ()) {
78                         _object = boost::dynamic_pointer_cast<T> (*i);
79                 }
80         }
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         boost::shared_ptr<T> object () const {
91                 if (!_object) {
92                         throw UnresolvedRefError (_id);
93                 }
94
95                 return _object;
96         }
97
98         /** operator-> to access the shared_ptr; an UnresolvedRefError is thrown
99          *  if the shared_ptr is not known.
100          */
101         T * operator->() const {
102                 if (!_object) {
103                         throw UnresolvedRefError (_id);
104                 }
105
106                 return _object.get ();
107         }
108
109         /** @return true if a shared_ptr is known for this Ref */
110         bool resolved () const {
111                 return _object;
112         }
113
114 private:
115         std::string _id;              ///< ID; will always be known
116         boost::shared_ptr<T> _object; ///< shared_ptr to the thing, may be null.
117 };
118
119 }
120
121 #endif