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
typedef BLUETOOTHDEVICEMAP::iterator BLUETOOTHDEVICEITERATOR;
typedef pair
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(
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(
void RemoveBluetoothDevice(LPTSTR bluetoothAddress);
void flush();
void Flush();
BluetoothDevice *findBluetoothDevice(LPTSTR bluetoothAddress,int &iIndex);
BluetoothDevice *AddBluetoothDevice(
BluetoothDevice *GetBluetoothDevice(int iIndex);
BluetoothDevice *GetBluetoothDevice(LPTSTR bluetoothAddress);
//CSDPRecord *GetSDPRecord(BT_ADDR bluetoothAddress,SERVICE_
-------------------BTMap.cpp--
#include "stdafx.h"
#include "BluetoothDeviceMap.h"
BluetoothDeviceMap::
{
InitializeCriticalSection(&m_
}
BluetoothDeviceMap::~
{
DeleteCriticalSection(&m_
}
void BluetoothDeviceMap::
{
if((iIndex >=0)&&(iIndex < (int)size()))
{
BLUETOOTHDEVICEITERATOR iter = find(iIndex);
delete iter->second;
erase(iter);
}
}
void BluetoothDeviceMap::
{
EnterCriticalSection(&m_
removeBluetoothDevice(iIndex);
LeaveCriticalSection(&m_
}
void BluetoothDeviceMap::
{
EnterCriticalSection(&m_
for(BLUETOOTHDEVICEITERATOR iter = begin(); iter != end();iter++)
{
if(iter->second == pBluetoothDevice)
{
removeBluetoothDevice(iter->
break;
}
}
LeaveCriticalSection(&m_
}
void BluetoothDeviceMap::
{
EnterCriticalSection(&m_
for(BLUETOOTHDEVICEITERATOR iter = begin(); iter != end();iter++)
{
if(iter->second->GetBTAddress(
{
removeBluetoothDevice(iter->
break;
}
}
LeaveCriticalSection(&m_
}
void BluetoothDeviceMap::flush()
{
for (BLUETOOTHDEVICEITERATOR itr = begin();itr != end();itr++)
{
delete itr->second;
}
clear();
m_bIsDirty = false;
}
void BluetoothDeviceMap::Flush()
{
EnterCriticalSection(&m_
//flushToRegistry();
flush();
LeaveCriticalSection(&m_
}
int BluetoothDeviceMap::
{
int tmp = -1;
EnterCriticalSection(&m_
for(BLUETOOTHDEVICEITERATOR iter = begin(); iter!=end();iter++)
{
if(iter->second = pBluetoothDevice)
{
tmp = iter->first;
break;
}
}
LeaveCriticalSection(&m_
return tmp;
}
BluetoothDevice *BluetoothDeviceMap::
{
BluetoothDevice *tmpBTDevice = NULL;
EnterCriticalSection(&m_
int iIndex = -1;
CString aa;
//aa.Format(L"%s", pQueryResult->tcAddress);
if(findBluetoothDevice(
{
tmpBTDevice = new BluetoothDevice(pQueryResult->
insert(BLUETOOTHDEVICEMAPPAIR(
iIndex = size()-1;
}else
iIndex = -1;
return tmpBTDevice;
}
BluetoothDevice * BluetoothDeviceMap::
{
BLUETOOTHDEVICEITERATOR iter;
bool bFound = false;
for(iter=begin(); iter!=end();iter++)
{
if(iter->second->GetBTAddress(
{
iIndex = iter->first;
bFound = true;
break;
}
}
return bFound == true ? iter->second : NULL;
}
BluetoothDevice * BluetoothDeviceMap::
{
BluetoothDevice *p;
BLUETOOTHDEVICEITERATOR itr;
EnterCriticalSection(&m_
if (0 <= iIndex && iIndex < (int)size())
{
itr = find(iIndex);
p = itr->second;
}
LeaveCriticalSection(&m_
return itr->second;
}
BluetoothDevice * BluetoothDeviceMap::
{
EnterCriticalSection(&m_
int iIndex = -1;
BluetoothDevice *p = findBluetoothDevice(
LeaveCriticalSection(&m_
return p;
}
bool BluetoothDeviceMap::
{
bool bReturn = false;
if(dwIndex >= 0 && dwIndex < (DWORD)size() )
{
BLUETOOTHDEVICEITERATOR iter = find(dwIndex);
if(iter->second != NULL)
{
bReturn = iter->second->IsDevicePaired()
}
}
return bReturn;
}
------------------------------
#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;};
};
----------------------
#include "stdafx.h"
#include "BluetoothDevice.h"
BluetoothDevice::
{
}
BluetoothDevice::
{
_tcscpy(m_DeviceName,
_tcscpy(m_tcAddress,tcAddress)
}
BluetoothDevice::~
{
}
------------------------To Use---------------------------
Add:
BLUETOOTHDEVICE *tmpBTDevice = new BLUETOOTHDEVICE();
CString sssAddress = "myName";
_tcsncpy(tmpBTDevice->
CString sssAddress = "myAddress";
_tcsncpy(tmpBTDevice->
m_pCMSBluetooth->
_tcscpy(tmpBTDevice->
_tcscpy(tmpBTDevice->
Retrieve:
int CxxxBluetooth::
{
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->
iError = *bsAddress == 0? -1:0;
}
return iError;
}
int CxxxBluetooth::
{
int iError = -100;
if(iIndex < GetDeviceCount())
{
BluetoothDevice *p = GetBluetoothDevice(iIndex);
//bsName = p->GetDeviceName();
_tcscpy(bsName,p->
iError = *bsName == 0? -1:0;
}
return iError;
}
1 comment:
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
Post a Comment