std::map<size,Employee*> itsEmployees;
//职员表
std::map<size,Product*> itsProducts;
//产品表
..
size addAccountMember(Account* e); //添加帐户
{
//获取下一个可用的ID号
size id=getNextAccountID();
itsAccounts[id]=e;
returnid;
}
size addEmployeeMember(Employee* e);//添加职员
{
//获取下一个可用的ID号
size id=getNextEmployeeID();
itsEmployees[id]=e;
return id;
}
..
void delAccount(size ID); //删除指定帐户
{
itsAccounts.erase(ID);
}
void delEmployee(size ID); //删除指定职员
{
itsEmployees.erase(ID);
}
..
Account* getAccountMember(size ID)
//获取指定帐户
{
return itsAccounts[ID];
}
Employee* getEmployeeMember(size ID) //获取指定职员
{
return itsEmployees[ID];
}
..
//获取下一个可用职员号
Size getNextEmployeeID()
{
if(itsEmployees.empty())
return 1;
std::map<size,Employee*>::iterator it=itsEmployees.end();
--it;
return it->first+1;
}
//获取下一个可用帐户号
Size getNextAccountID()
{
if(itsAccounts.empty())
return 1;
std::map<size,Account*>::iterator it=itsAccounts.end();
--it;
return it->first+1;
}
..
addAccountMember, addEmployeeMember,..
delAccount, delEmployee,..
getAccountMember, getEmployeeMember,..
getNextAccountID, getNextEmployeeID,..
addMember
delMember
getMember
getNextMemberID
size addMember(Account* e); //添加帐户
{
//获取下一个可用的ID号
size id=getNextAccountID();
itsAccounts[id]=e;
return id;
}
size addMember(Employee* e); //添加帐户
{
//获取下一个可用的ID号
size id=getNextEmployeeID();
itsEmployees[id]=e;
return id;
}
Template <typename T>
Struct Type2Type
{
typedef T OriginalType;
};
size getNextMemberID(Loki:: Type2Type<Employee>)
//对应职员操作
{
if (itsEmployees.empty())
return 1;
std::map<size,Employee*>::iterator it=itsEmployees.
end();
--it;
return it->first+1;
}
size getNextMemberID(Loki:: Type2Type<Account>)
//对应帐户操作
略..
void delMember(size ID, Loki:: Type2Type<Account>)
void delMember(size ID, Loki:: Type2Type<Employee>)
Account* getMember(size ID, Loki:: Type2Type<Account>)
Employee* getMember(size ID, Loki:: Type2Type<Employee>)
size addMember(Account* e); //添加帐户
{
//获取下一个可用的ID号
size id= getNextMemberID(Loki::Type2Type<Account>());
itsAccounts[id]=e;
return id;
}
size addMember(Employee* e); //添加帐户
{
//获取下一个可用的ID号
size id=getNextMemberID(Loki::Type2Type<Employee>());
itsEmployees[id]=e;
return id;
}
template<typename T>
size addMember(T* e)
{
size empid=getNextMemberID(Loki::Type2Type<T>());
//要害在于以下函数
std::map<size,T*>& its=getMap(Loki::Type2Type<T>());
its[empid]=e;
return empid;
}
std::map<size,Account*>& getMap(Loki::Type2Type<Account>)
{
return itsAccounts;
}
std::map<size,Employee*>& getMap(Loki::Type2Type
<Employee>);
{
return itsEmployees;
}
template <typename T>
size getNextMemberID(Loki::Type2Type<T>)
{
std::map<size,T*>& its=getMap(Loki::Type2Type<T>());
if (its.empty())
return 1;
std::map<size,T*>::iterator it=its.end();
--it;
return it->first+1;
}
template <typename T>
size addMember(T* e)
{
size empid=getNextMemberID(Loki::Type2Type<T>());
std::map<size,T*>& its=getMap(Loki::Type2Type<T>());
its[empid]=e;
return empid;
}
template <typename T>
T* getMember(size memberID,Loki::Type2Type<T>)
{
std::map<size,T*>& its=getMap(Loki::Type2Type<T>());
return its[memberID];
}
template <typename T>
void delMember(size memberID,Loki::Type2Type<T>)
{
std::map<size,T*>& its=getMap(Loki::Type2Type<T>());
its.erase(memberID);
}
void SerializeMap(CArchive& ar,std::map<size,Account*>&map)
{
typedef std::map< size,Account*>::value_type
value_type;
typedef std::map< size,Account*>::iterator iterator;
if (ar.IsStoring())
{
DWord n=map.size();
ar.WriteCount(n);
for(iterator it=map.begin();it!=map.end();++it)
{
ar<<it->first<<it->second;
}
}
else
{
size first;
Account* second;
DWORD nNewCount=ar.ReadCount();
while (nNewCount--)
{
ar>>first>>second;
value_type value(first,second);
map.insert(value);
}
}
}
template <typename Key,typename T>
void SerializeMap(CArchive& ar,std::map<Key,T>& map)
{
typedef std::map<Key,T>::value_type value_type;
typedef std::map<Key,T>::iterator iterator;
if (ar.IsStoring())
{
DWORD n=map.size();
ar.WriteCount(n);
for(iterator it=map.begin();it!=map.end();++it)
{
ar<<it->first<<it->second;
}
}
else
{
Key first;
T second;
DWORD nNewCount=ar.ReadCount();
while (nNewCount--)
{
ar>>first>>second;
value_type value(first,second);
map.insert(value);
}
}
}
SerializeMap(ar,itsEmployees);
SerializeMap(ar,itsAccounts);
........
新闻热点
疑难解答