From 253693b8e42a751030051d3d619201dc6f5b12db Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期五, 16 五月 2025 11:14:11 +0800
Subject: [PATCH] 1.CGlassPool实现; 2.CGlass翻译逻辑优化; 3.在Glass列表页,Glass项的显示,增加显示贴合的Glass的id

---
 SourceCode/Bond/Servo/Servo.vcxproj         |    2 
 SourceCode/Bond/Servo/CArm.cpp              |    6 +
 SourceCode/Bond/Servo/Context.cpp           |   25 ++++-
 SourceCode/Bond/Servo/CGlass.cpp            |    7 +
 SourceCode/Bond/Servo/CGlass.h              |    1 
 SourceCode/Bond/Servo/CGlassPool.h          |   32 ++++++++
 SourceCode/Bond/Servo/Model.cpp             |    5 +
 SourceCode/Bond/Servo/CLoadPort.cpp         |    4 
 SourceCode/Bond/Servo/CDoubleGlass.cpp      |    6 +
 SourceCode/Bond/Servo/CGlassPool.cpp        |   89 ++++++++++++++++++++++
 SourceCode/Bond/Servo/Servo.vcxproj.filters |    2 
 SourceCode/Bond/Servo/CEquipmentPage2.cpp   |   11 ++
 SourceCode/Bond/Servo/CEquipment.cpp        |    4 
 SourceCode/Bond/Servo/Model.h               |    2 
 SourceCode/Bond/Servo/CMaster.cpp           |   10 +-
 SourceCode/Bond/Servo/Context.h             |    5 +
 16 files changed, 191 insertions(+), 20 deletions(-)

diff --git a/SourceCode/Bond/Servo/CArm.cpp b/SourceCode/Bond/Servo/CArm.cpp
index e698692..064ab30 100644
--- a/SourceCode/Bond/Servo/CArm.cpp
+++ b/SourceCode/Bond/Servo/CArm.cpp
@@ -56,13 +56,17 @@
 
 	int CArm::tempStore(CGlass* pGlass)
 	{
-		// 保证列表中只存储一个物料
+		// 原:保证列表中只存储一个物料
+		// 修改为:先清空之前的,再添加当前pGlass, 如果pGlass有buddy,也要加入列表中
 		Lock();
 		for (auto item : m_glassList) {
 			item->release();
 		}
 		m_glassList.clear();
 		addGlassToList(pGlass);
+		if (pGlass->getBuddy() != nullptr) {
+			addGlassToList(pGlass->getBuddy());
+		}
 		Unlock();
 
 		if (m_listener.onDataChanged != nullptr) {
diff --git a/SourceCode/Bond/Servo/CDoubleGlass.cpp b/SourceCode/Bond/Servo/CDoubleGlass.cpp
index 1701145..782685a 100644
--- a/SourceCode/Bond/Servo/CDoubleGlass.cpp
+++ b/SourceCode/Bond/Servo/CDoubleGlass.cpp
@@ -1,5 +1,7 @@
 #include "stdafx.h"
+#include "Servo.h"
 #include "CDoubleGlass.h"
+#include "CGlassPool.h"
 
 
 namespace SERVO {
@@ -72,11 +74,11 @@
 			ULONGLONG ulGlass1;
 			ar >> ulGlass1;
 			if (ulGlass1 != 0) {
-				m_pGlass1 = new CGlass();
+				m_pGlass1 = theApp.m_model.m_glassPool.allocaGlass();
 				m_pGlass1->serialize(ar);
 			}
 			if (ulGlass1 != 0) {
-				m_pGlass2 = new CGlass();
+				m_pGlass2 = theApp.m_model.m_glassPool.allocaGlass();
 				m_pGlass2->serialize(ar);
 			}
 
diff --git a/SourceCode/Bond/Servo/CEquipment.cpp b/SourceCode/Bond/Servo/CEquipment.cpp
index a9d456a..f7e2424 100644
--- a/SourceCode/Bond/Servo/CEquipment.cpp
+++ b/SourceCode/Bond/Servo/CEquipment.cpp
@@ -3,6 +3,8 @@
 #include "ToolUnits.h"
 #include <regex>
 #include "CArm.h"
+#include "CGlassPool.h"
+#include "Servo.h"
 
 
 #define CHECK_READ_STEP_SIGNAL(addr, data, size) {							\
@@ -315,7 +317,7 @@
 			int count;
 			ar >> count;
 			for (int i = 0; i < count; i++) {
-				CGlass* pGlass = new CGlass();
+				CGlass* pGlass = theApp.m_model.m_glassPool.allocaGlass();
 				pGlass->serialize(ar);
 				addGlassToList(pGlass);
 			}
diff --git a/SourceCode/Bond/Servo/CEquipmentPage2.cpp b/SourceCode/Bond/Servo/CEquipmentPage2.cpp
index ca4b445..3bffbe7 100644
--- a/SourceCode/Bond/Servo/CEquipmentPage2.cpp
+++ b/SourceCode/Bond/Servo/CEquipmentPage2.cpp
@@ -74,8 +74,9 @@
 	ListView_SetImageList(m_listCtrl.GetSafeHwnd(), imageList, LVSIL_SMALL);
 	m_listCtrl.InsertColumn(0, _T(""), LVCFMT_RIGHT, width[0]);
 	m_listCtrl.InsertColumn(1, _T("ID"), LVCFMT_LEFT, width[1]);
-	m_listCtrl.InsertColumn(2, _T("鏃堕棿"), LVCFMT_LEFT, width[2]);
-	m_listCtrl.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
+	m_listCtrl.InsertColumn(2, _T("璐村悎"), LVCFMT_LEFT, width[2]);
+	m_listCtrl.InsertColumn(3, _T("鏃堕棿"), LVCFMT_LEFT, width[3]);
+	m_listCtrl.SetColumnWidth(3, LVSCW_AUTOSIZE_USEHEADER);
 
 
 	ASSERT(m_pEquipment);
@@ -84,10 +85,14 @@
 	for (auto item : list) {
 		item->addRef();				
 		item->release();		// 閲婃斁list涓殑寮曠敤
+
+		SERVO::CGlass* pBuddy = item->getBuddy();
 		int index = m_listCtrl.InsertItem(m_listCtrl.GetItemCount(), _T(""));
 		m_listCtrl.SetItemData(index, (DWORD_PTR)item);
 		m_listCtrl.SetItemText(index, 1, item->getID().c_str());
-
+		if (pBuddy != nullptr) {
+			m_listCtrl.SetItemText(index, 2, pBuddy->getID().c_str());
+		}
 	}
 
 	return TRUE;  // return TRUE unless you set the focus to a control
diff --git a/SourceCode/Bond/Servo/CGlass.cpp b/SourceCode/Bond/Servo/CGlass.cpp
index e2b6259..77e292f 100644
--- a/SourceCode/Bond/Servo/CGlass.cpp
+++ b/SourceCode/Bond/Servo/CGlass.cpp
@@ -12,6 +12,11 @@
 
 	CGlass::~CGlass()
 	{
+
+	}
+
+	void CGlass::reset()
+	{
 		CPath* pPath = m_pPath;
 		while (pPath != nullptr) {
 			CPath* pTemp = pPath->getNext();
@@ -20,7 +25,7 @@
 		}
 		m_pPath = nullptr;
 
-		if (m_pBuddy != nullptr	&& m_type == MaterialsType::G1) {
+		if (m_pBuddy != nullptr) {
 			m_pBuddy->release();
 		}
 	}
diff --git a/SourceCode/Bond/Servo/CGlass.h b/SourceCode/Bond/Servo/CGlass.h
index ffeeccc..8ed33c2 100644
--- a/SourceCode/Bond/Servo/CGlass.h
+++ b/SourceCode/Bond/Servo/CGlass.h
@@ -20,6 +20,7 @@
 	public:
 		CGlass();
 		virtual ~CGlass();
+		void reset();
 
 	public:
 		virtual std::string& getClassName();
diff --git a/SourceCode/Bond/Servo/CGlassPool.cpp b/SourceCode/Bond/Servo/CGlassPool.cpp
new file mode 100644
index 0000000..7e83c32
--- /dev/null
+++ b/SourceCode/Bond/Servo/CGlassPool.cpp
@@ -0,0 +1,89 @@
+#include "stdafx.h"
+#include "CGlassPool.h"
+
+
+#define DEFAULT_GLASS_POOL_SIZE				1024
+
+namespace SERVO {
+	CGlassPool::CGlassPool()
+	{
+		InitializeCriticalSection(&m_criticalSection);
+	}
+
+
+	CGlassPool::~CGlassPool()
+	{
+		DeleteCriticalSection(&m_criticalSection);
+	}
+
+	void CGlassPool::initPool()
+	{
+		auto onReleaseAny = [&](void* pAny) -> void {
+			// 回收Glass
+			freeGlass((CGlass*)pAny);
+		};
+
+
+		// 分配CGlass池
+		Lock();
+		for (int i = 0; i < DEFAULT_GLASS_POOL_SIZE; i++) {
+			CGlass* pGlass = new CGlass();
+			pGlass->setOnRelease(onReleaseAny);
+			m_lsitAvailable.push_back(pGlass);
+		}
+		Unlock();
+	}
+
+	void CGlassPool::term()
+	{
+		Lock();
+		for (auto item : m_lsitAvailable) {
+			delete item;
+		}
+		m_lsitAvailable.clear();
+
+		for (auto item : m_mapOccupation) {
+			delete item.second;
+		}
+		m_mapOccupation.clear();
+		Unlock();
+	}
+
+	CGlass* CGlassPool::allocaGlass()
+	{
+
+		Lock();
+		CGlass* pGlass = NULL;
+		if (!m_lsitAvailable.empty()) {
+			pGlass = m_lsitAvailable.front();
+			m_lsitAvailable.pop_front();
+			m_mapOccupation[pGlass] = pGlass;
+		}
+
+		Unlock();
+
+		return pGlass;
+	}
+
+	void CGlassPool::freeGlass(CGlass* pGlass)
+	{
+		// 回收CAny
+		Lock();
+		pGlass->reset();
+		auto iter = m_mapOccupation.find(pGlass);
+		if (iter != m_mapOccupation.end()) {
+			m_lsitAvailable.push_back(iter->second);
+			m_mapOccupation.erase(iter);
+		}
+		Unlock();
+	}
+
+	int CGlassPool::getAvailableSize()
+	{
+		Lock();
+		int nSize = (int)(m_lsitAvailable.size());
+		Unlock();
+
+		return nSize;
+	}
+}
diff --git a/SourceCode/Bond/Servo/CGlassPool.h b/SourceCode/Bond/Servo/CGlassPool.h
new file mode 100644
index 0000000..c6e53a9
--- /dev/null
+++ b/SourceCode/Bond/Servo/CGlassPool.h
@@ -0,0 +1,32 @@
+#pragma once
+#include "CGlass.h"
+#include <map>
+#include <list>
+
+
+namespace SERVO {
+	class CGlassPool
+	{
+	public:
+		CGlassPool();
+		~CGlassPool();
+
+	public:
+		void initPool();
+		void term();
+		CGlass* allocaGlass();
+		void freeGlass(CGlass* pGlass);
+		int getAvailableSize();
+
+	private:
+		inline void Lock() { EnterCriticalSection(&m_criticalSection); }
+		inline void Unlock() { LeaveCriticalSection(&m_criticalSection); }
+
+
+	private:
+		CRITICAL_SECTION m_criticalSection;
+		std::map<void*, CGlass*> m_mapOccupation;			// 使用中的
+		std::list<CGlass*> m_lsitAvailable;					// 空闲的
+	};
+}
+
diff --git a/SourceCode/Bond/Servo/CLoadPort.cpp b/SourceCode/Bond/Servo/CLoadPort.cpp
index ff98b63..d3879c3 100644
--- a/SourceCode/Bond/Servo/CLoadPort.cpp
+++ b/SourceCode/Bond/Servo/CLoadPort.cpp
@@ -1,5 +1,7 @@
 #include "stdafx.h"
 #include "CLoadPort.h"
+#include "CGlassPool.h"
+#include "Servo.h"
 
 
 #define CHECK_READ_STEP_SIGNAL2(addr, data, size) {							\
@@ -533,7 +535,7 @@
 				js.setGlass2Id(szBuffer);
 			}
 
-			CGlass* pGlass = new CGlass();
+			CGlass* pGlass = theApp.m_model.m_glassPool.allocaGlass();
 			pGlass->setID(szBuffer);
 			pGlass->setJobDataB(&jb);
 			pGlass->setType(type);
diff --git a/SourceCode/Bond/Servo/CMaster.cpp b/SourceCode/Bond/Servo/CMaster.cpp
index be5aab9..29ba607 100644
--- a/SourceCode/Bond/Servo/CMaster.cpp
+++ b/SourceCode/Bond/Servo/CMaster.cpp
@@ -34,11 +34,6 @@
 
 	CMaster::~CMaster()
 	{
-		for (auto item : m_listEquipment) {
-			delete item;
-		}
-		m_listEquipment.clear();
-
 		if (m_hEventReadBitsThreadExit[0] != nullptr) {
 			::CloseHandle(m_hEventReadBitsThreadExit[0]);
 			m_hEventReadBitsThreadExit[0] = nullptr;
@@ -180,6 +175,11 @@
 		saveCache();
 
 
+		for (auto item : m_listEquipment) {
+			delete item;
+		}
+		m_listEquipment.clear();
+
 
 		return 0;
 	}
diff --git a/SourceCode/Bond/Servo/Context.cpp b/SourceCode/Bond/Servo/Context.cpp
index 96aa4b1..d71cf34 100644
--- a/SourceCode/Bond/Servo/Context.cpp
+++ b/SourceCode/Bond/Servo/Context.cpp
@@ -4,6 +4,7 @@
 
 CContext::CContext()
 {
+	m_onReleaseCallback = nullptr;
 	m_nRef = 0;
 	m_nRetCode = 0;
 	m_hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
@@ -20,6 +21,11 @@
 	DeleteCriticalSection(&m_criticalSection);
 }
 
+void CContext::setOnRelease(ONRELEASECALLBACK fOnRelease)
+{
+	m_onReleaseCallback = fOnRelease;
+}
+
 int CContext::addRef()
 {
 	Lock();
@@ -32,13 +38,20 @@
 int CContext::release()
 {
 	Lock();
-	m_nRef--;
-	BOOL bDelete = m_nRef == 0;
-	Unlock();
+	if (m_nRef > 0) {
+		m_nRef--;
+		BOOL bRefCount0 = m_nRef == 0;
+		Unlock();
 
-	int nRef = m_nRef;;
-	if (bDelete) delete this;
-	return nRef;
+		if (bRefCount0 && m_onReleaseCallback != nullptr) {
+			m_onReleaseCallback(this);
+		}
+	}
+	else {
+		Unlock();
+	}
+
+	return m_nRef;
 }
 
 void CContext::setRetCode(int code)
diff --git a/SourceCode/Bond/Servo/Context.h b/SourceCode/Bond/Servo/Context.h
index b4e38cc..9e556ac 100644
--- a/SourceCode/Bond/Servo/Context.h
+++ b/SourceCode/Bond/Servo/Context.h
@@ -1,6 +1,9 @@
 #pragma once
 #include "Common.h"
+#include <functional>
 
+
+typedef std::function<void(void*)> ONRELEASECALLBACK;
 class CContext : public IRxObject
 {
 public:
@@ -8,6 +11,7 @@
 	virtual ~CContext();
 
 public:
+	void setOnRelease(ONRELEASECALLBACK fOnRelease);
 	int addRef();
 	int release();
 
@@ -32,6 +36,7 @@
 	static BOOL IsLotId(CString& strId);
 
 private:
+	ONRELEASECALLBACK m_onReleaseCallback;
 	int m_nRef;
 	CRITICAL_SECTION m_criticalSection;
 
diff --git a/SourceCode/Bond/Servo/Model.cpp b/SourceCode/Bond/Servo/Model.cpp
index 95183ef..14e2af9 100644
--- a/SourceCode/Bond/Servo/Model.cpp
+++ b/SourceCode/Bond/Servo/Model.cpp
@@ -5,6 +5,7 @@
 #include "ToolUnits.h"
 #include "CEqAlarmStep.h"
 #include "AlarmManager.h"
+#include "CGlassPool.h"
 
 
 CModel::CModel()
@@ -45,6 +46,9 @@
 	CString strModeType = _T("Master");
 	CString strSoftRev = _T("1.0.2");
 
+
+	// CGlassPool
+	m_glassPool.initPool();
 
 
 	// Log
@@ -182,6 +186,7 @@
 	m_hsmsPassive.term();
 	CLog::GetLog()->SetOnLogCallback(nullptr);
 	m_master.term();
+	m_glassPool.term();
 
 	return 0;
 }
diff --git a/SourceCode/Bond/Servo/Model.h b/SourceCode/Bond/Servo/Model.h
index 6e2655c..a4985ed 100644
--- a/SourceCode/Bond/Servo/Model.h
+++ b/SourceCode/Bond/Servo/Model.h
@@ -2,6 +2,7 @@
 #include "Configuration.h"
 #include "HsmsPassive.h"
 #include "CMaster.h"
+#include "CGlassPool.h"
 
 class CModel
 {
@@ -32,6 +33,7 @@
 public:
 	CConfiguration m_configuration;
 	CHsmsPassive m_hsmsPassive;
+	SERVO::CGlassPool m_glassPool;
 	SERVO::CMaster m_master;
 
 private:
diff --git a/SourceCode/Bond/Servo/Servo.vcxproj b/SourceCode/Bond/Servo/Servo.vcxproj
index 79cdf9c..4e0f9d1 100644
--- a/SourceCode/Bond/Servo/Servo.vcxproj
+++ b/SourceCode/Bond/Servo/Servo.vcxproj
@@ -198,6 +198,7 @@
     <Text Include="ReadMe.txt" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="CGlassPool.h" />
     <ClInclude Include="PageRecipe.h" />
     <ClInclude Include="CDoubleGlass.h" />
     <ClInclude Include="CProcessData.h" />
@@ -295,6 +296,7 @@
     <ClInclude Include="VerticalLine.h" />
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="CGlassPool.cpp" />
     <ClCompile Include="PageRecipe.cpp" />
     <ClCompile Include="CDoubleGlass.cpp" />
     <ClCompile Include="CProcessData.cpp" />
diff --git a/SourceCode/Bond/Servo/Servo.vcxproj.filters b/SourceCode/Bond/Servo/Servo.vcxproj.filters
index 2730a47..9ade198 100644
--- a/SourceCode/Bond/Servo/Servo.vcxproj.filters
+++ b/SourceCode/Bond/Servo/Servo.vcxproj.filters
@@ -103,6 +103,7 @@
     <ClCompile Include="PageRecipe.cpp" />
     <ClCompile Include="CDoubleGlass.cpp" />
     <ClCompile Include="CProcessData.cpp" />
+    <ClCompile Include="CGlassPool.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="AlarmManager.h" />
@@ -204,6 +205,7 @@
     <ClInclude Include="PageRecipe.h" />
     <ClInclude Include="CDoubleGlass.h" />
     <ClInclude Include="CProcessData.h" />
+    <ClInclude Include="CGlassPool.h" />
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="Servo.rc" />

--
Gitblit v1.9.3