NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
ScriptObjectInterface.h
Go to the documentation of this file.
00001 
00010 #pragma once
00011 
00012 // Standard Includes
00013 
00014 // Library Includes
00015 #include <threading.h>
00016 
00017 // Local Includes
00018 
00019 // Forward Declarations
00020 
00021 // Typedefs
00022 
00023 // Local Constants
00024 
00025 // Classes
00026 class ScriptObjectInterface {
00027 public:
00028   ScriptObjectInterface(unsigned int ref_count = 1) {
00029     this->refCount = ref_count;
00030   }
00031   
00032   ScriptObjectInterface(const ScriptObjectInterface& script_object) {
00033     this->refCount = 1; // New copies have little to do with the old copy in this case.
00034   }
00035   
00036   virtual ~ScriptObjectInterface() {}
00037   
00039   void Addref(void) {
00040     Threading::WriteLock w_lock(this->mutex);
00041     
00042     // Increase the reference counter
00043     this->refCount++;
00044   }
00045   
00047   void Release(void) {
00048     // Decrease ref count 
00049     {
00050       Threading::WriteLock w_lock(this->mutex);
00051       --this->refCount;
00052     }
00053     
00054     // Delete if it reaches 0
00055     if (this->refCount <= 0) {
00056       delete this;
00057     }
00058   }
00059   
00061   unsigned int GetReferenceCount(void) {
00062     Threading::ReadLock r_lock(this->mutex);
00063     
00064     return refCount;
00065   }
00066   
00067 private:
00068   ScriptObjectInterface& operator=(const ScriptObjectInterface&); 
00069   
00070 private:
00071   mutable Threading::ReadWriteMutex mutex;
00072   unsigned int refCount; 
00073 };