DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
Clone.h
1
16
17
#pragma once
18
30
31
#if defined(Clone_RECURSES)
32
#error Recursive header files inclusion detected in Clone.h
33
#else
// defined(Clone_RECURSES)
35
#define Clone_RECURSES
36
37
#if !defined Clone_h
39
#define Clone_h
40
42
// Inclusions
43
#include <iostream>
44
#include "DGtal/base/Common.h"
45
#include "DGtal/base/CountedPtr.h"
46
#include "DGtal/base/CowPtr.h"
48
49
namespace
DGtal
50
{
51
53
// template class Clone
264
template
<
typename
T>
265
class
Clone
266
{
267
// ----------------------- Internal classes ------------------------------
268
protected
:
269
271
enum
Parameter
{
CONST_LEFT_VALUE_REF
,
LEFT_VALUE_REF
,
PTR
,
CONST_PTR
,
272
COW_PTR
,
COUNTED_PTR
,
RIGHT_VALUE_REF
,
COUNTED_PTR_OR_PTR
,
273
COUNTED_CONST_PTR_OR_CONST_PTR
};
279
void
display
(
const
std::string& method,
Parameter
p )
const
280
{
281
std::string sp;
282
switch
(p) {
283
case
CONST_LEFT_VALUE_REF
: sp =
"CONST_LEFT_VALUE_REF"
;
break
;
284
case
LEFT_VALUE_REF
: sp =
"LEFT_VALUE_REF"
;
break
;
285
case
PTR
: sp =
"PTR"
;
break
;
286
case
CONST_PTR
: sp =
"CONST_PTR"
;
break
;
287
case
COW_PTR
: sp =
"COW_PTR"
;
break
;
288
case
COUNTED_PTR
: sp =
"COUNTED_PTR"
;
break
;
289
case
RIGHT_VALUE_REF
: sp =
"RIGHT_VALUE_REF"
;
break
;
290
case
COUNTED_PTR_OR_PTR
: sp =
"COUNTED_PTR_OR_PTR"
;
break
;
291
case
COUNTED_CONST_PTR_OR_CONST_PTR
: sp =
"COUNTED_CONST_PTR_OR_CONST_PTR"
;
break
;
292
default
: sp =
"UNKNOWN"
;
break
;
293
}
294
trace
.
info
() <<
"[Clone<T>::"
<< method <<
" param="
295
<< sp <<
"]"
<< std::endl;
296
297
}
298
299
struct
TempPtr
{
304
inline
TempPtr
( T* ptr ) :
_ptr
( ptr ) {}
308
inline
~TempPtr
() { ASSERT(
_ptr
!= 0 );
delete
_ptr
; }
310
T*
_ptr
;
311
};
312
313
// ----------------------- Standard services ------------------------------
314
public
:
315
319
inline
~Clone
() {}
320
325
inline
Clone
(
const
Clone
& other )
326
:
myParam
( other.
myParam
),
myPtr
( other.
myPtr
) {}
327
333
inline
Clone
(
const
T & t )
334
:
myParam
(
CONST_LEFT_VALUE_REF
),
myPtr
( static_cast<const void*>( &t ) ) {}
335
342
inline
Clone
( T* ptrT )
343
:
myParam
(
PTR
),
myPtr
( static_cast<const void*>( ptrT ) ) {}
344
351
inline
Clone
(
const
CowPtr<T>
& ptrT )
352
:
myParam
(
COW_PTR
),
myPtr
( static_cast<const void*>( &ptrT ) ) {}
353
360
inline
Clone
(
const
CountedPtr<T>
& ptrT )
361
:
myParam
(
COUNTED_PTR
),
myPtr
( static_cast<const void*>( &ptrT ) ) {}
362
369
inline
Clone
( T && t )
370
:
myParam
(
RIGHT_VALUE_REF
),
myPtr
( static_cast<const void*>( &t ) ) {}
371
372
383
inline
operator
T()
const
384
{
385
// display( "operator T() const", myParam );
386
switch
(
myParam
) {
387
case
CONST_LEFT_VALUE_REF
:
388
return
T( *
static_cast<
const
T*
>
(
myPtr
) );
389
case
PTR
: {
390
TempPtr
tmp(
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) );
391
return
T( *
static_cast<
const
T*
>
(
myPtr
) );
392
}
// destroy acquired pointer.
393
case
COW_PTR
:
394
return
T( *
static_cast<
const
CowPtr<T>
*
>
(
myPtr
)->get() );
395
case
COUNTED_PTR
:
396
return
T( *
static_cast<
const
CountedPtr<T>
*
>
(
myPtr
)->get() );
397
case
RIGHT_VALUE_REF
:
398
return
T( std::move( *
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) ) );
399
default
: ASSERT(
false
&&
"[Clone::operator T() const] Invalid cast for given type. "
);
400
return
T( *
static_cast<
const
T*
>
(
myPtr
) );
401
}
402
}
403
414
inline
operator
CowPtr<T>
()
const
415
{
416
// display( "operator CowPtr<T>() const", myParam );
417
switch
(
myParam
) {
418
case
CONST_LEFT_VALUE_REF
:
419
return
CowPtr<T>
(
new
T( *
static_cast<
const
T*
>
(
myPtr
) ) );
420
case
PTR
:
421
return
CowPtr<T>
(
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) );
422
case
COW_PTR
:
423
return
CowPtr<T>
( *
static_cast<
const
CowPtr<T>
*
>
(
myPtr
) );
424
case
COUNTED_PTR
:
425
return
CowPtr<T>
( *
static_cast<
const
CountedPtr<T>
*
>
(
myPtr
),
true
);
426
case
RIGHT_VALUE_REF
:
427
return
CowPtr<T>
(
new
T( std::move( *
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) ) ) );
428
default
: ASSERT(
false
&&
"[Clone::operator CowPtr<T>() const] Invalid cast for given type. "
);
429
return
CowPtr<T>
( 0 );
430
}
431
}
432
443
inline
operator
CountedPtr<T>
()
const
444
{
445
// display( "operator CountedPtr<T>() const", myParam );
446
switch
(
myParam
) {
447
case
CONST_LEFT_VALUE_REF
:
448
return
CountedPtr<T>
(
new
T( *
static_cast<
const
T*
>
(
myPtr
) ) );
449
case
PTR
:
450
return
CountedPtr<T>
(
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) );
451
case
COW_PTR
:
452
return
CountedPtr<T>
(
new
T( *
static_cast<
const
CowPtr<T>
*
>
(
myPtr
)->get() ) );
453
case
COUNTED_PTR
:
454
return
CountedPtr<T>
( *
static_cast<
const
CountedPtr<T>
*
>
(
myPtr
) );
455
case
RIGHT_VALUE_REF
:
456
return
CountedPtr<T>
(
new
T( std::move( *
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) ) ) );
457
default
: ASSERT(
false
&&
"[Clone::operator CountedPtr<T>() const] Invalid cast for given type. "
);
458
return
CountedPtr<T>
( 0 );
459
}
460
}
461
475
inline
T*
operator&
()
const
476
{
477
// display( "T* operator &() const", myParam );
478
switch
(
myParam
) {
479
case
CONST_LEFT_VALUE_REF
:
480
return
new
T( *
static_cast<
const
T*
>
(
myPtr
) );
481
case
PTR
:
482
return
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) );
483
case
COW_PTR
:
484
return
new
T( *(
static_cast<
const
CowPtr<T>
*
>
(
myPtr
)->get() ) );
485
case
COUNTED_PTR
:
486
return
new
T( *(
static_cast<
const
CountedPtr<T>
*
>
(
myPtr
)->get() ) );
487
case
RIGHT_VALUE_REF
:
488
return
new
T( std::move( *
const_cast<
T*
>
(
static_cast<
const
T*
>
(
myPtr
) ) ) );
489
default
: ASSERT(
false
&&
"[T* Clone::operator&() const] Invalid address for given type. "
);
490
return
0;
491
}
492
}
493
494
// ------------------------- Private Data --------------------------------
495
private
:
497
const
Parameter
myParam
;
499
const
void
*
const
myPtr
;
500
501
502
// ------------------------- Hidden services ------------------------------
503
private
:
504
509
Clone
();
510
511
518
Clone
&
operator=
(
const
Clone
& other );
519
520
// ------------------------- Internals ------------------------------------
521
private
:
522
523
};
// end of class Clone
524
525
}
// namespace DGtal
526
527
529
// Includes inline functions.
530
531
// //
533
534
#endif
// !defined Clone_h
535
536
#undef Clone_RECURSES
537
#endif
// else defined(Clone_RECURSES)
DGtal::Clone::operator=
Clone & operator=(const Clone &other)
DGtal::Clone::operator&
T * operator&() const
Definition
Clone.h:475
DGtal::Clone::Clone
Clone(T *ptrT)
Definition
Clone.h:342
DGtal::Clone::myParam
const Parameter myParam
Characterizes the type of the input parameter at clone instantiation.
Definition
Clone.h:497
DGtal::Clone::display
void display(const std::string &method, Parameter p) const
Definition
Clone.h:279
DGtal::Clone::Clone
Clone(const CowPtr< T > &ptrT)
Definition
Clone.h:351
DGtal::Clone::Clone
Clone(const Clone &other)
Definition
Clone.h:325
DGtal::Clone::myPtr
const void *const myPtr
Stores the address of the input parameter for further use.
Definition
Clone.h:499
DGtal::Clone::Clone
Clone(const CountedPtr< T > &ptrT)
Definition
Clone.h:360
DGtal::Clone::Clone
Clone(const T &t)
Definition
Clone.h:333
DGtal::Clone::~Clone
~Clone()
Definition
Clone.h:319
DGtal::Clone::Clone
Clone(T &&t)
Definition
Clone.h:369
DGtal::Clone::Parameter
Parameter
Internal class that allows to distinguish the different types of parameters.
Definition
Clone.h:271
DGtal::Clone::COUNTED_PTR_OR_PTR
@ COUNTED_PTR_OR_PTR
Definition
Clone.h:272
DGtal::Clone::LEFT_VALUE_REF
@ LEFT_VALUE_REF
Definition
Clone.h:271
DGtal::Clone::COUNTED_PTR
@ COUNTED_PTR
Definition
Clone.h:272
DGtal::Clone::COUNTED_CONST_PTR_OR_CONST_PTR
@ COUNTED_CONST_PTR_OR_CONST_PTR
Definition
Clone.h:273
DGtal::Clone::RIGHT_VALUE_REF
@ RIGHT_VALUE_REF
Definition
Clone.h:272
DGtal::Clone::CONST_LEFT_VALUE_REF
@ CONST_LEFT_VALUE_REF
Definition
Clone.h:271
DGtal::Clone::COW_PTR
@ COW_PTR
Definition
Clone.h:272
DGtal::Clone::CONST_PTR
@ CONST_PTR
Definition
Clone.h:271
DGtal::Clone::PTR
@ PTR
Definition
Clone.h:271
DGtal::Clone::Clone
Clone()
DGtal::CountedPtr
Aim: Smart pointer based on reference counts.
Definition
CountedPtr.h:80
DGtal::CountedPtr::CountedPtr
CountedPtr(T *p=0)
Definition
CountedPtr.h:119
DGtal::CowPtr
Aim: Copy on write shared pointer.
Definition
CowPtr.h:68
DGtal::CowPtr::CowPtr
CowPtr(T *p=0) noexcept
Definition
CowPtr.h:74
DGtal::Trace::info
std::ostream & info()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
DGtal::Clone::TempPtr
Internal class that is used for a late deletion of an acquired pointer.
Definition
Clone.h:299
DGtal::Clone::TempPtr::_ptr
T * _ptr
Acquired pointer.
Definition
Clone.h:310
DGtal::Clone::TempPtr::~TempPtr
~TempPtr()
Definition
Clone.h:308
DGtal::Clone::TempPtr::TempPtr
TempPtr(T *ptr)
Definition
Clone.h:304
src
DGtal
base
Clone.h
Generated on
for DGtal by
1.18.0