| 1 |
|
| #pragma once |
| 2 |
|
| #include <xs/basic.h> |
| 3 |
|
| #include <memory> |
| 4 |
|
| #include <stdexcept> |
| 5 |
|
| #include <panda/cast.h> |
| 6 |
|
| #include <panda/refcnt.h> |
| 7 |
|
| |
| 8 |
|
| namespace xs { |
| 9 |
|
| |
| 10 |
|
| class Backref { |
| 11 |
|
| public: |
| 12 |
|
| mutable SV* svobj; |
| 13 |
|
| mutable bool zombie; |
| 14 |
|
| mutable bool in_cdtor; |
| 15 |
|
| |
| 16 |
0 |
| template <class T> static const Backref* get (T* var) { return panda::dyn_cast<const Backref*>(var); } |
| 17 |
|
| template <class T> static const Backref* get (const panda::iptr<T>& var) { return panda::dyn_cast<const Backref*>(var.get()); } |
| 18 |
|
| template <class T> static const Backref* get (const std::shared_ptr<T>& var) { return panda::dyn_cast<const Backref*>(var.get()); } |
| 19 |
|
| |
| 20 |
|
| protected: |
| 21 |
7 |
| Backref () : svobj(NULL), zombie(false), in_cdtor(false) {} |
| 22 |
|
| |
| 23 |
6 |
| void dtor () const { |
| 24 |
6 |
| in_cdtor = true; |
| 25 |
6 |
▶ | if (!svobj) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!svobj) return; |
2 |
4 |
6 |
|
| 26 |
4 |
| auto tmp = svobj; |
| 27 |
4 |
| svobj = NULL; |
| 28 |
4 |
| SvREFCNT_dec_NN(tmp); |
| 29 |
|
| }; |
| 30 |
|
| |
| 31 |
0 |
▶ | virtual ~Backref () { if (!in_cdtor) _throw_no_dtor(); } // protect against forgetting calling the dtor() |
Branch: 1 branch
| Branch | True | False | Total |
| virtual ~Backref () { if (!in_cdtor) _throw_no_dtor(); } // protect against forgetting calling the dtor() |
0 |
6 |
6 |
|
| 32 |
|
| |
| 33 |
|
| private: |
| 34 |
0 |
| static void _throw_no_dtor () { |
| 35 |
0 |
▶ | throw std::logic_error("~Backref panic: dtor() wasn't called - you must explicitly call Backref::dtor() or use make_backref()"); |
Branch: 1 branch
| Branch | True | False | Total |
| throw std::logic_error("~Backref panic: dtor() wasn't called - you must explicitly call Backref::dtor() or use make_backref()"); |
0 |
0 |
0 |
|
| 36 |
|
| } |
| 37 |
|
| }; |
| 38 |
|
| |
| 39 |
|
| template <class CLASS> |
| 40 |
|
| class BackrefWrapper : public CLASS, public Backref { |
| 41 |
0 |
| ~BackrefWrapper () override { Backref::dtor(); } |
| 42 |
|
| public: |
| 43 |
0 |
| template <typename... Args> BackrefWrapper (Args&&... args) : CLASS(std::forward<Args>(args)...) {} |
| 44 |
|
| }; |
| 45 |
|
| |
| 46 |
0 |
| template <typename CLASS, typename... Args> inline CLASS* make_backref (Args&&... args) { |
| 47 |
0 |
▶ | return new BackrefWrapper<CLASS>(std::forward<Args>(args)...); |
Branch: 2 branches
| Branch | True | False | Total |
| return new BackrefWrapper<CLASS>(std::forward<Args>(args)...); |
0 |
0 |
0 |
| return new BackrefWrapper<CLASS>(std::forward<Args>(args)...); |
0 |
0 |
0 |
|
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| } |