DUDS
Distributed Update of Data from Something
SomethingRef.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
14 #include <boost/uuid/uuid.hpp>
15 #include <boost/uuid/nil_generator.hpp>
16 //#include <duds/general/LanguageTaggedString.hpp>
17 //#include <duds/GenericValue.hpp>
18 #include <memory>
19 
20 namespace duds {
21 
22 using std::shared_ptr;
23 using std::weak_ptr;
24 
38 template <class ST = Something>
43  boost::uuids::uuid someId;
47  weak_ptr<ST> wp;
48 public:
52  SomethingWeakRef() = default;
56  SomethingWeakRef(const SomethingRef &) = default;
63  SomethingWeakRef(const weak_ptr<ST> &s) {
64  shared_ptr<ST> sp = s.lock();
65  if (!sp) {
66  throw std::runtime_error(); // FIX THIS!!!!
67  }
68  someId = sp->uuid();
69  wp = s;
70  }
77  SomethingWeakRef(const shared_ptr<ST> &s) {
78  if (!s) {
79  throw std::runtime_error(); // FIX THIS!!!!
80  }
81  someId = s->uuid();
82  wp = s;
83  }
87  const boost::uuids::uuid &uuid() const {
88  return someId;
89  }
93  const weak_ptr<ST> weak() const {
94  return wp;
95  }
99  bool expired() const noexcept {
100  return wp.expired();
101  }
107  shared_ptr<ST> lock() const noexcept {
108  return wp.lock();
109  }
115  void reset() noexcept {
116  someId = boost::uuids::nil_uuid();
117  wp.reset();
118  }
122  template <class S>
123  bool operator < (const S &s) const {
124  return someId < s.uuid();
125  }
129  template <class S>
130  bool operator > (const S &s) const {
131  return someId > s.uuid();
132  }
136  template <class S>
137  bool operator <= (const S &s) const {
138  return someId <= s.uuid();
139  }
143  template <class S>
144  bool operator >= (const S &s) const {
145  return someId >= s.uuid();
146  }
150  template <class S>
151  bool operator == (const S &s) const {
152  return someId == s.uuid();
153  }
157  template <class S>
158  bool operator != (const S &s) const {
159  return someId != s.uuid();
160  }
161 };
162 
175 template <class ST = Something>
180  shared_ptr<ST> sp;
181 public:
185  SomethingRef() = default;
189  SomethingRef(const SomethingRef &) = default;
196  SomethingRef(const shared_ptr<ST> &s) {
197  if (!s) {
198  throw std::runtime_error(); // FIX THIS!!!!
199  }
200  sp = s;
201  }
206  const boost::uuids::uuid &uuid() const {
207  return sp->uuid();
208  }
212  const shared_ptr<ST> shared() const {
213  return sp;
214  }
218  ST *get() const {
219  return sp.get();
220  }
224  bool operator bool() const noexcept {
225  return sp;
226  }
232  void reset() noexcept {
233  someId = boost::uuids::nil_uuid();
234  sp.reset();
235  }
240  template <class S>
241  bool operator < (const S &s) const {
242  return sp->uuid() < s.uuid();
243  }
248  template <class S>
249  bool operator > (const S &s) const {
250  return sp->uuid() > s.uuid();
251  }
256  template <class S>
257  bool operator <= (const S &s) const {
258  return sp->uuid() <= s.uuid();
259  }
264  template <class S>
265  bool operator >= (const S &s) const {
266  return sp->uuid() >= s.uuid();
267  }
272  template <class S>
273  bool operator == (const S &s) const {
274  return sp->uuid() == s.uuid();
275  }
280  template <class S>
281  bool operator != (const S &s) const {
282  return sp->uuid() != s.uuid();
283  }
284 };
285 
294 template<class C>
296  bool operator() (const std::shared_ptr<C> &l,
297  const std::shared_ptr<C> &r) const {
298  // get arguments
299  const C *pl = l.get();
300  const C *pr = r.get();
301  // have left?
302  if (pl) {
303  // have right, too?
304  if (pr) {
305  // have both arguments, so do the compare
306  return *pl < *pr;
307  }
308  }
309  // have one or less of the arguments; compare by pointer address
310  return pl < pr;
311  }
312 };
313 
314 // this is asking for trouble
315 typedef std::set< const std::shared_ptr<Something>, CompareSharedContent<Something> >
317 
318 }
319 
shared_ptr< ST > sp
A shared pointer to Something.
OLD Performs a less than comparison on an object contained within a Boost shared pointer.
void reset() noexcept
Loses the reference to Something.
const weak_ptr< ST > weak() const
Returns the weak pointer to Something.
boost::uuids::uuid someId
A unique identifier for Something that is valid across all peers.
const boost::uuids::uuid & uuid() const
Returns the object&#39;s unique identifier.
SomethingWeakRef()=default
Makes an uninitalized reference to nothing.
bool operator>(const S &s) const
Something objects are compared using their UUID.
bool operator!=(const S &s) const
Something objects are compared using their UUID.
void reset() noexcept
Loses the reference to Something.
bool operator==(const S &s) const
Something objects are compared using their UUID.
SomethingRef(const shared_ptr< ST > &s)
Makes a new reference to Something from a shared pointer.
shared_ptr< ST > lock() const noexcept
Attempts to obtain a shared pointer to the referenced Something.
bool operator<=(const S &s) const
Something objects are compared using their UUID.
A reference to a Something object.
const shared_ptr< ST > shared() const
Returns the shared pointer to Something.
SomethingWeakRef(const shared_ptr< ST > &s)
Makes a new reference to Something from a shared pointer.
bool expired() const noexcept
Tells if the weak pointer to Something has expired.
weak_ptr< ST > wp
A weak pointer to a Something.
std::set< const std::shared_ptr< Something >, CompareSharedContent< Something > > SomethingSet
const boost::uuids::uuid & uuid() const
Returns the object&#39;s unique identifier.
bool operator>=(const S &s) const
Something objects are compared using their UUID.
bool operator<(const S &s) const
Something objects are compared using their UUID.
SomethingWeakRef(const weak_ptr< ST > &s)
Makes a new reference to Something from a weak pointer.
A weak reference to a Something object.