Lumiera  0.pre.03
»edit your freedom«
p.hpp
Go to the documentation of this file.
1 /*
2  P.hpp - customised shared_ptr with ordering and type relationships
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
49 #ifndef LIB_P_H
50 #define LIB_P_H
51 
52 
53 #include "lib/error.hpp"
54 #include "lib/meta/util.hpp"
55 
56 #include <memory>
57 
58 
59 namespace lib {
60 
61  using std::shared_ptr;
62  using std::weak_ptr;
63 
76  template<class TAR, class BASE =shared_ptr<TAR>>
77  class P
78  : public BASE
79  {
80  public:
81  P ( ) : BASE() {}
82  template<class Y> explicit P (Y* p) : BASE(p) {}
83  template<class Y, class D> P (Y* p, D d) : BASE(p,d){}
84 
85  P (P const& r) : BASE(r) {}
86  P (P const&& rr) : BASE(rr) {}
87  template<class Y> P (shared_ptr<Y> const& r) : BASE(r) {}
88  template<class Y> explicit P (weak_ptr<Y> const& wr) : BASE(wr) {}
89  template<class Y> explicit P (std::unique_ptr<Y>&& u) : BASE(u.release()) {}
90 
91 
92  P& operator= (P const& r) { BASE::operator= (r); return *this; }
93  P& operator= (P const&& rr) { BASE::operator= (rr); return *this; }
94  template<class Y> P& operator=(shared_ptr<Y> const& sr) { BASE::operator= (sr); return *this; }
95 
96  TAR* get() const { return dynamic_cast<TAR*> (BASE::get()); }
97  TAR& operator*() const { return *get(); }
98  TAR* operator->() const { return get(); }
99 
100  void swap(P& b) { BASE::swap (b);}
101 
102  operator std::string() const noexcept;
103 
104 
105  private: /* === friend operators injected into enclosing namespace for ADL === */
107  template<typename _O_,typename B>
108  friend inline bool
109  operator== (P const& p, P<_O_, B> const& q) { return (p && q)? (*p == *q) : (!p && !q); }
110 
111  template<typename _O_,typename B>
112  friend inline bool
113  operator!= (P const& p, P<_O_, B> const& q) { return (p && q)? (*p != *q) : !(!p && !q); }
114 
115  template<typename _O_,typename B>
116  friend inline bool
117  operator< (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *p < *q; }
118 
119  template<typename _O_,typename B>
120  friend inline bool
121  operator> (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *q < *p; }
122 
123  template<typename _O_,typename B>
124  friend inline bool
125  operator<= (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *p <= *q;}
126 
127  template<typename _O_,typename B>
128  friend inline bool
129  operator>= (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *p >= *q;}
130 
131  };
132 
133 
134 
135 
142  template<typename X, typename...ARGS>
143  inline P<X>
144  makeP (ARGS&&... ctorArgs)
145  {
146  return P<X>{new X {std::forward<ARGS>(ctorArgs)...}};
147  }
148 
149 
150 
155  template<class TAR, class BASE>
156  inline
157  P<TAR,BASE>::operator std::string() const noexcept
158  try {
159  if (BASE::get())
160  return util::StringConv<TAR>::invoke (this->operator*());
161  else
162  return "⟂ P<"+meta::typeStr<TAR>()+">";
163  }
164  catch(...)
165  { return meta::FAILURE_INDICATOR; }
166 
167 
168 
169 } // namespace lib
170 #endif
Simple and lightweight helpers for metaprogramming and type detection.
Implementation namespace for support and library code.
Lumiera error handling (C++ interface).
P< X > makeP(ARGS &&... ctorArgs)
Helper to create and manage by lib::P.
Definition: p.hpp:144
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
failsafe invocation of custom string conversion.
Definition: meta/util.hpp:399