Tuesday, January 27, 2009

Storage: MAP in C++ and List in C# (2)

MAP in C++ and List in C#
=====================

(2) MAP in C++

In C++, we can use MAP to do similar thing as List in C#.
-------------------BTMap.h----
----------------------
#include "BluetoothDevice.h"
#include map
using namespace std;

typedef struct
{
TCHAR tcDeviceName[100];
TCHAR tcAddress[100];
}BLUETOOTHDEVICE;

typedef map BLUETOOTHDEVICEMAP;
typedef BLUETOOTHDEVICEMAP::iterator BLUETOOTHDEVICEITERATOR;
typedef pair BLUETOOTHDEVICEMAPPAIR;

class BluetoothDeviceMap : public BLUETOOTHDEVICEMAP
{
private:
CRITICAL_SECTION m_CriticalSection;;
bool m_bIsDirty;

private:

BluetoothDevice *findBluetoothDevice(CString bluetoothAddress,int &iIndex);
void removeBluetoothDevice(int iIndex);
void removeBluetoothDevice(BluetoothDevice *pBluetoothDevice,int &iIndex);

public:
BluetoothDeviceMap(void);
virtual ~BluetoothDeviceMap(void);
int GetDeviceIndex(BluetoothDevice *pBluetoothDevice);
bool IsDevicePaired(DWORD dwIndex);
void SetDirtyBit() {m_bIsDirty = true;};
void RemoveBluetoothDevice(int iIndex);
void RemoveBluetoothDevice(BluetoothDevice *pBluetoothDevice);
void RemoveBluetoothDevice(LPTSTR bluetoothAddress);
void flush();
void Flush();

BluetoothDevice *findBluetoothDevice(LPTSTR bluetoothAddress,int &iIndex);
BluetoothDevice *AddBluetoothDevice(BLUETOOTHDEVICE *pQueryResult);
BluetoothDevice *GetBluetoothDevice(int iIndex);
BluetoothDevice *GetBluetoothDevice(LPTSTR bluetoothAddress);
//CSDPRecord *GetSDPRecord(BT_ADDR bluetoothAddress,SERVICE_CLASSES_UUID16 serviceClassID);

-------------------BTMap.cpp-----------------------------
#include "stdafx.h"
#include "BluetoothDeviceMap.h"

BluetoothDeviceMap::BluetoothDeviceMap(void)
{
InitializeCriticalSection(&m_CriticalSection);
}

BluetoothDeviceMap::~BluetoothDeviceMap(void)
{
DeleteCriticalSection(&m_CriticalSection);
}
void BluetoothDeviceMap::removeBluetoothDevice(int iIndex)
{
if((iIndex >=0)&&(iIndex < (int)size()))
{
BLUETOOTHDEVICEITERATOR iter = find(iIndex);
delete iter->second;
erase(iter);
}
}

void BluetoothDeviceMap::RemoveBluetoothDevice(int iIndex)
{
EnterCriticalSection(&m_CriticalSection);
removeBluetoothDevice(iIndex);
LeaveCriticalSection(&m_CriticalSection);
}

void BluetoothDeviceMap::RemoveBluetoothDevice(BluetoothDevice *pBluetoothDevice)
{
EnterCriticalSection(&m_CriticalSection);
for(BLUETOOTHDEVICEITERATOR iter = begin(); iter != end();iter++)
{
if(iter->second == pBluetoothDevice)
{
removeBluetoothDevice(iter->first);
break;
}
}
LeaveCriticalSection(&m_CriticalSection);

}
void BluetoothDeviceMap::RemoveBluetoothDevice(LPTSTR bluetoothAddress)
{
EnterCriticalSection(&m_CriticalSection);
for(BLUETOOTHDEVICEITERATOR iter = begin(); iter != end();iter++)
{
if(iter->second->GetBTAddress() == bluetoothAddress)
{
removeBluetoothDevice(iter->first);
break;
}
}
LeaveCriticalSection(&m_CriticalSection);
}

void BluetoothDeviceMap::flush()
{
for (BLUETOOTHDEVICEITERATOR itr = begin();itr != end();itr++)
{
delete itr->second;
}
clear();
m_bIsDirty = false;
}
void BluetoothDeviceMap::Flush()
{
EnterCriticalSection(&m_CriticalSection);
//flushToRegistry();
flush();
LeaveCriticalSection(&m_CriticalSection);
}

int BluetoothDeviceMap::GetDeviceIndex(BluetoothDevice *pBluetoothDevice)
{
int tmp = -1;
EnterCriticalSection(&m_CriticalSection);
for(BLUETOOTHDEVICEITERATOR iter = begin(); iter!=end();iter++)
{
if(iter->second = pBluetoothDevice)
{
tmp = iter->first;
break;
}
}
LeaveCriticalSection(&m_CriticalSection);
return tmp;
}

BluetoothDevice *BluetoothDeviceMap::AddBluetoothDevice(BLUETOOTHDEVICE *pQueryResult)
{
BluetoothDevice *tmpBTDevice = NULL;
EnterCriticalSection(&m_CriticalSection);
int iIndex = -1;
CString aa;
//aa.Format(L"%s", pQueryResult->tcAddress);
if(findBluetoothDevice(pQueryResult->tcAddress, iIndex)==NULL)
{
tmpBTDevice = new BluetoothDevice(pQueryResult->tcDeviceName, pQueryResult->tcAddress);
insert(BLUETOOTHDEVICEMAPPAIR(size(), tmpBTDevice));
iIndex = size()-1;
}else
iIndex = -1;

return tmpBTDevice;
}
BluetoothDevice * BluetoothDeviceMap::findBluetoothDevice(LPTSTR bluetoothAddress,int &iIndex)
{
BLUETOOTHDEVICEITERATOR iter;
bool bFound = false;
for(iter=begin(); iter!=end();iter++)
{
if(iter->second->GetBTAddress() == bluetoothAddress)
{
iIndex = iter->first;
bFound = true;
break;
}
}
return bFound == true ? iter->second : NULL;

}

BluetoothDevice * BluetoothDeviceMap::GetBluetoothDevice(int iIndex)
{
BluetoothDevice *p;
BLUETOOTHDEVICEITERATOR itr;
EnterCriticalSection(&m_CriticalSection);
if (0 <= iIndex && iIndex < (int)size())
{
itr = find(iIndex);
p = itr->second;
}
LeaveCriticalSection(&m_CriticalSection);
return itr->second;
}
BluetoothDevice * BluetoothDeviceMap::GetBluetoothDevice(LPTSTR bluetoothAddress)
{
EnterCriticalSection(&m_CriticalSection);
int iIndex = -1;
BluetoothDevice *p = findBluetoothDevice(bluetoothAddress,iIndex);
LeaveCriticalSection(&m_CriticalSection);
return p;
}
bool BluetoothDeviceMap::IsDevicePaired(DWORD dwIndex)
{
bool bReturn = false;
if(dwIndex >= 0 && dwIndex < (DWORD)size() )
{
BLUETOOTHDEVICEITERATOR iter = find(dwIndex);
if(iter->second != NULL)
{
bReturn = iter->second->IsDevicePaired();
}
}
return bReturn;
}
------------------------------BluetoothDevice.h-------------------
#pragma once

class BluetoothDevice
{
private:
TCHAR m_DeviceName[100];
TCHAR m_tcAddress[100];
bool m_Paired;

public:
BluetoothDevice(void);
BluetoothDevice(TCHAR DeviceName[100], TCHAR tcAddress[100]);

virtual ~BluetoothDevice(void);
LPTSTR GetBTAddress(){return m_tcAddress;};
LPTSTR GetDeviceName(){return m_DeviceName;};
bool IsDevicePaired(){ return m_Paired;};
};
----------------------BluetoothDevice.cpp------------------------
#include "stdafx.h"
#include "BluetoothDevice.h"

BluetoothDevice::BluetoothDevice(void)
{
}
BluetoothDevice::BluetoothDevice(LPTSTR DeviceName, LPTSTR tcAddress)
{
_tcscpy(m_DeviceName,DeviceName);
_tcscpy(m_tcAddress,tcAddress);
}

BluetoothDevice::~BluetoothDevice(void)
{
}
------------------------To Use--------------------------------
Add:

BLUETOOTHDEVICE *tmpBTDevice = new BLUETOOTHDEVICE();
CString sssAddress = "myName";
_tcsncpy(tmpBTDevice->tcDeviceName,sssAddress,sssAddress.GetLength());
CString sssAddress = "myAddress";
_tcsncpy(tmpBTDevice->tcAddress ,sssAddress,sssAddress.GetLength());

m_pCMSBluetooth->AddBluetoothDevice(tmpBTDevice);
_tcscpy(tmpBTDevice->tcAddress,L"");
_tcscpy(tmpBTDevice->tcDeviceName,L"");


Retrieve:

int CxxxBluetooth::GetBluetoothAddress(int iIndex, TCHAR* bsAddress)
{
int iError = -100;
if(iIndex < GetDeviceCount())
{
BluetoothDevice *p = GetBluetoothDevice(iIndex); //GetBluetoothDevice: please see above function defination
//bsAddress = p->GetBTAddress(); //cannot do this, value is not copied!
_tcscpy(bsAddress,p->GetBTAddress());
iError = *bsAddress == 0? -1:0;
}
return iError;
}
int CxxxBluetooth::GetBluetoothDeviceName(int iIndex, TCHAR* bsName)
{
int iError = -100;
if(iIndex < GetDeviceCount())
{
BluetoothDevice *p = GetBluetoothDevice(iIndex);
//bsName = p->GetDeviceName();
_tcscpy(bsName,p->GetDeviceName());
iError = *bsName == 0? -1:0;
}
return iError;
}

1 comment:

Dennis said...

typedef map<int, BluetoothDevice *>BLUETOOTHDEVICEMAP;
typedef BLUETOOTHDEVICEMAP::iterator BLUETOOTHDEVICEITERATOR;
typedef pair<int,BluetoothDevice *> BLUETOOTHDEVICEMAPPAIR;

class BluetoothDeviceMap : public BLUETOOTHDEVICEMAP


//must inheritated from SDPRECORDMAP, otherwise all begin(), end() won't work