/usr/include/terralib/kernel/TeSharedPtr.h is in libterralib-dev 4.0.0-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
You should have received a copy of the GNU Lesser General Public
License along with this library.
The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/
#ifndef TESHAREDPTR_H
#define TESHAREDPTR_H
#include "TeMutex.h"
#include "TeAgnostic.h"
/**
* @class This class deals with automatic object deletion when no
* reference to that object no longer exists.
* @note This is a thread-safe class.
* @author Emiliano F. Castejon <castejon@dpi.inpe.br>
* @ingroup Utils
* @example TeSharedPtr_test.cpp Shows how to use this class.
* @note Shared-mode - The object will be automaticaly deleted
* when apropriate.
* @note Not-Shared-mode - The object will NOT be deleted.
*/
template< class T >
class TeSharedPtr {
public :
/**
* @brief Default Constructor( Shared mode ).
*/
explicit TeSharedPtr();
/**
* @brief Alternative Constructor( Shared mode ).
*
* @param pointer A pointer the the active object.
*/
explicit TeSharedPtr( T* pointer );
/**
* @brief Alternative Constructor.
*
* @param external External shared pointer reference.
*/
TeSharedPtr( const TeSharedPtr< T >& external );
/**
* @brief Alternative Constructor( Not-shared mode ).
* @param objectReference A reference to an existing
* object.
* @note By using this constructor this pointer instance
* will be constructed in not-shared mode and will not delete
* the object instance.
*/
explicit TeSharedPtr( T& objectReference );
/**
* @deprecated DEPRECATED - Will be removed in the future (Use the
* method TeSharedPtr( T& objectReference ) to construct pointers
* in not-shared mode ).
* @brief Alternative Constructor.
* @param pointer A pointer the the active object.
* @param not_shared_flag A flag indication for a static
* assignment ( the pointed object will not be deleted at the
* this object destruction ).
*/
explicit TeSharedPtr( T* pointer, bool not_shared_flag );
/**
* @brief Default Destructor
*/
~TeSharedPtr();
/**
* @brief Verifies if the current pointer points to an active object.
*
* @return true if active, false if not.
*/
inline bool isActive() const
{
return ( reference_ != 0 );
};
/**
* @brief Verifies if the current pointer is shared.
*
* @return true if shared, false if not.
*/
inline bool isShared() const
{
return ( ! not_shared_flag_ );
};
/**
* @brief Reset the active instance deleting any pointed object
* if the current instance is in shared mode and no more references
* to the pointed object exists.
*/
inline void reset()
{
reset( 0, 0 );
};
/**
* @brief Reset the active instance the the new pointer (shared mode).
*
* @param pointer A pointer the the active object.
*/
inline void reset( T* pointer )
{
reset( pointer, false );
};
/**
* @brief Reset the active instance the the new pointer.
*
* @param pointer A pointer the the active object.
* @param not_shared_flag A flag indication for a static
* assignment ( the pointed object will not be deleted at the
* this object destruction ).
*/
void reset( T* pointer, bool not_shared_flag );
// Operator = overload.
const TeSharedPtr< T >& operator=( const TeSharedPtr< T >& external );
/**
* @brief Operator * overload.
*
* @return The pointed object reference.
*/
inline T& operator*() const
{
TEAGN_DEBUG_CONDITION( isActive(),
"Trying to use an inactive shared pointer instance" );
return *reference_;
};
/**
* @brief Operator -> overload.
*
* @return The pointed object pointer.
*/
inline T* operator->() const
{
TEAGN_DEBUG_CONDITION( isActive(),
"Trying to use an inactive shared pointer instance" );
return reference_;
};
// Operator == overload.
inline bool operator==( const TeSharedPtr< T >& external ) const
{
return ( ( reference_ == external.reference_ ) &&
( counter_ == external.counter_ ) &&
( not_shared_flag_ == external.not_shared_flag_ ) );
};
/**
* @brief Operator< overload.
*
* @param external The external instance reference.
* @return true if the current pointer pointed address has a lower value
* then the external shared pointer pointed address.
*/
inline bool operator<( const TeSharedPtr< T >& external ) const
{
return ( reference_ < external.reference_ );
};
/**
* @brief Operator> overload.
*
* @param external The external instance reference.
* @return true if the current pointer pointed address has a lower value
* then the external shared pointer pointed address.
*/
inline bool operator>( const TeSharedPtr< T >& external ) const
{
return ( reference_ > external.reference_ );
};
// Operator != overload.
inline bool operator!=( const TeSharedPtr< T >& external ) const
{
return ( ! operator==( external ) );
};
/**
* @brief A Naked Pointer to the encapsulated object.
*
* @return The internal instance pointer.
*/
inline T* nakedPointer() const
{
return reference_;
};
/**
* @brief The number of current references to the pointed object.
*
* @return The number of current references to the pointed object.
*/
unsigned long int getRefCount() const;
protected :
/**
* @brief This instance locking mutex.
*/
mutable TeMutex this_lock_instance_;
/**
* @brief A pointer to the shared counter locking mutex (default:0).
*/
mutable TeMutex* counter_lock_instance_ptr_;
/**
* @brief A pointer to the current number of active users of this
* pointer (default:0).
*/
mutable unsigned long int* counter_;
/**
* @brief A pointer to the current object encapsulated by this
* shared pointer (default:0).
*/
mutable T* reference_;
/**
* @brief A flag indicating if this shared pointer was created by a
* dynamic assignment ( false value ) or by a static
* assignment(if true, the encapsulated object will not
* be deleted when apropriate) (default:false).
*/
mutable bool not_shared_flag_;
/**
* @brief Set all internal variables to default values.
*
*/
void init();
};
template< class T >
TeSharedPtr< T >::TeSharedPtr()
{
init();
}
template< class T >
TeSharedPtr< T >::TeSharedPtr( T* pointer )
{
init();
reset( pointer, false );
}
template< class T >
TeSharedPtr< T >::TeSharedPtr( const TeSharedPtr< T >& external )
{
init();
operator=( external );
}
template< class T >
TeSharedPtr< T >::TeSharedPtr( T& objectReference )
{
init();
reset( &objectReference, true );
}
template< class T >
TeSharedPtr< T >::TeSharedPtr( T* pointer, bool not_shared_flag )
{
init();
reset( pointer, not_shared_flag );
}
template< class T >
TeSharedPtr< T >::~TeSharedPtr()
{
reset( 0, 0 );
}
template< class T >
void TeSharedPtr< T >::reset( T* pointer, bool not_shared_flag )
{
this_lock_instance_.lock();
/* updating the shared objects */
if( ( reference_ != 0 ) && ( ! not_shared_flag_ ) ) {
counter_lock_instance_ptr_->lock();
--(*counter_);
/* Cleanning the shared pointed objects if necessary */
if( (*counter_) == 0 ) {
delete counter_;
delete reference_;
delete counter_lock_instance_ptr_;
} else {
counter_lock_instance_ptr_->unLock();
}
}
init();
/* Acquiring the pointed object */
if( pointer != 0 ) {
if( ! not_shared_flag ) {
counter_ = new unsigned long int;
(*counter_) = 1;
counter_lock_instance_ptr_ = new TeMutex;
}
reference_ = pointer;
not_shared_flag_ = not_shared_flag;
}
this_lock_instance_.unLock();
}
template< class T >
const TeSharedPtr< T >& TeSharedPtr< T >::operator=(
const TeSharedPtr< T >& external )
{
if( ( &external ) != this )
{
reset( 0, 0 );
this_lock_instance_.lock();
if( (&external) != 0 )
{
external.this_lock_instance_.lock();
if( external.reference_ )
{
reference_ = external.reference_;
counter_lock_instance_ptr_ = external.counter_lock_instance_ptr_;
counter_ = external.counter_;
reference_ = external.reference_;
not_shared_flag_ = external.not_shared_flag_;
if( ! not_shared_flag_ )
{
(*counter_) = (*counter_) + 1;
}
}
external.this_lock_instance_.unLock();
}
this_lock_instance_.unLock();
}
return *this;
}
template< class T >
unsigned long int TeSharedPtr< T >::getRefCount() const
{
if( reference_ ) {
if( counter_) {
return (*counter_);
} else {
return 1;
}
} else {
return 0;
}
}
template< class T >
void TeSharedPtr< T >::init()
{
counter_ = 0;
reference_ = 0;
counter_lock_instance_ptr_ = 0;
not_shared_flag_ = false;
}
#endif
|