aboutsummaryrefslogtreecommitdiff
path: root/odb/details/unique-ptr.hxx
blob: 547dd3b1a2a201bfa3b856cdfde8ecbc7e49e08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// file      : odb/details/unique-ptr.hxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_DETAILS_UNIQUE_PTR_HXX
#define ODB_DETAILS_UNIQUE_PTR_HXX

#include <odb/pre.hxx>

#include <odb/details/config.hxx>

namespace odb
{
  namespace details
  {
    template <typename T>
    class unique_ptr
    {
    public:
      typedef T element_type;

      explicit unique_ptr (T* p = 0): p_ (p) {}
      ~unique_ptr () {delete p_;}

#ifdef ODB_CXX11
      unique_ptr (unique_ptr&& p): p_ (p.p_) {p.p_ = 0;}
      unique_ptr& operator= (unique_ptr&& p)
      {
        if (this != &p)
        {
          delete p_;
          p_ = p.p_;
          p.p_ = 0;
        }
        return *this;
      }
#endif

    private:
      unique_ptr (const unique_ptr&);
      unique_ptr& operator= (const unique_ptr&);

    public:
      T*
      operator-> () const {return p_;}

      T&
      operator* () const {return *p_;}

      typedef T* unique_ptr::*unspecified_bool_type;
      operator unspecified_bool_type () const
      {
        return p_ != 0 ? &unique_ptr::p_ : 0;
      }

      T*
      get () const {return p_;}

      void
      reset (T* p = 0)
      {
        delete p_;
        p_ = p;
      }

      T*
      release ()
      {
        T* r (p_);
        p_ = 0;
        return r;
      }

    private:
      T* p_;
    };

    template <typename T1, typename T2>
    inline bool
    operator== (const unique_ptr<T1>& a, const unique_ptr<T2>& b)
    {
      return a.get () == b.get ();
    }

    template <typename T1, typename T2>
    inline bool
    operator!= (const unique_ptr<T1>& a, const unique_ptr<T2>& b)
    {
      return a.get () != b.get ();
    }
  }
}

#include <odb/post.hxx>

#endif // ODB_DETAILS_UNIQUE_PTR_HXX