summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ue5/array.hpp20
-rw-r--r--ue5/mean_mark.hpp2
-rw-r--r--ue5/shared_ptr.hpp55
3 files changed, 13 insertions, 64 deletions
diff --git a/ue5/array.hpp b/ue5/array.hpp
index e9adc55..0acd37d 100644
--- a/ue5/array.hpp
+++ b/ue5/array.hpp
@@ -1,12 +1,13 @@
1#ifndef ARRAY_H 1#ifndef ARRAY_H
2#define ARRAY_H 2#define ARRAY_H
3 3
4#include <utility>
5#include <algorithm>
6#include <stdexcept>
7
4#undef SOLVED_2 8#undef SOLVED_2
5#define SOLVED_2 9#define SOLVED_2
6 10
7#include <utility> // for std::move
8#include <stdexcept> // for std::out_of_range
9
10namespace Ti 11namespace Ti
11{ 12{
12 template<typename T, std::size_t N> 13 template<typename T, std::size_t N>
@@ -22,21 +23,12 @@ namespace Ti
22 23
23 void fill(const T& u) 24 void fill(const T& u)
24 { 25 {
25 /* std::fill_n(begin(), size(), u); */ 26 std::fill_n(begin(), N, u);
26 for(size_type i = 0; i < N; ++i)
27 m_data[i] = u;
28 } 27 }
29 28
30 /* range check not necessary. N must be the same in other */
31 void swap(array<T,N> & other) 29 void swap(array<T,N> & other)
32 { 30 {
33 /* std::swap_ranges(begin(), end(), other.begin()); */ 31 std::swap_ranges(begin(), end(), other.begin());
34 for(size_type i = 0; i < N; ++i)
35 {
36 T x(m_data[i]);
37 m_data[i] = other.m_data[i];
38 other.m_data[i] = x;
39 }
40 } 32 }
41 33
42 iterator begin() 34 iterator begin()
diff --git a/ue5/mean_mark.hpp b/ue5/mean_mark.hpp
index ba5a230..a28b219 100644
--- a/ue5/mean_mark.hpp
+++ b/ue5/mean_mark.hpp
@@ -1,6 +1,8 @@
1#ifndef MEAN_MARK_H 1#ifndef MEAN_MARK_H
2#define MEAN_MARK_H 2#define MEAN_MARK_H
3 3
4#include <typeinfo>
5
4#undef SOLVED_3 6#undef SOLVED_3
5#define SOLVED_3 7#define SOLVED_3
6 8
diff --git a/ue5/shared_ptr.hpp b/ue5/shared_ptr.hpp
index 00a4dc7..e423904 100644
--- a/ue5/shared_ptr.hpp
+++ b/ue5/shared_ptr.hpp
@@ -1,19 +1,13 @@
1#ifndef SHARED_PTR_H 1#ifndef SHARED_PTR_H
2#define SHARED_PTR_H 2#define SHARED_PTR_H
3 3
4/* TODO includes */ 4#include <algorithm>
5 5
6#undef SOLVED_1 6#undef SOLVED_1
7#define SOLVED_1 7#define SOLVED_1
8 8
9/* TODO: remove */
10#define SHPDEBUG if (0)
11#include <iostream>
12
13namespace Ti 9namespace Ti
14{ 10{
15 /* TODO helpers */
16
17 template <typename T> 11 template <typename T>
18 class shared_ptr 12 class shared_ptr
19 { 13 {
@@ -24,20 +18,16 @@ namespace Ti
24 public: 18 public:
25 shared_ptr() 19 shared_ptr()
26 : m_ptr(NULL), m_count(NULL) 20 : m_ptr(NULL), m_count(NULL)
27 { 21 {}
28 SHPDEBUG std::cerr << this << ": shared_ptr()" << std::endl;
29 }
30 22
31 T* get() const 23 T* get() const
32 { 24 {
33 SHPDEBUG std::cerr << this << ": get()" << std::endl;
34 return m_ptr; 25 return m_ptr;
35 } 26 }
36 27
37 shared_ptr(const shared_ptr<T>& other) 28 shared_ptr(const shared_ptr<T>& other)
38 : m_ptr(other.m_ptr), m_count(other.m_count) 29 : m_ptr(other.m_ptr), m_count(other.m_count)
39 { 30 {
40 SHPDEBUG std::cerr << this << ": shared_ptr(shared_ptr<T>& other)" << std::endl;
41 add_ref(); 31 add_ref();
42 } 32 }
43 33
@@ -45,7 +35,6 @@ namespace Ti
45 shared_ptr(const shared_ptr<O>& other) 35 shared_ptr(const shared_ptr<O>& other)
46 : m_ptr(other.m_ptr), m_count(other.m_count) 36 : m_ptr(other.m_ptr), m_count(other.m_count)
47 { 37 {
48 SHPDEBUG std::cerr << this << ": shared_ptr(shared_ptr<O>& other)" << std::endl;
49 add_ref(); 38 add_ref();
50 } 39 }
51 40
@@ -54,59 +43,39 @@ namespace Ti
54 shared_ptr(const shared_ptr<O>& other, T* ptr) 43 shared_ptr(const shared_ptr<O>& other, T* ptr)
55 : m_ptr(ptr), m_count(other.m_count) 44 : m_ptr(ptr), m_count(other.m_count)
56 { 45 {
57 SHPDEBUG std::cerr << this << ": shared_ptr(shared_ptr<O>& other, O* ptr)" << std::endl;
58 add_ref(); 46 add_ref();
59 } 47 }
60 48
61 template <typename O> 49 template <typename O>
62 explicit shared_ptr(O* p) 50 explicit shared_ptr(O* p)
63 : m_ptr(p), m_count(new unsigned long(1)) 51 : m_ptr(p), m_count(new unsigned long(1))
64 { 52 {}
65 SHPDEBUG std::cerr << this << ": shared_ptr(O* p)" << std::endl;
66 }
67 53
68 shared_ptr& operator=(const shared_ptr<T>& other) 54 shared_ptr& operator=(const shared_ptr<T>& other)
69 { 55 {
70 SHPDEBUG std::cerr << this << ": operator=(shared_ptr<T>& other)" << std::endl;
71#if 1
72 shared_ptr(other).swap(*this);
73 return *this;
74#else
75 if (*this == other) 56 if (*this == other)
76 return *this; 57 return *this;
77
78 release(); 58 release();
79
80 m_ptr = other.m_ptr; 59 m_ptr = other.m_ptr;
81 m_count = other.m_count; 60 m_count = other.m_count;
82 add_ref(); 61 add_ref();
83 return *this; 62 return *this;
84#endif
85 } 63 }
86 64
87 template <typename O> 65 template <typename O>
88 shared_ptr& operator=(const shared_ptr<O>& other) 66 shared_ptr& operator=(const shared_ptr<O>& other)
89 { 67 {
90 SHPDEBUG std::cerr << this << ": operator=(shared_ptr<O>& other)" << std::endl;
91#if 0
92 shared_ptr(other).swap(*this);
93 return *this;
94#else
95 if (*this == other) 68 if (*this == other)
96 return *this; 69 return *this;
97
98 release(); 70 release();
99
100 m_ptr = other.m_ptr; 71 m_ptr = other.m_ptr;
101 m_count = other.m_count; 72 m_count = other.m_count;
102 add_ref(); 73 add_ref();
103 return *this; 74 return *this;
104#endif
105 } 75 }
106 76
107 ~shared_ptr() 77 ~shared_ptr()
108 { 78 {
109 SHPDEBUG std::cerr << this << ": ~shared_ptr()" << std::endl;
110 release(); 79 release();
111 } 80 }
112 81
@@ -122,15 +91,8 @@ namespace Ti
122 91
123 void swap(shared_ptr<T>& other) 92 void swap(shared_ptr<T>& other)
124 { 93 {
125 /* std::swap(m_ptr, other.m_ptr); */ 94 std::swap(m_ptr, other.m_ptr);
126 T* ptr(m_ptr); 95 std::swap(m_count, other.m_count);
127 m_ptr = other.m_ptr;
128 other.m_ptr = ptr;
129
130 /* std::swap(m_count, other.m_count); */
131 unsigned long* count(m_count);
132 m_count = other.m_count;
133 other.m_count = count;
134 } 96 }
135 97
136 inline void reset() 98 inline void reset()
@@ -188,7 +150,6 @@ namespace Ti
188 template <typename T, typename ... Args> 150 template <typename T, typename ... Args>
189 shared_ptr<T> make_shared(Args ... args) 151 shared_ptr<T> make_shared(Args ... args)
190 { 152 {
191 SHPDEBUG std::cerr << "make_shared(args...)" << std::endl;
192 return shared_ptr<T>(new T(args ...)); 153 return shared_ptr<T>(new T(args ...));
193 } 154 }
194 155
@@ -231,16 +192,12 @@ namespace Ti
231 template <typename T, typename F> 192 template <typename T, typename F>
232 shared_ptr<T> shared_dynamic_cast(const shared_ptr<F>& from) 193 shared_ptr<T> shared_dynamic_cast(const shared_ptr<F>& from)
233 { 194 {
234 SHPDEBUG std::cerr << "shared_dynamic_cast(...)" << std::endl;
235 T* castptr = dynamic_cast<T *>(from.get()); 195 T* castptr = dynamic_cast<T *>(from.get());
236 if (castptr != NULL) 196 if (castptr != NULL)
237 {
238 return shared_ptr<T>(from, castptr); 197 return shared_ptr<T>(from, castptr);
239 }
240 else 198 else
241 return shared_ptr<T>(); 199 return shared_ptr<T>();
242 } 200 }
243
244} // end namespace ti 201} // end namespace ti
245 202
246namespace std 203namespace std
@@ -251,8 +208,6 @@ namespace std
251 { 208 {
252 t1.swap(t2); 209 t1.swap(t2);
253 } 210 }
254
255 /* TODO */
256} 211}
257 212
258#endif 213#endif