The Battle for Wesnoth  1.19.0-dev
shared_reference.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include <memory>
18 #include <stdexcept>
19 
20 namespace utils {
21 
22 template <typename T>
24 {
25  std::shared_ptr<T> m_ptr;
26 
27 public:
28  template<typename Y>
30  : m_ptr(p.m_ptr)
31  { }
32  template<typename Y>
34  : m_ptr(std::move(p.m_ptr))
35  { }
36  template<typename Y>
37  explicit shared_reference(const std::shared_ptr<Y>& p)
38  : m_ptr(p)
39  {
40  if(!p) {
41  throw std::invalid_argument("invalid shared_reference");
42  }
43  }
44  template<typename Y>
45  explicit shared_reference(std::shared_ptr<Y>&& p)
46  : m_ptr(p)
47  {
48  if(!p) {
49  throw std::invalid_argument("invalid shared_reference");
50  }
51  }
52 
53  template<typename Y>
55  {
56  m_ptr = std::move(p.m_ptr);
57  return *this;
58  }
59  template<typename Y>
61  {
62  m_ptr = p.m_ptr;
63  return *this;
64  }
65 
66  ~shared_reference() = default;
67 
68  operator std::shared_ptr<T>()
69  { return m_ptr; }
70 
71  T* operator->() { return m_ptr.get(); }
72  const T* operator->() const { return m_ptr.get(); }
73 
74  T& operator*() { return *m_ptr.get(); }
75  const T& operator*() const { return *m_ptr.get(); }
76 
77  template <typename XT, typename...XTypes>
79 
80 };
81 
82 
83 template <typename T, typename...Types>
85 {
86  return shared_reference<T>(std::make_shared<T>(std::forward<Types>(args)...));
87 }
88 
89 
90 } // namespace utils
std::shared_ptr< T > m_ptr
shared_reference(const shared_reference< Y > &p)
shared_reference & operator=(const shared_reference< Y > &p)
shared_reference & operator=(shared_reference< Y > &&p)
const T * operator->() const
const T & operator*() const
shared_reference(const std::shared_ptr< Y > &p)
friend shared_reference< XT > make_shared_reference(XTypes &&...args)
shared_reference(shared_reference< Y > &&p)
shared_reference(std::shared_ptr< Y > &&p)
shared_reference< T > make_shared_reference(Types &&...args)
mock_party p