TT CORE SDK 2.0.1.1
TT CORE SDK documentation
Loading...
Searching...
No Matches
shared_ptr.h
Go to the documentation of this file.
1/***************************************************************************
2 *
3 * Unpublished Work Copyright (c) 2018-2020
4 * Trading Technologies International, Inc.
5 * All Rights Reserved Worldwide
6 *
7 * * * * S T R I C T L Y P R O P R I E T A R Y * * *
8 *
9 * WARNING: This program (or document) is unpublished, proprietary property
10 * of Trading Technologies International, Inc. and is to be maintained in
11 * strict confidence. Unauthorized reproduction, distribution or disclosure
12 * of this program (or document), or any program (or document) derived from
13 * it is prohibited by State and Federal law, and by local law outside of
14 * the U.S.
15 *
16 ***************************************************************************/
17#pragma once
18#include "shared_base.h"
19#include <cstddef>
20
21namespace ttsdk
22{
23
24 template<typename T>
26 {
27 public:
28 typedef T element_type;
29 typedef T * pointer;
30 typedef const T * const_pointer;
31 typedef T& reference;
32 typedef const T& const_reference;
33
35 :object(nullptr)
36 {}
37
39 :object(nullptr)
40 {
41 reset(p);
42 }
43
45 {
46 dec_ref(object);
47 }
48
49 // Copy constructor
51 :object(nullptr)
52 {
53 reset(cc.object);
54 }
55
56 // Copy from a derived type
57 template<typename U>
59 :object(nullptr)
60 {
61 reset(cc.object);
62 }
63
65 {
66 reset(rhs.object);
67 return *this;
68 }
69
70 // assignment from a derived type
71 template<typename U>
73 {
74 reset(rhs.object);
75 return *this;
76 }
77
78 // true if not null
79 operator bool() {return object != nullptr;}
80 operator bool() const {return object != nullptr;}
81
82 // Cast to type U, usually used to downcast to derived type
83 // example:
84 // shared_ptr<base_class> base;
85 // shared_ptr<derived_class> derived = base.cast<derived_class>()
86 template<typename U>
88 {
89 return shared_ptr<U>(static_cast<U *>(object));
90 }
91
93 {
94 return object;
95 }
96
98 {
99 return object;
100 }
101
103 {
104 return *object;
105 }
106
108 {
109 return *object;
110 }
111
113 {
114 dec_ref(object);
115 object = p;
116 inc_ref(object);
117 }
118 void reset()
119 {
120 dec_ref(object);
121 object = nullptr;
122 }
123
125 {
126 return object;
127 }
128
130 {
131 return object;
132 }
133
134 private:
135 void dec_ref(const T * p)
136 {
137 if (p)
138 {
139 if (p->dec_ref() == 0)
140 {
141 //delete p;
142 p->del_ref();
143 }
144 }
145 }
146 void inc_ref(const T * p)
147 {
148 if (p)
149 p->inc_ref();
150 }
151 template<typename U>
152 friend class shared_ptr;
153 mutable T *object;
154 };
155
156
157 // equivalence operators
158 template<typename T>
159 inline bool operator==(const shared_ptr<T>& p, std::nullptr_t)
160 {
161 return p.get() == 0;
162 }
163
164 template<class T>
165 inline bool operator!=(const shared_ptr<T>& p, std::nullptr_t)
166 {
167 return p.get() != 0;
168 }
169
170 template<class T>
172 {
173 return a.get() == b.get();
174 }
175
176 template<class T>
178 {
179 return a.get() != b.get();
180 }
181}
shared_ptr< U > cast()
Definition shared_ptr.h:87
shared_ptr(pointer p)
Definition shared_ptr.h:38
pointer operator->()
Definition shared_ptr.h:92
shared_ptr(const shared_ptr< U > &cc)
Definition shared_ptr.h:58
const_pointer get() const
Definition shared_ptr.h:129
const T & const_reference
Definition shared_ptr.h:32
shared_ptr(const shared_ptr< T > &cc)
Definition shared_ptr.h:50
void reset(pointer p)
Definition shared_ptr.h:112
const T * const_pointer
Definition shared_ptr.h:30
shared_ptr & operator=(const shared_ptr &rhs)
Definition shared_ptr.h:64
const_pointer operator->() const
Definition shared_ptr.h:97
friend class shared_ptr
Definition shared_ptr.h:152
shared_ptr & operator=(const shared_ptr< U > &rhs)
Definition shared_ptr.h:72
reference operator*()
Definition shared_ptr.h:102
bool operator==(const shared_ptr< T > &p, std::nullptr_t)
Definition shared_ptr.h:159
bool operator!=(const shared_ptr< T > &p, std::nullptr_t)
Definition shared_ptr.h:165