object.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00033 #ifndef __object__header__
00034 #define __object__header__
00035 
00036 #include "gpucalc/prerequisites.h"
00037 #include "gpucalc/exception.h"
00038 
00039 namespace gpucalc
00040 {
00055 #define OBJ_DISABLE_COPY(class_name)  class_name(const class_name &); class_name & operator = (const class_name &);
00056 
00057  
00058  
00059  
00060  
00061  
00062 
00068  class _GpuCalcExport Object
00069  {
00070   OBJ_DISABLE_COPY(Object)
00071   
00072   public:
00073    Object(const std::string & ClassName, const std::string & ObjectName): mClassName(ClassName), mObjectName(ObjectName), mSpecificParameters(),
00074     mIsLocked(false)
00075    {
00076    }
00077 
00078 
00079    virtual ~Object()
00080    {
00081     /*for (SpecificParametersMap::iterator i = mSpecificParameters.begin(); i != mSpecificParameters.end(); ++i)
00082     {
00083      free(i->second.Pointer);
00084     }
00085     */
00086    }
00087 
00088 
00092    const std::string & getClassName() const
00093    {
00094     return mClassName;
00095    }
00096 
00097 
00101    const std::string & getObjectName() const
00102    {
00103     return mObjectName;
00104    }
00105 
00106 
00113    bool isLocked() const
00114    {
00115     return mIsLocked;
00116    }
00117 
00118 
00128    void getSpecificParameter(const std::string & ParameterName, void * ParameterValue)
00129    {
00130     SpecificParametersMap::iterator i = mSpecificParameters.find(ParameterName);
00131     if (i != mSpecificParameters.end())
00132     {
00133      i->second.copyValue(ParameterValue);
00134     }
00135     else
00136     {
00137      Except<ERR_ITEM_NOT_FOUND>(this, "Parameter \"" + ParameterName + "\" does not exists.", "Object::getSpecificParameter()", __FILE__, __LINE__);
00138     }
00139    }
00140 
00141 
00142   private:
00143    std::string mClassName;
00144    std::string mObjectName;
00145 
00146 
00147    class Any
00148    {
00149     public:
00150      Any(): mPointer(0), mSize(0), mUseCount(0)
00151      {
00152      }
00153 
00154 
00155      Any(size_t size): mPointer(0), mSize(size),mUseCount(0)
00156      {
00157       mPointer = malloc(size);
00158       mUseCount = new size_t(1);
00159      }
00160 
00161 
00162      Any(void * from, size_t size): mSize(size)
00163      {
00164       mPointer = malloc(size);
00165       memcpy(mPointer, from, size);
00166       mUseCount = new size_t(1);
00167      }
00168 
00169 
00170      Any(const Any & any): mPointer(any.mPointer), mSize(any.mSize), mUseCount(any.mUseCount)
00171      {
00172       if (mUseCount)
00173       {
00174        ++(*mUseCount);
00175       }
00176      }
00177 
00178 
00179      Any & operator = (const Any & any)
00180      {
00181       if (mPointer == any.mPointer)
00182       {
00183        return *this;
00184       }
00185       else
00186       {
00187        Any tmp(any);
00188        swap(tmp);
00189        return *this;
00190       }
00191      }
00192 
00193 
00194      ~Any()
00195      {
00196       release();
00197      }
00198 
00199 
00200      void copyValue(void * To)
00201      {
00202       memcpy(To, mPointer, mSize);
00203      }
00204 
00205 
00206     private:
00207      void * mPointer;
00208      size_t mSize;
00209      size_t * mUseCount;
00210 
00211 
00212      void release()
00213      {
00214       if (mUseCount)
00215       {
00216        if (--(*mUseCount) == 0)
00217        {
00218         destroy();
00219        }
00220       }
00221 
00222      }
00223 
00224 
00225      void destroy()
00226      {
00227       free(mPointer);
00228       mSize = 0;
00229       delete mUseCount;
00230      }
00231 
00232 
00233      void swap(Any & other)
00234      {
00235       std::swap(mPointer, other.mPointer);
00236       std::swap(mSize, other.mSize);
00237       std::swap(mUseCount, other.mUseCount);
00238      }
00239 
00240    };
00241 
00242 
00243    typedef std::map<std::string, Any> SpecificParametersMap;
00244    SpecificParametersMap mSpecificParameters;
00245 
00246 
00247    bool mIsLocked;
00248 
00249 
00250 
00251   protected:
00252 
00253 
00254    void addSpecificParameter(const std::string & ParameterName, void * ParameterValue, size_t Size)
00255    {
00256     SpecificParametersMap::iterator i = mSpecificParameters.find(ParameterName);
00257     if (i != mSpecificParameters.end())
00258     {
00259      mSpecificParameters.erase(i);
00260     }
00261     mSpecificParameters[ParameterName] = Any(ParameterValue, Size);
00262    }
00263 
00264 
00265    void lock()
00266    {
00267     mIsLocked = true;
00268    }
00269 
00270 
00271    void unlock()
00272    {
00273     mIsLocked = false;
00274    }
00275  };
00276 }
00277 
00278 #endif

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6