summaryrefslogtreecommitdiff
path: root/backend/src/sys/ref.hpp
blob: 1672f543baf444990859754f71c1eecd07e99680 (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
97
98
99
/* 
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Benjamin Segovia <benjamin.segovia@intel.com>
 */

#ifndef __PF_REF_HPP__
#define __PF_REF_HPP__

#include "sys/atomic.hpp"
#include "sys/alloc.hpp"

namespace pf
{
  class RefCount
  {
  public:
    RefCount() : refCounter(0) {}
    virtual ~RefCount() {}
    INLINE void refInc() { refCounter++; }
    INLINE bool refDec() { return !(--refCounter); }
    Atomic32 refCounter;
  };

  ////////////////////////////////////////////////////////////////////////////////
  /// Reference to single object
  ////////////////////////////////////////////////////////////////////////////////

  template<typename Type>
  class Ref {
  public:
    Type* const ptr;

    ////////////////////////////////////////////////////////////////////////////////
    /// Constructors, Assignment & Cast Operators
    ////////////////////////////////////////////////////////////////////////////////

    INLINE Ref(void) : ptr(NULL) {}
    INLINE Ref(NullTy) : ptr(NULL) {}
    INLINE Ref(const Ref& input) : ptr(input.ptr) { if ( ptr ) ptr->refInc(); }
    INLINE Ref(Type* const input) : ptr(input) { if (ptr) ptr->refInc(); }
    INLINE ~Ref(void) { if (ptr && ptr->refDec()) PF_DELETE(ptr); }

    INLINE Ref& operator= (const Ref &input) {
      if (input.ptr) input.ptr->refInc();
      if (ptr && ptr->refDec()) PF_DELETE(ptr);
      *(Type**)&ptr = input.ptr;
      return *this;
    }

    INLINE Ref& operator= (NullTy) {
      if (ptr && ptr->refDec()) DELETE(ptr);
      *(Type**)&ptr = NULL;
      return *this;
    }

    INLINE operator bool(void) const { return ptr != NULL; }
    INLINE operator Type*(void) const { return ptr; }

    ////////////////////////////////////////////////////////////////////////////////
    /// Properties
    ////////////////////////////////////////////////////////////////////////////////

    INLINE const Type& operator*  (void) const { return *ptr; }
    INLINE const Type* operator-> (void) const { return  ptr; }
    INLINE Type& operator*  (void) { return *ptr; }
    INLINE Type* operator-> (void) { return  ptr; }

    template<typename TypeOut>
    INLINE       Ref<TypeOut> cast()       { return Ref<TypeOut>(static_cast<TypeOut*>(ptr)); }
    template<typename TypeOut>
    INLINE const Ref<TypeOut> cast() const { return Ref<TypeOut>(static_cast<TypeOut*>(ptr)); }
    PF_CLASS(Ref);
  };

  template<typename Type> INLINE bool operator< ( const Ref<Type>& a, const Ref<Type>& b ) { return a.ptr <  b.ptr ; }
  template<typename Type> INLINE bool operator== ( const Ref<Type>& a, NullTy             ) { return a.ptr == NULL  ; }
  template<typename Type> INLINE bool operator== ( NullTy            , const Ref<Type>& b ) { return NULL  == b.ptr ; }
  template<typename Type> INLINE bool operator== ( const Ref<Type>& a, const Ref<Type>& b ) { return a.ptr == b.ptr ; }
  template<typename Type> INLINE bool operator!= ( const Ref<Type>& a, NullTy             ) { return a.ptr != NULL  ; }
  template<typename Type> INLINE bool operator!= ( NullTy            , const Ref<Type>& b ) { return NULL  != b.ptr ; }
  template<typename Type> INLINE bool operator!= ( const Ref<Type>& a, const Ref<Type>& b ) { return a.ptr != b.ptr ; }
}

#endif /* __PF_REF_HPP__ */