MFC data structures
-
Hi all . Please tell me if I can integrate more object data types into a single MFC data structures ( Array , List or Map ). If I have an CStringArray object , let's say m_saType , ( CStringArray type ) , and m_saItem ( CStringArray type too ) , and m_saPerson ( CStringArray type too ) ... how can I integrate all togheter into CObList MFC object , because I want to serialize all m_saType, m_saItem, m_saPerson into a single file on disk. Thank you . I accept any ideea .
-
Hi all . Please tell me if I can integrate more object data types into a single MFC data structures ( Array , List or Map ). If I have an CStringArray object , let's say m_saType , ( CStringArray type ) , and m_saItem ( CStringArray type too ) , and m_saPerson ( CStringArray type too ) ... how can I integrate all togheter into CObList MFC object , because I want to serialize all m_saType, m_saItem, m_saPerson into a single file on disk. Thank you . I accept any ideea .
m_saType.Serialize(ar); m_saItem.Serialize(ar); m_saPerson.Serialize(ar); Of course a better way is to declare a CObject derived class CPerson with CString members m_sType, m_sItem, m_sPerson Implementing Serialize in this class the using a CObList and calling its Serialize...
-
Hi all . Please tell me if I can integrate more object data types into a single MFC data structures ( Array , List or Map ). If I have an CStringArray object , let's say m_saType , ( CStringArray type ) , and m_saItem ( CStringArray type too ) , and m_saPerson ( CStringArray type too ) ... how can I integrate all togheter into CObList MFC object , because I want to serialize all m_saType, m_saItem, m_saPerson into a single file on disk. Thank you . I accept any ideea .
#include class CDataPack : public CObject
{
public: // you can determine this access scope
CStringArray m_saType;
CStringArray m_saItem;
CStringArray m_saPerson;const CDataPack& operator=(const CDataPack& rdp) { m\_saType.Copy(rdp.m\_saType); m\_saItem.Copy(rdp.m\_saItem); m\_saPerson.Copy(rdp.m\_saPerson); return \*this; } BOOL IsSerializable() const { return TRUE; } virtual void Serialize(CArchive& ar) { m\_saType.Serialize(ar); m\_saItem.Serialize(ar); m\_saPerson.Serialize(ar); }
};
typedef CArray CDataPackType;
void AnyFunc()
{
CString strElement(_T("An Element"));
CDataPack pkt;pkt.m\_saType.Add(strElement); // using public access pkt.m\_saItem.Add(strElement); pkt.m\_saPerson.Add(strElement); CDataPackType arrData; arrData.Add(pkt); CFile file; // TODO: you need to open the file CArchive ar(&file, CArchive::store); arrData.Serialize(ar);
}
If you don't want to use template counterpart, you can also use CObArray or even CPtrArray (or List ones such as CObList, CPtrList, see afxcoll.h). Just beware cleanup of objects.
-
#include class CDataPack : public CObject
{
public: // you can determine this access scope
CStringArray m_saType;
CStringArray m_saItem;
CStringArray m_saPerson;const CDataPack& operator=(const CDataPack& rdp) { m\_saType.Copy(rdp.m\_saType); m\_saItem.Copy(rdp.m\_saItem); m\_saPerson.Copy(rdp.m\_saPerson); return \*this; } BOOL IsSerializable() const { return TRUE; } virtual void Serialize(CArchive& ar) { m\_saType.Serialize(ar); m\_saItem.Serialize(ar); m\_saPerson.Serialize(ar); }
};
typedef CArray CDataPackType;
void AnyFunc()
{
CString strElement(_T("An Element"));
CDataPack pkt;pkt.m\_saType.Add(strElement); // using public access pkt.m\_saItem.Add(strElement); pkt.m\_saPerson.Add(strElement); CDataPackType arrData; arrData.Add(pkt); CFile file; // TODO: you need to open the file CArchive ar(&file, CArchive::store); arrData.Serialize(ar);
}
If you don't want to use template counterpart, you can also use CObArray or even CPtrArray (or List ones such as CObList, CPtrList, see afxcoll.h). Just beware cleanup of objects.
Thank you very much ! You are very kind ! And if gave me good ideas , I push my luck and ask more : which structure of collection data you choose if you want to clone 3 SQL tables , every one with one unique key integer type and other fiels is strings , but keep link between tables through those unique key integer ?
-
Thank you very much ! You are very kind ! And if gave me good ideas , I push my luck and ask more : which structure of collection data you choose if you want to clone 3 SQL tables , every one with one unique key integer type and other fiels is strings , but keep link between tables through those unique key integer ?
I think, you mean which collection class? If so, Array is more efficient on random access. I also think, you need to access the elements with Primary Keys/Forein Keys in master-detail relationships. If you need rows in sorted order, you can fill this array sorted. You can take advantage of SQL's SELECT with ORDER BY clause while filling. If the array is sorted, you can access its items by using binary search to speed up searches. If you just need to access items by the key, you can simply employ CMap to access its elements with key-value pair.
typedef CMap<int, int, CDataPack, CDataPack&> CDataPackMap;
You need to keep relationships yourself, or maybe, you may create more sophisticated structures. [edit]BTW, Not at all. :) [/edit] -
I think, you mean which collection class? If so, Array is more efficient on random access. I also think, you need to access the elements with Primary Keys/Forein Keys in master-detail relationships. If you need rows in sorted order, you can fill this array sorted. You can take advantage of SQL's SELECT with ORDER BY clause while filling. If the array is sorted, you can access its items by using binary search to speed up searches. If you just need to access items by the key, you can simply employ CMap to access its elements with key-value pair.
typedef CMap<int, int, CDataPack, CDataPack&> CDataPackMap;
You need to keep relationships yourself, or maybe, you may create more sophisticated structures. [edit]BTW, Not at all. :) [/edit]