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
|
/*s***************************************************************************
*
* Copyright (c), Ilya Valuev 2006 All Rights Reserved.
*
* Author : Ilya Valuev, MIPT, Moscow, Russia
*
* Project : ivutils
*
*****************************************************************************/
/*s****************************************************************************
* $Log: refobj.h,v $
* Revision 1.2 2011/06/11 16:53:55 valuev
* sync with LAMMPS
*
* Revision 1.1 2011/06/10 17:15:07 morozov
* First Windows project with the correct directory structure
*
* Revision 1.15 2010/10/07 11:20:31 valuev
* preliminary program restart
*
* Revision 1.14 2009/07/24 05:08:46 valuev
* Sync with FDTD, added molecule setup
*
* Revision 1.33 2009/05/19 21:50:17 valuev
* Added TestRay for plane
*
* Revision 1.32 2009/03/23 22:00:48 lesha
* const mngptr &operator=(const mngarg<T> &arg) is added
*
* Revision 1.31 2009/01/30 13:54:05 valuev
* restructured as a library
*
* Revision 1.30 2009/01/21 09:28:15 lesha
* refvector::clear is added
*
* Revision 1.29 2009/01/15 07:31:07 lesha
* *** empty log message ***
*
* Revision 1.28 2009/01/14 10:02:36 lesha
* operator [] is added to mngptr
*
* Revision 1.27 2008/04/29 01:23:59 lesha
* nothing important
*
* Revision 1.26 2008/02/28 08:57:27 lesha
* shptr::free() is made public
*
* Revision 1.25 2008/02/27 13:37:23 lesha
* shptr is added
*
* Revision 1.24 2008/01/22 10:14:05 lesha
* mngarg is added
*
* Revision 1.23 2007/08/08 10:55:37 lesha
* constructor in refvector is correted
*
* Revision 1.22 2007/07/10 19:52:44 lesha
* make gcc compilable
*
* Revision 1.21 2007/07/06 12:23:37 valuev
* made compilable with icc 9
*
* Revision 1.20 2007/06/22 09:42:25 valuev
* *** empty log message ***
*
* Revision 1.19 2007/06/17 00:51:44 lesha
* refobj, refcounter :: reset is modified for ptr==this->ptr case
*
* Revision 1.18 2007/06/05 16:30:53 lesha
* make gcc compilable
*
* Revision 1.17 2007/06/05 11:37:53 lesha
* make gcc compilable
*
* Revision 1.16 2007/06/05 11:07:04 lesha
* make gcc compilable
*
* Revision 1.15 2007/06/04 14:03:55 lesha
* *** empty log message ***
*
* Revision 1.14 2007/05/31 18:00:42 lesha
* *** empty log message ***
*
* Revision 1.13 2007/05/31 16:57:30 lesha
* ref_sequence is added
*
* Revision 1.12 2007/05/31 01:25:01 lesha
* new version of mng_ptr, pencil etc
*
* Revision 1.11 2007/02/20 10:26:11 valuev
* added newlines at end of file
*
* Revision 1.10 2007/02/16 10:16:51 valuev
* allowed array mngptr
*
* Revision 1.4 2007/02/16 09:40:32 valuev
* Added Nudged Elastic Band saddle point search
*
* Revision 1.3 2006/12/20 14:29:33 valuev
* Updated workflow, sync with FDTD
*
* Revision 1.9 2006/12/14 08:42:36 valuev
* reformulated detector
* projectors, corrected open file limit control, tested Fourier sceleton
*
* Revision 1.8 2006/11/29 18:05:05 valuev
* made the code compilable with g++
*
* Revision 1.7 2006/11/29 17:17:01 valuev
* added using base_t::member for ANSI-compatibility
*
* Revision 1.6 2006/11/29 17:11:59 valuev
* added includes
*
* Revision 1.5 2006/11/28 09:16:59 valuev
* Fixed vectors storing managed pointers
*
* Revision 1.4 2006/11/24 20:17:31 valuev
* Added CVS headers
*
*******************************************************************************/
#ifndef _REFOBJ_H
#define _REFOBJ_H
# include <utility>
# include <vector>
# include <map>
using namespace std;
template<class T>
class mngarg: public pair<T *, int>{
public:
typedef pair<T *, int> base_t;
using base_t::second;
using base_t::first;
mngarg(T *ptr, int managed=0): pair<T*,int>(ptr,managed){}
template<class A>
mngarg(const mngarg<A> &arg): pair<T*,int>(arg.first,arg.second){}
};
template<class T>
mngarg<T> make_mngarg(T *ptr, int managed=1){
return mngarg<T>(ptr,managed);
}
/// managed pointer
/// managed==0 do not delete
/// managed==1 delete
/// (NOT IMPLEMENTED) managed==2 copy and delete, requires copy constructor
/// flag 0x8 -- delete as array
template<class T>
class mngptr: public pair<T *, int>{
public:
typedef pair<T *, int> base_t;
typedef T *pointer;
using base_t::second;
using base_t::first;
mngptr(T* ptr=NULL, int managed=0): pair<T*,int>(ptr,managed){
//if(managed==2)ptr= new T(*ptr);
}
mngptr(const mngarg<T> &arg): pair<T*,int>(arg.first,arg.second){}
const mngptr &operator=(const mngarg<T> &arg){
reset(arg.first,arg.second);
return *this;
}
void reset(T* ptr=NULL, int managed=0){
if(second && first && first!=ptr){
if(second&0x8)delete [] first;
else delete first;
}
first=ptr;
second=managed;
}
void reset(const mngarg<T> &arg){
reset(arg.first,arg.second);
}
T* ptr() const {
return first;
}
T* operator->() const {
return first;
}
T& operator*() const{
return *first;
}
T& operator[] (int i) const{
return *(first+i);
}
int managed() const {
return second;
}
~mngptr(){
reset();
}
};
# if 0
template <template<class _type> class cont_tt, class T>
class refcontainer: public cont_tt< T * >{
protected:
int man;
public:
typedef cont_tt< T * > base_t;
typedef typename base_t::iterator iterator;
typedef typename base_t::const_iterator const_iterator;
refcontainer(int smanaged=0):man(smanaged){}
refcontainer(size_t n, int smanaged=0):base_t(n),man(smanaged){}
void set_managed(int sman){
man=sman;
}
~refcontainer(){
if(man){
size_t i, n=base_t::size();
for(i=0;i<n;i++)
if((*this)[i]) delete (*this)[i];
}
}
};
# endif
template < class T >
class refvector: public std::vector< T * >{
protected:
int man;
public:
typedef vector<T*> base_t;
typedef typename base_t::iterator iterator;
typedef typename base_t::const_iterator const_iterator;
refvector(int smanaged=0):man(smanaged){}
// refvector(size_t n, int smanaged=0):base_t(n),man(smanaged){} // ambigious constructors
refvector(size_t n, int smanaged):base_t(n),man(smanaged){}
void set_managed(int sman){
man=sman;
}
~refvector(){
clear();
}
void clear() {
if(man){
iterator it=base_t::begin();
for(;it!=base_t::end();++it)
if(*it)
delete (*it);
}
base_t::clear();
}
iterator erase(iterator it){
if(man && *it)
delete (*it);
return base_t::erase(it);
}
};
template <class key_tt, class T >
class refmap: public std::map<key_tt, T * >{
protected:
int man;
public:
typedef std::map<key_tt, T * > base_t;
typedef typename base_t::iterator iterator;
typedef typename base_t::const_iterator const_iterator;
refmap(int smanaged=0):man(smanaged){}
refmap(size_t n, int smanaged=0):base_t(n),man(smanaged){}
void set_managed(int sman){
man=sman;
}
~refmap(){
clear();
}
void clear() {
if(man){
for(typename base_t::iterator i=base_t::begin();i!=base_t::end();++i)
if(i->second) delete i->second;
}
base_t::clear();
}
iterator erase(iterator it){
if(man && it->second)
delete it->second;
return base_t::erase(it);
}
};
template<class T>
class delete_ptr{
public:
void operator()(T *ptr){
delete ptr;
}
};
template<class T, class delete_t=delete_ptr<T> >
class shptr{
template<class Y, class Z> friend class shptr;
T *p;
int *num; //if num==NULL than p is not managed (as in mngptr)
void set(T *p_, int managed){
p=p_;
if(p&&managed){
num=new int;
*num=1;
}
else num=NULL;
}
template<class Y>
void set(const Y &other){
p=other.p;
if(p){
num=other.num;
if(num)(*num)++;
}
else num=NULL;
}
void set(const shptr &other){
p=other.p;
if(p){
num=other.num;
if(num)(*num)++;
}
else num=NULL;
}
public:
shptr(T* p=NULL, int managed=1){
set(p,managed);
}
shptr(const mngarg<T> &arg){
set(arg.first,arg.second);
}
template<class Y>
shptr(const Y &other){
set(other);
}
shptr(const shptr &other){
set(other);
}
void reset(T *p_, int managed=1) {
if(p!=p_){
free();
set(p_,managed);
}
}
void reset(const shptr &other) {
if(this!=&other){
free();
set(other);
}
}
const shptr &operator=(T *p) {
reset(p,0);
return *this;
}
const shptr &operator=(const mngarg<T> &arg) {
reset(arg.first,arg.second);
return *this;
}
template<class Y>
const shptr &operator=(const Y &other){
reset(other);
return *this;
}
const shptr &operator=(const shptr &other) {
reset(other);
return *this;
}
virtual ~shptr(){
free();
}
void free(){
if(p){
if(num){
(*num)--;
if((*num)==0){
delete_t()(p);
delete num;
}
num=NULL;
}
p=NULL;
}
}
bool valid() const {
return p!=NULL;
}
T* ptr() const {
return p;
}
T* operator->() const {
return p;
}
T& operator*() const{
return *p;
}
};
/*
class RefObject{
void *ref_data;
int ref_count;
public:
protected:
virtual void delete_data(void *data);
virtual void *new_data();
virtual void *copy_data(void *data);
}
class RefA: public RefObject{
public:
refA(){
ref_data = new A;
}
refA(const refA &other){
Ref(other);
}
refA& operator=(const refA &other){
if(ref_data != other.ref_data){
Ref(other);
}
return *this;
}
private:
void delete_data(void *data){
delete (A *)data;
}
void *new_data(){
return (void *)new A;
}
void *copy_data(void *data){
return (void *)(new A(*((A*)data)));
}
}*/
#endif
|