CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_debug_mem_h 00008 #define _lucene_debug_mem_h 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "lucenebase.h" 00015 00016 //Macro for creating new objects 00017 #if defined(LUCENE_ENABLE_MEMLEAKTRACKING) 00018 #define _CLNEW new(__FILE__, __LINE__) 00019 #define LUCENE_BASE public CL_NS(debug)::LuceneBase 00020 #elif defined(LUCENE_ENABLE_REFCOUNT) 00021 #define _CLNEW new 00022 #define LUCENE_BASE public CL_NS(debug)::LuceneBase 00023 #else 00024 #define _CLNEW new 00025 #define LUCENE_BASE public CL_NS(debug)::LuceneVoidBase 00026 #define LUCENE_BASE_CHECK(obj) (obj)->dummy__see_mem_h_for_details 00027 #endif 00028 #define _CL_POINTER(x) (x==NULL?NULL:(x->__cl_addref()>=0?x:x)) //return a add-ref'd object 00029 #define LUCENE_REFBASE public CL_NS(debug)::LuceneBase //this is the base of classes who *always* need refcounting 00030 00031 #if defined(_DEBUG) 00032 #if !defined(LUCENE_BASE_CHECK) 00033 #define LUCENE_BASE_CHECK(x) 00034 #endif 00035 #else 00036 #undef LUCENE_BASE_CHECK 00037 #define LUCENE_BASE_CHECK(x) 00038 #endif 00039 00040 //Macro for creating new arrays 00041 #ifdef LUCENE_ENABLE_MEMLEAKTRACKING 00042 #define _CL_NEWARRAY(type,size) (type*)CL_NS(debug)::LuceneBase::__cl_voidpadd(new type[(size_t)size],__FILE__,__LINE__,(size_t)size); 00043 #define _CLDELETE_ARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__); delete [] x; x=NULL;} 00044 #define _CLDELETE_LARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__);delete [] x;} 00045 #ifndef _CLDELETE_CARRAY 00046 #define _CLDELETE_CARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__);delete [] x; x=NULL;} 00047 #define _CLDELETE_LCARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__);delete [] x;} 00048 #endif 00049 #else 00050 #define _CL_NEWARRAY(type,size) new type[size] 00051 #define _CLDELETE_ARRAY(x) if (x!=NULL){delete [] x; x=NULL;} 00052 #define _CLDELETE_LARRAY(x) if (x!=NULL){delete [] x;} 00053 #ifndef _CLDELETE_CARRAY 00054 #define _CLDELETE_CARRAY(x) if (x!=NULL){delete [] x; x=NULL;} 00055 #define _CLDELETE_LCARRAY(x) if (x!=NULL){delete [] x;} 00056 #endif 00057 #endif 00058 //a shortcut for deleting a carray and all its contents 00059 #define _CLDELETE_CARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE_CARRAY(x[xcda]);}_CLDELETE_ARRAY(x)}; 00060 #define _CLDELETE_ARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE(x[xcda]);}_CLDELETE_ARRAY(x)}; 00061 #ifndef _CLDELETE_CaARRAY 00062 #define _CLDELETE_CaARRAY _CLDELETE_CARRAY 00063 #define _CLDELETE_LCaARRAY _CLDELETE_LCARRAY 00064 #endif 00065 00066 //Macro for deleting 00067 #ifdef LUCENE_ENABLE_REFCOUNT 00068 #define _CLDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; x=NULL; } 00069 #define _CLLDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; } 00070 #else 00071 #define _CLDELETE(x) if (x!=NULL){ LUCENE_BASE_CHECK(x); delete x; x=NULL; } 00072 #define _CLLDELETE(x) if (x!=NULL){ LUCENE_BASE_CHECK(x); delete x; } 00073 #endif 00074 00075 //_CLDECDELETE deletes objects which are *always* refcounted 00076 #define _CLDECDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; x=NULL; } 00077 #define _CLLDECDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; } 00078 00079 //_VDelete should be used for deleting non-clucene objects. 00080 //when using reference counting, _CLDELETE casts the object 00081 //into a LuceneBase*. 00082 #define _CLVDELETE(x) if(x!=NULL){delete x; x=NULL;} 00083 00084 template<typename T> 00085 class Array: LUCENE_BASE{ 00086 public: 00087 T* values; 00088 size_t length; 00089 00090 void deleteAll(){ 00091 for (size_t i=0;i<length;i++) 00092 _CLDELETE(values[i]); 00093 _CLDELETE_ARRAY(values); 00094 } 00095 void deleteArray(){ 00096 _CLDELETE_ARRAY(values); 00097 } 00098 00099 Array(){ 00100 values = NULL; 00101 length = 0; 00102 } 00103 Array(T* values, size_t length){ 00104 this->values = values; 00105 this->length = length; 00106 } 00107 Array(size_t length){ 00108 this->values = _CL_NEWARRAY(T,length); 00109 this->length = length; 00110 } 00111 ~Array(){} 00112 00113 const T operator[](size_t _Pos) const 00114 { 00115 if (length <= _Pos){ 00116 _CLTHROWA(CL_ERR_IllegalArgument,"vector subscript out of range"); 00117 } 00118 return (*(values + _Pos)); 00119 } 00120 T operator[](size_t _Pos) 00121 { 00122 if (length <= _Pos){ 00123 _CLTHROWA(CL_ERR_IllegalArgument,"vector subscript out of range"); 00124 } 00125 return (*(values + _Pos)); 00126 } 00127 00128 }; 00129 00130 #endif //_lucene_debug_lucenebase_