-
Notifications
You must be signed in to change notification settings - Fork 2
/
areafit.h
2001 lines (1674 loc) · 50.7 KB
/
areafit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//$ nocpp
/**
* @file areafit.h
*
* @brief The inclusion file for the CAreaFitter class.
*
* @section license License
*
* Copyright (c) 2016 Aleksey Vaneev
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef AREAFIT_INCLUDED
#define AREAFIT_INCLUDED
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
namespace afit {
//#define VOX_AREAFITTER_TEST 1 // Define to enable statistical testing of the
// area fitting algorithm.
/**
* "libvox" default memory buffer allocator. Used to supply various storage
* classes with the required memory allocation, re-allocation and freeing
* functions.
*/
class CVoxMemAllocator
{
protected:
static void* allocmem( const size_t Size )
{
return( :: malloc( Size ));
}
static void* reallocmem( void* p, const size_t Size )
{
return( :: realloc( p, Size ));
}
static void freemem( void* p )
{
:: free( p );
}
};
/**
* Memory buffer object. Allows easier handling of memory blocks allocation
* and automatic deallocation for arrays (buffers) consisting of elements of
* specified class. Contains "local storage" which is used when allocated
* block size is below a certain constant. This allows to optimize allocation
* and reallocation of small blocks greatly.
*
* This class manages memory space only - it does not perform element class
* construction operations.
*
* LocSizeBytes template parameter specifies how much memory a local storage
* should take. This value divided by sizeof( T ) will be assigned to
* LocalStorageSize value (result can be 0).
* CAlloc - data allocator and base class.
*/
template< class T, int LocSizeBytes = 48, class CAlloc = CVoxMemAllocator >
class CBuffer : public CAlloc
{
public:
CBuffer()
: Data( NULL )
, Capacity( 0 )
{
}
CBuffer( const int aCapacity )
{
allocinit( aCapacity );
}
CBuffer( const CBuffer& Source )
{
allocinit( Source.Capacity );
memcpy( Data, Source.Data, Capacity * sizeof( T ));
}
~CBuffer()
{
freeData();
}
CBuffer& operator = ( const CBuffer& Source )
{
copy( Source );
return( *this );
}
/**
* Function copies contents of the specified buffer to *this buffer.
*
* @param Source Source buffer.
*/
void copy( const CBuffer& Source )
{
alloc( Source.Capacity );
memcpy( Data, Source.Data, Capacity * sizeof( T ));
}
/**
* Function allocates memory so that the specified number of elements
* can be stored in *this buffer object.
*
* @param aCapacity Storage for this number of elements to allocate.
*/
void alloc( const int aCapacity )
{
freeData();
allocinit( aCapacity );
}
/**
* Function deallocates any previously allocated buffer.
*/
void free()
{
freeData();
Data = NULL;
Capacity = 0;
}
/**
* Function returns pointer to the element buffer.
*/
T* getPtr() const
{
return( Data );
}
/**
* Function returns capacity of the element buffer.
*/
int getCapacity() const
{
return( Capacity );
}
/**
* Function changes allocated capacity of the buffer. If the specified
* capacity is lower than the previously allocated one, buffer's capacity
* will be simply decreased. Otherwise buffer will be reallocated.
* This function is different from alloc() function in that it does not
* release any previously allocated buffer.
*
* @param NewCapacity New buffer capacity (in elements).
*/
void setCapacity( const int NewCapacity )
{
if( NewCapacity <= Capacity )
{
Capacity = NewCapacity;
}
else
{
increaseCapacity( NewCapacity );
}
}
/**
* Function increases capacity so that the specified number of elements
* can be stored. This function increases the previous capacity value by
* half the current capacity value until space for the required number of
* elements is available.
*
* @param ReqCapacity Required capacity.
*/
void updateCapacity( const int ReqCapacity )
{
if( ReqCapacity <= Capacity )
{
return;
}
int NewCapacity = Capacity;
while( NewCapacity < ReqCapacity )
{
NewCapacity += NewCapacity / 2 + 1;
}
increaseCapacity( NewCapacity );
}
/**
* Functions moves contents of the specified Source buffer to *this
* buffer, and frees allocated buffer in this Source buffer.
*
* @param Source Source buffer.
*/
void moveFrom( CBuffer& Source )
{
freeData();
if( Source.Data != (T*) Source.LocalStorage )
{
Data = Source.Data;
}
else
{
memcpy( &LocalStorage, &Source.LocalStorage,
Source.Capacity * sizeof( T ));
Data = (T*) LocalStorage;
}
Capacity = Source.Capacity;
Source.Data = NULL;
Source.Capacity = 0;
}
/**
* Functions moves contents of the specified buffer defined as pointer to
* memory, to *this buffer.
*
* @param Source Source buffer. Should be allocated via the CAlloc class.
* @param SourceLen Source buffer length (number of elements).
*/
void moveFrom( T* Source, const int SourceLen )
{
freeData();
Data = Source;
Capacity = SourceLen;
}
/**
* Function reallocates *this buffer to a larger size so that it will be
* able to hold the specified number of elements.
*
* @param NewCapacity New capacity.
*/
void increaseCapacity( const int NewCapacity )
{
if( Data == NULL )
{
allocinit( NewCapacity );
return;
}
if( Data == (T*) LocalStorage )
{
if( NewCapacity > LocalStorageSize )
{
Data = (T*) CAlloc :: allocmem( NewCapacity * sizeof( T ));
memcpy( Data, &LocalStorage, Capacity * sizeof( T ));
}
}
else
{
Data = (T*) CAlloc :: reallocmem( Data,
NewCapacity * sizeof( T ));
}
Capacity = NewCapacity;
}
/**
* Element referencing operators.
*/
operator T* () const
{
return( Data );
}
private:
static const int LocalStorageSize = ( LocSizeBytes +
(int) sizeof( T ) - 1 ) / (int) sizeof( T ); ///< The number of
/// elements stored locally.
T* Data; ///< Element buffer pointer. Points either to the LocalStorage or
/// a buffer allocated via the CAlloc :: allocmem function.
int Capacity; ///< Element buffer capacity.
uint8_t LocalStorage[ LocalStorageSize * (int) sizeof( T )]; ///< Local
/// element storage.
/**
* Internal element buffer allocation function used during object
* construction.
*
* @param Capacity Storage for this number of elements to allocate.
*/
void allocinit( const int aCapacity )
{
if( aCapacity <= LocalStorageSize )
{
Data = (T*) LocalStorage;
}
else
{
Data = (T*) CAlloc :: allocmem( aCapacity * sizeof( T ));
}
Capacity = aCapacity;
}
/**
* Function frees a previously allocated Data buffer if it does not point
* to object's internal storage.
*/
void freeData()
{
if( Data != (T*) LocalStorage )
{
CAlloc :: freemem( Data );
}
}
};
/**
* Base array class. Implements handling of a linear array of objects of class
* T, addressable via operator[]. New object insertions are quick since
* implementation uses prior space allocation (capacity), thus not requiring
* frequent memory block reallocations.
*
* CItemAlloc is a class that implements allocation of individual array items.
* Also used as a base class.
* CBufferAlloc is a class that provides memory allocation functions to array
* buffer.
*/
template< class T, int LocSizeBytes, class CItemAlloc,
class CBufferAlloc = CVoxMemAllocator >
class CArrayBase : public CItemAlloc
{
public:
CArrayBase()
: ItemCount( 0 )
{
}
CArrayBase( const int aItemCount )
: ItemCount( 0 )
{
setItemCount( aItemCount );
}
CArrayBase( const CArrayBase& Source )
: ItemCount( 0 )
, Items( Source.getItemCount() )
{
while( ItemCount < Source.getItemCount() )
{
CItemAlloc :: allocateItem( Items + ItemCount,
Source[ ItemCount ]);
ItemCount++;
}
}
~CArrayBase()
{
CItemAlloc :: deallocateItems( Items, ItemCount, 0 );
}
/**
* Operator creates copy of the specified array.
*
* @param Source Array whose copy to create.
*/
CArrayBase& operator = ( const CArrayBase& Source )
{
CItemAlloc :: deallocateItems( Items, ItemCount, 0 );
ItemCount = 0;
const int NewCount = Source.ItemCount;
Items.updateCapacity( NewCount );
while( ItemCount < NewCount )
{
CItemAlloc :: allocateItem( Items + ItemCount,
Source[ ItemCount ]);
ItemCount++;
}
return( *this );
}
/**
* Operator appends Source array to the end of *this array.
*
* @param Source Array to append to *this array.
*/
void operator += ( const CArrayBase& Source )
{
if( Source.ItemCount == 0 )
{
return;
}
const int NewCount = ItemCount + Source.ItemCount;
Items.updateCapacity( NewCount );
int i = 0;
while( ItemCount < NewCount )
{
CItemAlloc :: allocateItem( Items + ItemCount, Source[ i ]);
ItemCount++;
i++;
}
}
/**
* Element referencing operators.
*/
T& operator []( const int Index )
{
return( CItemAlloc :: getRef( Items + Index ));
}
const T& operator []( const int Index ) const
{
return( CItemAlloc :: getConstRef( Items + Index ));
}
/**
* Function returns number of allocated items.
*/
int getItemCount() const
{
return( ItemCount );
}
/**
* Function changes number of allocated items. New items are created with
* the default constructor. If NewCount is below the current item count,
* items that are above NewCount range will be destructed.
*
* @param NewCount New requested item count.
*/
void setItemCount( const int NewCount )
{
if( NewCount > ItemCount )
{
Items.updateCapacity( NewCount );
CItemAlloc :: allocateItems( Items, ItemCount, NewCount );
}
else
{
CItemAlloc :: deallocateItems( Items, ItemCount, NewCount );
}
ItemCount = NewCount;
}
/**
* Function creates a new object of type T with the default constructor
* and adds this object to the array.
*
* @return References to added object.
*/
T& add()
{
if( ItemCount == Items.getCapacity() )
{
Items.increaseCapacity( ItemCount * 2 + 1 );
}
CItemAlloc :: allocateItem( Items + ItemCount );
ItemCount++;
return( (*this)[ ItemCount - 1 ]);
}
/**
* Function creates a new object of type T with the copy constructor
* and adds this object to the array.
*
* @param Source Source object to pass to object's copy constructor.
* @return References to added object.
*/
template< class TS >
T& add( const TS& Source )
{
if( ItemCount == Items.getCapacity() )
{
Items.increaseCapacity( ItemCount * 2 + 1 );
}
CItemAlloc :: allocateItem( Items + ItemCount, Source );
ItemCount++;
return( (*this)[ ItemCount - 1 ]);
}
/**
* Function searches for the given item, and if the item was not found,
* creates a new object of type T with the copy constructor and adds this
* object to the array. Otherwise existing item will be returned.
*
* @param Source Source object to pass to object's copy constructor.
* @return References to added object.
*/
template< class TS >
T& addUnique( const TS& Source )
{
const int Index = find( Source );
if( Index != -1 )
{
return( (*this)[ Index ]);
}
return( add( Source ));
}
/**
* Function creates a new object of type T with the default constructor,
* and adds this object to the array at the specified position.
*
* @param Index Item will be inserted before existing element with
* this index.
* @return Reference to the newly added object.
*/
T& insert( const int Index )
{
if( ItemCount == Items.getCapacity() )
{
Items.increaseCapacity( ItemCount * 2 + 1 );
}
memmove( &Items[ Index + 1 ], &Items[ Index ],
( ItemCount - Index ) * sizeof( StorageType ));
CItemAlloc :: allocateItem( Items + Index );
ItemCount++;
return( (*this)[ Index ]);
}
/**
* Function creates a new object of type T with the copy constructor,
* and adds this object to the array at the specified position.
*
* @param Index Item will be inserted before existing element with
* this index.
* @param Source Source object to pass to object's copy constructor.
* @return Reference to the newly added object.
*/
template< class TS >
T& insert( const int Index, const TS& Source )
{
if( ItemCount == Items.getCapacity() )
{
Items.increaseCapacity( ItemCount * 2 + 1 );
}
memmove( &Items[ Index + 1 ], &Items[ Index ],
( ItemCount - Index ) * sizeof( StorageType ));
CItemAlloc :: allocateItem( Items + Index, Source );
ItemCount++;
return( (*this)[ Index ]);
}
/**
* Function erases (destroys) item at the selected index. Items past the
* selected index will be moved to 1 index location down (e.g. from 2 to 1,
* from 22 to 21).
*
* @param Index Index of item to erase.
*/
void erase( const int Index )
{
CItemAlloc :: deallocateItem( Items + Index );
ItemCount--;
memcpy( &Items[ Index ], &Items[ Index + 1 ],
( ItemCount - Index ) * sizeof( StorageType ));
}
/**
* Function searches for the specified object in the array, and erases
* the found object.
*
* @param Source Object to use for comparison.
*/
template< class TR >
void remove( const TR& Source )
{
int i;
for( i = 0; i < ItemCount; i++ )
{
if( (*this)[ i ] == Source )
{
erase( i );
return;
}
}
}
/**
* Function searches for the specified object in the array, and returns
* its index. Function returns -1 if no required object was found.
*
* @param Source Object to use for comparison.
*/
template< class TS >
int find( const TS& Source ) const
{
int i;
for( i = 0; i < ItemCount; i++ )
{
if( (*this)[ i ] == Source )
{
return( i );
}
}
return( -1 );
}
/**
* Function erases all items of *this array.
*/
void clear()
{
CItemAlloc :: deallocateItems( Items, ItemCount, 0 );
ItemCount = 0;
}
private:
typedef typename CItemAlloc :: StorageType StorageType;
int ItemCount; ///< Number of items available in array.
CBuffer< StorageType, LocSizeBytes, CBufferAlloc > Items; ///< Item array.
};
/**
* Array allocator suitable for working with simple object types. These object
* types should not require default constructor and destructor calls, and
* should be assignable via "operator =" operation, without requiring prior
* initialized state. References or pointers to array items that use allocator
* of this type should not be saved if array is going to be changed after such
* value saving.
*
* In general case, CArrayAllocator should be used when working with arrays
* that store elementary types like "int", "uint8_t", "double", pointers to
* various objects, and structures built from these types.
*/
template< class T >
class CArrayAllocator
{
protected:
typedef T StorageType;
/**
* Function allocates a new item.
*
* @param p Pointer to item's storage.
*/
static void allocateItem( void* const p )
{
}
/**
* Function allocates a new copy of an item.
*
* @param p Pointer to item's storage.
* @param Source Source item to copy.
*/
template< class TS >
static void allocateItem( void* const p, const TS& Source )
{
*(T*) p = Source;
}
/**
* Function deallocate an item.
*
* @param p Pointer to item's storage.
*/
static void deallocateItem( void* const p )
{
}
/**
* Function returns a reference to an item.
*
* @param p Pointer to item's storage.
*/
static T& getRef( void* const p )
{
return( *(T*) p );
}
/**
* Function returns a const reference to an item.
*
* @param p Pointer to item's storage.
*/
static const T& getConstRef( const void* const p )
{
return( *(T*) p );
}
/**
* Function allocates items in an array so that the current item count
* becomes new item count.
*
* @param Items Pointer to items' storage.
* @param ItemCount Current item count.
* @param NewItemCount Required item count.
*/
static void allocateItems( StorageType* const Items, int ItemCount,
const int NewItemCount )
{
}
/**
* Function deallocates items in an array so that the current item count
* becomes new item count.
*
* @param Items Pointer to items' storage.
* @param ItemCount Current item count.
* @param NewItemCount Required item count.
*/
static void deallocateItems( StorageType* const Items, int ItemCount,
const int NewItemCount )
{
}
};
/**
* Array class that uses CArrayAllocator for handling arrays of simple
* relocatable item types.
*/
template< typename T, int LocSizeBytes = sizeof( int ) * 8 >
class CArray : public CArrayBase< T, LocSizeBytes, CArrayAllocator< T > >
{
public:
CArray()
: CArrayBase< T, LocSizeBytes, CArrayAllocator< T > >()
{
}
CArray( const int aItemCount )
: CArrayBase< T, LocSizeBytes, CArrayAllocator< T > >( aItemCount )
{
}
CArray( const CArray& Source )
: CArrayBase< T, LocSizeBytes, CArrayAllocator< T > >( Source )
{
}
};
/**
* Class that implements recursive area fitting. This class is designed in a
* way to allow several fitting threads to be started with each thread testing
* its own range of possibilities.
*
* Each thread operates on a single CAreaFitter object and its results are
* best locally.
*/
/**
* An alternative CArrayAllocator allocator which performs contructor and
* destructor calls.
*/
template< class T >
class CInitArrayAllocator
{
protected:
struct StorageType
{
T value;
StorageType()
: value()
{
}
StorageType( const T& Source )
: value( Source )
{
}
void* operator new( size_t Size, void* p )
{
return( p );
}
void operator delete( void* p )
{
}
};
static void allocateItem( void* const p )
{
new( p ) StorageType();
}
template< class TS >
static void allocateItem( void* const p, const TS& Source )
{
new( p ) StorageType( Source );
}
static void deallocateItem( void* const p )
{
( (StorageType*) p ) -> ~StorageType();
}
static T& getRef( void* const p )
{
return(( (StorageType*) p ) -> value );
}
static const T& getConstRef( const void* const p )
{
return(( (StorageType*) p ) -> value );
}
static void allocateItems( StorageType* const Items, int ItemCount,
const int NewItemCount )
{
while( ItemCount < NewItemCount )
{
allocateItem( Items + ItemCount );
ItemCount++;
}
}
static void deallocateItems( StorageType* const Items, int ItemCount,
const int NewItemCount )
{
while( ItemCount > NewItemCount )
{
ItemCount--;
deallocateItem( Items + ItemCount );
}
}
};
/**
* Array class that uses CInitArrayAllocator for handling arrays of simple
* relocatable item types with constructor and destructor calls.
*/
template< typename T, int LocSizeBytes = sizeof( int ) * 8 >
class CInitArray : public CArrayBase< T, LocSizeBytes,
CInitArrayAllocator< T > >
{
public:
CInitArray()
: CArrayBase< T, LocSizeBytes, CInitArrayAllocator< T > >()
{
}
CInitArray( const int aItemCount )
: CArrayBase< T, LocSizeBytes,
CInitArrayAllocator< T > >( aItemCount )
{
}
CInitArray( const CInitArray& Source )
: CArrayBase< T, LocSizeBytes, CInitArrayAllocator< T > >( Source )
{
}
};
/**
* An auxiliary class that can be used for storing temporary pointers that
* can be deallocated by calling operator "delete".
*/
template< class T >
class CPtrKeeper
{
public:
CPtrKeeper()
: Object( NULL )
{
}
template< class T2 >
CPtrKeeper( T2 const aObject )
: Object( aObject )
{
}
~CPtrKeeper()
{
delete Object;
}
template< class T2 >
CPtrKeeper& operator = ( T2 const aObject )
{
reset();
Object = aObject;
return( *this );
}
T operator -> () const
{
return( Object );
}
operator T () const
{
return( Object );
}
/**
* Function resets keeped pointer and deletes it.
*/
void reset()
{
T DelObj = Object;
Object = NULL;
delete DelObj;
}
/**
* Function returns the keeped pointer and removes it from the keeper.
*/
T unkeep()
{
T ResObject = Object;
Object = NULL;
return( ResObject );
}
private:
T Object;
};