Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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
47#ifndef LIB_P_H
48#define LIB_P_H
49
50
51#include "lib/error.hpp"
52#include "lib/meta/util.hpp"
53
54#include <memory>
55
56
57namespace lib {
58
59 using std::shared_ptr;
60 using std::weak_ptr;
61
74 template<class TAR, class BASE =shared_ptr<TAR>>
75 class P
76 : public BASE
77 {
78 public:
79 P ( ) : BASE() {}
80 template<class Y> explicit P (Y* p) : BASE(p) {}
81 template<class Y, class D> P (Y* p, D d) : BASE(p,d){}
82
83 P (P const& r) : BASE(r) {}
84 P (P const&& rr) : BASE(rr) {}
85 template<class Y> P (shared_ptr<Y> const& r) : BASE(r) {}
86 template<class Y> explicit P (weak_ptr<Y> const& wr) : BASE(wr) {}
87 template<class Y> explicit P (std::unique_ptr<Y>&& u) : BASE(u.release()) {}
88
89
90 P& operator= (P const& r) { BASE::operator= (r); return *this; }
91 P& operator= (P const&& rr) { BASE::operator= (rr); return *this; }
92 template<class Y> P& operator=(shared_ptr<Y> const& sr) { BASE::operator= (sr); return *this; }
93
94 TAR* get() const { return dynamic_cast<TAR*> (BASE::get()); }
95 TAR& operator*() const { return *get(); }
96 TAR* operator->() const { return get(); }
97
98 void swap(P& b) { BASE::swap (b);}
99
100 operator std::string() const noexcept;
101
102
103 private: /* === friend operators injected into enclosing namespace for ADL === */
105 template<typename _O_,typename B>
106 friend inline bool
107 operator== (P const& p, P<_O_, B> const& q) { return (p and q)? (*p == *q) : (!p and !q); }
109 template<typename _O_,typename B>
110 friend inline bool
111 operator!= (P const& p, P<_O_, B> const& q) { return (p and q)? (*p != *q) : !(!p and !q); }
112
113 template<typename _O_,typename B>
114 friend inline bool
115 operator< (P const& p, P<_O_, B> const& q) { REQUIRE (p and q); return *p < *q; }
116
117 template<typename _O_,typename B>
118 friend inline bool
119 operator> (P const& p, P<_O_, B> const& q) { REQUIRE (p and q); return *q < *p; }
120
121 template<typename _O_,typename B>
122 friend inline bool
123 operator<= (P const& p, P<_O_, B> const& q) { REQUIRE (p and q); return *p <= *q;}
124
125 template<typename _O_,typename B>
126 friend inline bool
127 operator>= (P const& p, P<_O_, B> const& q) { REQUIRE (p and q); return *p >= *q;}
128
129 };
130
131
132
133
140 template<typename X, typename...ARGS>
141 inline P<X>
142 makeP (ARGS&&... ctorArgs)
143 {
144 return P<X>{new X {std::forward<ARGS>(ctorArgs)...}};
145 }
146
147
148
153 template<class TAR, class BASE>
154 inline
155 P<TAR,BASE>::operator std::string() const noexcept
156 try {
157 if (BASE::get())
158 return util::StringConv<TAR>::invoke (this->operator*());
159 else
160 return "⟂ P<"+meta::typeStr<TAR>()+">";
161 }
162 catch(...)
163 { return meta::FAILURE_INDICATOR; }
164
165
166
167} // namespace lib
168#endif
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition p.hpp:77
friend bool operator>=(P const &p, P< _O_, B > const &q)
Definition p.hpp:127
TAR * get() const
Definition p.hpp:94
friend bool operator!=(P const &p, P< _O_, B > const &q)
Definition p.hpp:111
void swap(P &b)
Definition p.hpp:98
P(Y *p, D d)
Definition p.hpp:81
P & operator=(shared_ptr< Y > const &sr)
Definition p.hpp:92
P(weak_ptr< Y > const &wr)
Definition p.hpp:86
P(P const &r)
Definition p.hpp:83
P(P const &&rr)
Definition p.hpp:84
P()
Definition p.hpp:79
P & operator=(P const &r)
Definition p.hpp:90
P(shared_ptr< Y > const &r)
Definition p.hpp:85
friend bool operator<(P const &p, P< _O_, B > const &q)
Definition p.hpp:115
friend bool operator>(P const &p, P< _O_, B > const &q)
Definition p.hpp:119
friend bool operator<=(P const &p, P< _O_, B > const &q)
Definition p.hpp:123
TAR * operator->() const
Definition p.hpp:96
P(std::unique_ptr< Y > &&u)
Definition p.hpp:87
P(Y *p)
Definition p.hpp:80
TAR & operator*() const
Definition p.hpp:95
Lumiera error handling (C++ interface).
Simple and lightweight helpers for metaprogramming and type detection.
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
const string FAILURE_INDICATOR
Implementation namespace for support and library code.
P< X > makeP(ARGS &&... ctorArgs)
Helper to create and manage by lib::P.
Definition p.hpp:142
static std::string invoke(X const &x) noexcept