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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
40 #ifndef LIB_P_H
41 #define LIB_P_H
42 
43 
44 #include "lib/error.hpp"
45 #include "lib/meta/util.hpp"
46 
47 #include <memory>
48 
49 
50 namespace lib {
51 
52  using std::shared_ptr;
53  using std::weak_ptr;
54 
67  template<class TAR, class BASE =shared_ptr<TAR>>
68  class P
69  : public BASE
70  {
71  public:
72  P ( ) : BASE() {}
73  template<class Y> explicit P (Y* p) : BASE(p) {}
74  template<class Y, class D> P (Y* p, D d) : BASE(p,d){}
75 
76  P (P const& r) : BASE(r) {}
77  P (P const&& rr) : BASE(rr) {}
78  template<class Y> P (shared_ptr<Y> const& r) : BASE(r) {}
79  template<class Y> explicit P (weak_ptr<Y> const& wr) : BASE(wr) {}
80  template<class Y> explicit P (std::unique_ptr<Y>&& u) : BASE(u.release()) {}
81 
82 
83  P& operator= (P const& r) { BASE::operator= (r); return *this; }
84  P& operator= (P const&& rr) { BASE::operator= (rr); return *this; }
85  template<class Y> P& operator=(shared_ptr<Y> const& sr) { BASE::operator= (sr); return *this; }
86 
87  TAR* get() const { return dynamic_cast<TAR*> (BASE::get()); }
88  TAR& operator*() const { return *get(); }
89  TAR* operator->() const { return get(); }
90 
91  void swap(P& b) { BASE::swap (b);}
92 
93  operator std::string() const noexcept;
94 
95 
96  private: /* === friend operators injected into enclosing namespace for ADL === */
98  template<typename _O_,typename B>
99  friend inline bool
100  operator== (P const& p, P<_O_, B> const& q) { return (p && q)? (*p == *q) : (!p && !q); }
101 
102  template<typename _O_,typename B>
103  friend inline bool
104  operator!= (P const& p, P<_O_, B> const& q) { return (p && q)? (*p != *q) : !(!p && !q); }
105 
106  template<typename _O_,typename B>
107  friend inline bool
108  operator< (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *p < *q; }
109 
110  template<typename _O_,typename B>
111  friend inline bool
112  operator> (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *q < *p; }
113 
114  template<typename _O_,typename B>
115  friend inline bool
116  operator<= (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *p <= *q;}
117 
118  template<typename _O_,typename B>
119  friend inline bool
120  operator>= (P const& p, P<_O_, B> const& q) { REQUIRE (p && q); return *p >= *q;}
121 
122  };
123 
124 
125 
126 
133  template<typename X, typename...ARGS>
134  inline P<X>
135  makeP (ARGS&&... ctorArgs)
136  {
137  return P<X>{new X {std::forward<ARGS>(ctorArgs)...}};
138  }
139 
140 
141 
146  template<class TAR, class BASE>
147  inline
148  P<TAR,BASE>::operator std::string() const noexcept
149  try {
150  if (BASE::get())
151  return util::StringConv<TAR>::invoke (this->operator*());
152  else
153  return "⟂ P<"+meta::typeStr<TAR>()+">";
154  }
155  catch(...)
156  { return meta::FAILURE_INDICATOR; }
157 
158 
159 
160 } // namespace lib
161 #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:135
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:71
failsafe invocation of custom string conversion.
Definition: meta/util.hpp:390