replicode
base.h
Go to the documentation of this file.
1 // base.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef core_base_h
32 #define core_base_h
33 
34 #include <cstdlib>
35 #include <atomic>
36 #include "dll.h"
37 
38 
39 namespace core {
40 
41 class _Object;
42 
43 // Smart pointer (ref counting, deallocates when ref count<=0).
44 // No circular refs (use std c++ ptrs).
45 // No passing in functions (cast P<C> into C*).
46 // Cannot be a value returned by a function (return C* instead).
47 template<class C> class P {
48 private:
50 public:
51  P();
52  P(C *o);
53  P(const P<C> &p);
54  ~P();
55  C *operator ->() const;
56  template<class D> operator D *() const {
57 
58  return (D *)object;
59  }
60  bool operator ==(C *c) const;
61  bool operator !=(C *c) const;
62  bool operator <(C *c) const;
63  bool operator >(C *c) const;
64  bool operator !() const;
65  template<class D> bool operator ==(P<D> &p) const;
66  template<class D> bool operator !=(P<D> &p) const;
67  P<C> &operator =(C *c);
68  P<C> &operator =(const P<C> &p);
69  template<class D> P<C> &operator =(const P<D> &p);
70 };
71 
73 
74 // Root smart-pointable object class.
76  template<class C> friend class P;
77  friend class _P;
78 protected:
79  std::atomic_int_fast64_t refCount;
80  _Object();
81 public:
82  virtual ~_Object();
83  void incRef();
84  virtual void decRef();
85 };
86 
87 }
88 
89 
90 #include "base.tpl.cpp"
91 
92 
93 #endif
Definition: base.h:75
Definition: base.cpp:35
P()
Definition: base.tpl.cpp:33
bool operator!() const
Definition: base.tpl.cpp:79
bool operator>(C *c) const
Definition: base.h:47
bool operator<(C *c) const
bool operator!=(C *c) const
Definition: base.tpl.cpp:64
C * operator->() const
Definition: base.tpl.cpp:54
bool operator==(C *c) const
Definition: base.tpl.cpp:59
#define core_dll
Definition: dll.h:43
std::atomic_int_fast64_t refCount
Definition: base.h:79
_Object * object
Definition: base.h:49
~P()
Definition: base.tpl.cpp:48
P< C > & operator=(C *c)
Definition: base.tpl.cpp:84