Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
cursor_ptr.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <cstddef>
35
36namespace console
37{
48 template <class T>
50 {
51 T *original_ptr;
52 T *current_ptr;
53
54 public:
56 cursor_ptr() : original_ptr(nullptr), current_ptr(nullptr) {}
57
63 cursor_ptr(T *p) : original_ptr(p), current_ptr(p) {}
64
71 : original_ptr(nullptr), current_ptr(cp.current_ptr) {}
72
78 : original_ptr(cp.original_ptr), current_ptr(cp.current_ptr)
79 {
80 cp.original_ptr = nullptr;
81 }
82
85 T &operator*() { return *current_ptr; }
86 T *operator->() { return current_ptr; }
87 T &operator[](size_t i) { return *(current_ptr + i); }
88 T *get() { return current_ptr; }
89 operator T *() { return current_ptr; }
91
94 const T &operator*() const { return *current_ptr; }
95 const T *operator->() const { return current_ptr; }
96 const T &operator[](size_t i) const { return *(current_ptr + i); }
97 const T *get() const { return current_ptr; }
98 operator const T *() const { return current_ptr; }
100
102 ~cursor_ptr() { delete original_ptr; }
103
107 {
108 ++current_ptr;
109 return *this;
110 }
112 {
113 cursor_ptr tmp(*this);
114 ++current_ptr;
115 return tmp;
116 }
118 {
119 --current_ptr;
120 return *this;
121 }
123 {
124 cursor_ptr tmp(*this);
125 --current_ptr;
126 return tmp;
127 }
128
129
132 cursor_ptr operator+(int sep) const
133 {
134 cursor_ptr cp;
135 cp.current_ptr = current_ptr + sep;
136 return cp;
137 }
138 cursor_ptr operator-(int sep) const
139 {
140 cursor_ptr cp;
141 cp.current_ptr = current_ptr - sep;
142 return cp;
143 }
144 const cursor_ptr &operator+=(int sep)
145 {
146 current_ptr += sep;
147 return *this;
148 }
149 const cursor_ptr &operator-=(int sep)
150 {
151 current_ptr -= sep;
152 return *this;
153 }
154 int operator-(const cursor_ptr &cp) const
155 {
156 return current_ptr - cp.current_ptr;
157 }
158
159
162 bool operator==(const cursor_ptr &cp) const
163 {
164 return current_ptr == cp.current_ptr;
165 }
166 bool operator==(const T *p) const
167 {
168 return current_ptr == p;
169 }
170 friend bool operator==(const T *p, const cursor_ptr &cp)
171 {
172 return cp == p;
173 }
174 bool operator!=(const cursor_ptr &cp) const
175 {
176 return current_ptr != cp.current_ptr;
177 }
178 bool operator!=(const T *p) const
179 {
180 return current_ptr != p;
181 }
182 friend bool operator!=(const T *p, const cursor_ptr &cp)
183 {
184 return cp != p;
185 }
186
187
190
196 {
197 if (original_ptr == p)
198 return *this;
199 delete original_ptr;
200 original_ptr = p;
201 current_ptr = p;
202 return *this;
203 }
204
211 {
212 if (this == &cp)
213 return *this;
214 delete original_ptr;
215 original_ptr = nullptr;
216 current_ptr = cp.current_ptr;
217 return *this;
218 }
219
226 {
227 if (this == &cp)
228 return *this;
229 delete original_ptr;
230 current_ptr = cp.current_ptr;
231 original_ptr = cp.original_ptr;
232 cp.original_ptr = nullptr;
233 return *this;
234 }
235
236
241 void swap(cursor_ptr &cp)
242 {
243 T *p1 = current_ptr, *p2 = original_ptr;
244 current_ptr = cp.current_ptr;
245 original_ptr = cp.original_ptr;
246 cp.current_ptr = p1;
247 cp.original_ptr = p2;
248 }
249 };
250
258 template <class T>
259 class cursor_ptr<T[]>
260 {
261 T *original_ptr;
262 T *current_ptr;
263
264 public:
266 cursor_ptr() : original_ptr(nullptr), current_ptr(nullptr) {}
267
272 cursor_ptr(T *p) : original_ptr(p), current_ptr(p) {}
273
279 : original_ptr(nullptr), current_ptr(cp.current_ptr) {}
280
286 : original_ptr(cp.original_ptr), current_ptr(cp.current_ptr)
287 {
288 cp.original_ptr = nullptr;
289 }
290
293 T &operator*() { return *current_ptr; }
294 T *operator->() { return current_ptr; }
295 T &operator[](size_t i) { return *(current_ptr + i); }
296 T *get() { return current_ptr; }
297 operator T *() { return current_ptr; }
299
302 const T &operator*() const { return *current_ptr; }
303 const T *operator->() const { return current_ptr; }
304 const T &operator[](size_t i) const { return *(current_ptr + i); }
305 const T *get() const { return current_ptr; }
306 operator const T *() const { return current_ptr; }
308
310 ~cursor_ptr() { delete[] original_ptr; }
311
315 {
316 ++current_ptr;
317 return *this;
318 }
320 {
321 cursor_ptr tmp(*this);
322 ++current_ptr;
323 return tmp;
324 }
326 {
327 --current_ptr;
328 return *this;
329 }
331 {
332 cursor_ptr tmp(*this);
333 --current_ptr;
334 return tmp;
335 }
336
337
340 cursor_ptr operator+(int sep) const
341 {
342 cursor_ptr cp;
343 cp.current_ptr = current_ptr + sep;
344 return cp;
345 }
346 cursor_ptr operator-(int sep) const
347 {
348 cursor_ptr cp;
349 cp.current_ptr = current_ptr - sep;
350 return cp;
351 }
352 const cursor_ptr &operator+=(int sep)
353 {
354 current_ptr += sep;
355 return *this;
356 }
357 const cursor_ptr &operator-=(int sep)
358 {
359 current_ptr -= sep;
360 return *this;
361 }
362 int operator-(const cursor_ptr &cp) const
363 {
364 return current_ptr - cp.current_ptr;
365 }
366
367
370 bool operator==(const cursor_ptr &cp) const
371 {
372 return current_ptr == cp.current_ptr;
373 }
374 bool operator==(const T *p) const
375 {
376 return current_ptr == p;
377 }
378 friend bool operator==(const T *p, const cursor_ptr &cp)
379 {
380 return cp == p;
381 }
382 bool operator!=(const cursor_ptr &cp) const
383 {
384 return current_ptr != cp.current_ptr;
385 }
386 bool operator!=(const T *p) const
387 {
388 return current_ptr != p;
389 }
390 friend bool operator!=(const T *p, const cursor_ptr &cp)
391 {
392 return cp != p;
393 }
394
395
399 {
400 if (original_ptr == p)
401 return *this;
402 delete[] original_ptr;
403 original_ptr = p;
404 current_ptr = p;
405 return *this;
406 }
408 {
409 if (this == &cp)
410 return *this;
411 delete[] original_ptr;
412 original_ptr = nullptr;
413 current_ptr = cp.current_ptr;
414 return *this;
415 }
417 {
418 if (this == &cp)
419 return *this;
420 delete[] original_ptr;
421 current_ptr = cp.current_ptr;
422 original_ptr = cp.original_ptr;
423 cp.original_ptr = nullptr;
424 return *this;
425 }
426
427
432 void swap(cursor_ptr &cp)
433 {
434 T *p1 = current_ptr, *p2 = original_ptr;
435 current_ptr = cp.current_ptr;
436 original_ptr = cp.original_ptr;
437 cp.current_ptr = p1;
438 cp.original_ptr = p2;
439 }
440 };
441}
friend bool operator!=(const T *p, const cursor_ptr &cp)
Definition cursor_ptr.h:390
const T & operator*() const
Definition cursor_ptr.h:302
cursor_ptr()
默认构造。
Definition cursor_ptr.h:266
cursor_ptr(const cursor_ptr &cp)
拷贝构造,仅复制游标位置。
Definition cursor_ptr.h:278
cursor_ptr operator+(int sep) const
Definition cursor_ptr.h:340
cursor_ptr operator--(int)
Definition cursor_ptr.h:330
const cursor_ptr & operator=(T *p)
Definition cursor_ptr.h:398
T & operator*()
Definition cursor_ptr.h:293
cursor_ptr & operator++()
Definition cursor_ptr.h:314
T & operator[](size_t i)
Definition cursor_ptr.h:295
const T & operator[](size_t i) const
Definition cursor_ptr.h:304
bool operator==(const cursor_ptr &cp) const
Definition cursor_ptr.h:370
T * operator->()
Definition cursor_ptr.h:294
cursor_ptr(cursor_ptr &&cp)
移动构造,转移所有权。
Definition cursor_ptr.h:285
friend bool operator==(const T *p, const cursor_ptr &cp)
Definition cursor_ptr.h:378
const cursor_ptr & operator+=(int sep)
Definition cursor_ptr.h:352
bool operator==(const T *p) const
Definition cursor_ptr.h:374
bool operator!=(const cursor_ptr &cp) const
Definition cursor_ptr.h:382
cursor_ptr operator-(int sep) const
Definition cursor_ptr.h:346
cursor_ptr(T *p)
从原始数组指针构造,获取所有权。
Definition cursor_ptr.h:272
const cursor_ptr & operator=(cursor_ptr &&cp)
Definition cursor_ptr.h:416
void swap(cursor_ptr &cp)
交换两个 cursor_ptr 的内容。
Definition cursor_ptr.h:432
const cursor_ptr & operator=(const cursor_ptr &cp)
Definition cursor_ptr.h:407
T * get()
Definition cursor_ptr.h:296
const cursor_ptr & operator-=(int sep)
Definition cursor_ptr.h:357
cursor_ptr & operator--()
Definition cursor_ptr.h:325
const T * operator->() const
Definition cursor_ptr.h:303
cursor_ptr operator++(int)
Definition cursor_ptr.h:319
int operator-(const cursor_ptr &cp) const
Definition cursor_ptr.h:362
const T * get() const
Definition cursor_ptr.h:305
bool operator!=(const T *p) const
Definition cursor_ptr.h:386
~cursor_ptr()
析构,释放数组内存(使用 delete[])。
Definition cursor_ptr.h:310
int operator-(const cursor_ptr &cp) const
Definition cursor_ptr.h:154
friend bool operator!=(const T *p, const cursor_ptr &cp)
Definition cursor_ptr.h:182
const T & operator[](size_t i) const
Definition cursor_ptr.h:96
cursor_ptr operator++(int)
Definition cursor_ptr.h:111
bool operator==(const cursor_ptr &cp) const
Definition cursor_ptr.h:162
cursor_ptr & operator--()
Definition cursor_ptr.h:117
cursor_ptr(const cursor_ptr &cp)
拷贝构造,仅复制游标位置,不转移所有权。
Definition cursor_ptr.h:70
const T & operator*() const
Definition cursor_ptr.h:94
friend bool operator==(const T *p, const cursor_ptr &cp)
Definition cursor_ptr.h:170
cursor_ptr(T *p)
从原始指针构造,获取所有权。
Definition cursor_ptr.h:63
cursor_ptr()
默认构造,两个指针均为 nullptr。
Definition cursor_ptr.h:56
~cursor_ptr()
析构,释放 original_ptr 指向的内存(使用 delete)。
Definition cursor_ptr.h:102
const cursor_ptr & operator-=(int sep)
Definition cursor_ptr.h:149
cursor_ptr operator--(int)
Definition cursor_ptr.h:122
cursor_ptr operator+(int sep) const
Definition cursor_ptr.h:132
void swap(cursor_ptr &cp)
交换两个 cursor_ptr 的内容(包括所有者和游标)。
Definition cursor_ptr.h:241
const cursor_ptr & operator=(T *p)
从原始指针赋值,释放原有内存并接管新指针。
Definition cursor_ptr.h:195
T & operator*()
Definition cursor_ptr.h:85
cursor_ptr operator-(int sep) const
Definition cursor_ptr.h:138
T & operator[](size_t i)
Definition cursor_ptr.h:87
bool operator!=(const cursor_ptr &cp) const
Definition cursor_ptr.h:174
cursor_ptr(cursor_ptr &&cp)
移动构造,转移所有权。
Definition cursor_ptr.h:77
bool operator==(const T *p) const
Definition cursor_ptr.h:166
const cursor_ptr & operator=(cursor_ptr &&cp)
移动赋值,转移所有权。
Definition cursor_ptr.h:225
bool operator!=(const T *p) const
Definition cursor_ptr.h:178
const T * get() const
Definition cursor_ptr.h:97
const cursor_ptr & operator+=(int sep)
Definition cursor_ptr.h:144
cursor_ptr & operator++()
Definition cursor_ptr.h:106
const cursor_ptr & operator=(const cursor_ptr &cp)
拷贝赋值,仅复制游标位置,不转移所有权。
Definition cursor_ptr.h:210
const T * operator->() const
Definition cursor_ptr.h:95
T * operator->()
Definition cursor_ptr.h:86
T * get()
Definition cursor_ptr.h:88
本库所有组件所在的顶层命名空间。
@ T
Definition kb.h:72