⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gasmartptr.h

📁 遗传算法做的排课系统
💻 H
字号:

#ifndef __GA_SMART_PTR_H__
#define __GA_SMART_PTR_H__

#include "..\ExportImport.h"
#include "..\CallConvention.h"
#include "..\Threading\GaThreading.h"

using namespace Threading;

namespace Common
{
	template <typename T>
	class GaSmartPtr;

	// Manages safe location for sotring data
	template <typename T>
	class GaSmartStorage
	{

	friend class GaSmartPtr<T>;

	private:

		// Number of references pointing to data
		mutable int _count;

		// Pointer to the actual data
		T* _data;

	public:

		// Makes new safe storage location and returns smart pointer.
		DLL_EXPORT
		static GaSmartPtr<T> GACALL MakeInstance(T* data);

		// Increase count of references which point to this location
		DLL_EXPORT
		static int GACALL AddReference(GaSmartStorage<T>* location);

		// Decrease count of references which point to this location
		DLL_EXPORT
		static int GACALL RemoveReference(GaSmartStorage<T>* location);

		// Initialize safe storage location
		DLL_EXPORT
		GaSmartStorage(T* data);

		// Frees memory allocated for data
		DLL_EXPORT
		~GaSmartStorage();

		// Returns pointer to actual data
		DLL_EXPORT
		T* GACALL GetData() const;

		// Returns the number of references pointing to this location
		DLL_EXPORT
		int GACALL GetCount() const;

	};// END CLASS DEFINITION GaSmartStorage

	// Smart pointer
	template <typename T>
	class GaSmartPtr
	{

	private:

		// Used for locking of concurrent access to changes of pointer.
		volatile mutable unsigned long _lock;

		// Direct pointerto actual data
		T* _data;

		// Pointer to safe location
		GaSmartStorage<T>* _location;

	public:

		// Initialize safe pointer.
		DLL_EXPORT
		GaSmartPtr(GaSmartStorage<T>& storage);

		// Copy constructor
		DLL_EXPORT
		GaSmartPtr(const GaSmartPtr<T>& pointer);

		// NULL pointer.
		DLL_EXPORT
		GaSmartPtr();

		// When smart pointer is out of scope it decrease count of references pointing to safe location
		DLL_EXPORT 
		~GaSmartPtr();

		// Returns pointer to data on safe location at which this pointer points
		DLL_EXPORT
		T* GACALL operator ->() const;

		// Returns reference to the chromosome
		DLL_EXPORT
		T& GACALL operator *() const;

		// Change location at which this smart pointer points
		DLL_EXPORT
		GaSmartPtr<T>& GACALL operator =(const GaSmartPtr<T>& lhs);

		// Change location at which this smart pointer points
		DLL_EXPORT
		GaSmartPtr<T>& GACALL operator =(GaSmartStorage<T>& lhs);

		// Compare to see if two smart pointer points to same safe location.
		DLL_EXPORT
		bool GACALL operator ==(const GaSmartPtr<T>& lhs) const;

		// Returns TRUE if the pointer is NULL
		DLL_EXPORT
		bool GACALL IsNULL() const;

	private:

		// Locks this pointer from concurrent changes.
		void Lock() const;

		// Locks this smart pointer and another to prevent concurrent changes when assigning pointers.
		void Lock(const GaSmartPtr<T>& second) const;

		// Unlocks this smart pointer
		void Unlock() const;

		// Unlocks this and another smart pointer
		void Unlock(const GaSmartPtr<T>& second);

	public:

		// NULL pointer
		DLL_EXPORT
		static const GaSmartPtr<T> NullPtr;

	};// END CLASS DEFINITION GaSmartPtr

} // Common

#endif //__GA_SMART_PTR_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -