ESLA
Embeddable Scriting LAnguage
Stanford University, Rock Fracture Project research group
© 2003
00001 /************************************************************************* 00002 * ESLA: Embeddable Scripting LAnguage 00003 * Copyright (C) 2003 Frantz Maerten 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License as 00007 * published by the Free Software Foundation; either version 2 of the 00008 * License, or (at your option) any later version. 00009 * 00010 * If you modify this software, you should contact the author, include 00011 * a notice giving the name of the person performing the modification, 00012 * the date of modification, and the reason for such modification. 00013 * 00014 * Note that the GNU General Public License does not permit 00015 * incorporating the Software into proprietary programs. 00016 * 00017 * Contact: Frantz Maerten 00018 * frantz@pangea.stanford.edu 00019 * 00020 * Dept. of Geological & Environmental Sciences 00021 * Stanford University 00022 * Stanford, CA 94305-2115 00023 * USA 00024 * 00025 * Adapted from Bruno Levy (levy@loria.fr) 00026 *************************************************************************/ 00027 00028 00029 #ifndef COUNTED__ 00030 #define COUNTED__ 00031 00032 #include "../esla_namespace.h" 00033 #include <iostream> 00034 #include <assert.h> 00035 00036 BEGIN_LIB_NAMESPACE 00037 00038 class Counted { 00039 public: 00040 Counted() ; 00041 virtual ~Counted() ; 00042 00043 void inc_ref() const ; 00044 void dec_ref() const ; 00045 bool is_shared() const ; 00046 00047 static void inc_ref(const Counted* counted) ; 00048 static void dec_ref(const Counted* counted) ; 00049 00050 // For debug purpose only 00051 int nbr_ref() const ; 00052 00053 private: 00054 int nb_refs_ ; 00055 } ; 00056 00057 //------------------------------------------------------------------ 00058 inline void Counted::inc_ref() const { 00059 Counted* non_const_this = (Counted *)this ; 00060 non_const_this-> nb_refs_++ ; 00061 } 00062 00063 inline void Counted::dec_ref() const { 00064 Counted* non_const_this = (Counted *)this ; 00065 non_const_this-> nb_refs_-- ; 00066 assert(nb_refs_ >= 0) ; 00067 00068 if(nb_refs_ == 0) { 00069 delete this ; 00070 } 00071 } 00072 00073 inline bool Counted::is_shared() const { 00074 return (nb_refs_ > 1) ; 00075 } 00076 00077 inline int Counted::nbr_ref() const { 00078 return nb_refs_ ; 00079 } 00080 00081 00082 END_LIB_NAMESPACE 00083 00084 //------------------------------------------------------------------ 00085 00086 #endif 00087