SGObject.h

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2008 Soeren Sonnenburg
00008  * Copyright (C) 2008 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef __SGOBJECT_H__
00012 #define __SGOBJECT_H__
00013 
00014 #include "lib/io.h"
00015 #include "base/Parallel.h"
00016 #include "base/Version.h"
00017 
00018 class CSGObject;
00019 class CIO;
00020 
00021 // define reference counter macros
00022 #if defined(HAVE_SWIG) && !defined(HAVE_R)
00023 #define SG_REF(x) { if (x) x->ref(); }
00024 #define SG_UNREF(x) { if (x) x->unref(); } 
00025 #else
00026 #define SG_REF(x)
00027 #define SG_UNREF(x)
00028 #endif // HAVE_SWIG
00029 
00038 class CSGObject
00039 {
00040 public:
00041     virtual ~CSGObject()
00042     {
00043     }
00044 
00045 #ifdef HAVE_SWIG
00046 #ifndef HAVE_R
00047     inline CSGObject() : refcount(0)
00048     {
00049     }
00050 
00051     inline CSGObject(const CSGObject& orig)
00052         : refcount(0) , parallel(orig.parallel), io(orig.io)
00053     {
00054     }
00055 
00056     inline int32_t ref()
00057     {
00058         ++refcount;
00059         SG_DEBUG("ref():%ld obj:%p\n", refcount, this);
00060         return refcount;
00061     }
00062 
00063     inline int32_t ref_count() const
00064     {
00065         SG_DEBUG("ref_count(): refcount is: %d\n", refcount);
00066         return refcount;
00067     }
00068 
00069     inline int32_t unref()
00070     {
00071         if (refcount==0 || --refcount==0)
00072         {
00073             SG_DEBUG("unref():%ld obj:%p destroying\n", refcount, this);
00074             //don't do this yet for static interfaces (as none is
00075             //calling ref/unref properly)
00076             delete this;
00077             return 0;
00078         }
00079         else
00080         {
00081             SG_DEBUG("unref():%ld obj:%p decreased\n", refcount, this);
00082             return refcount;
00083         }
00084     }
00085 
00086 private:
00087     int32_t refcount;
00088 #else //HAVE_R
00089     inline CSGObject()
00090     {
00091     }
00092 
00093     inline CSGObject(const CSGObject& orig)
00094         : parallel(orig.parallel), io(orig.io)
00095     {
00096     }
00097 #endif
00098 
00099 public:
00100     CParallel parallel;
00101     CIO io;
00102     CVersion version;
00103 #else // HAVE_SWIG
00104 public:
00105     inline CSGObject()
00106     {
00107     }
00108 
00109     inline CSGObject(const CSGObject& orig)
00110     {
00111     }
00112 
00113 public:
00114     static CParallel parallel;
00115     static CIO io;
00116     static CVersion version;
00117 #endif // HAVE_SWIG
00118 };
00119 #endif // __SGOBJECT_H__

SHOGUN Machine Learning Toolbox - Documentation