Suggestions for code for records of varying formats
I have a file with a variety of record formats. The code shown below is a sample of how I process the records. The code works; I will use it unless someone has improvements I can use. So I am only asking for comments and suggestions for the code.
In this sample code, I have only four record types but in my actual code I have many more formats. Also, in the sample code, the data is supplied in the sample but of course for the actual code the data is read from a file.
The code is creating a vector (typeSampleDataVector) of the records, then processes the vector. The records will only be shown, I don't need to update them. I am actually creating the vector for use in a MFC document and the data will then be shown in custom windows from a view.
Instead of using a switch, I am using a map of functions (typeShowRecordMap) in which the record type is the map key.
I am sure I can improve the code but I am not aware of any major improvements. I can use const more than I have.
If there is better way to include code here in the forums, then I will be happy to use it.
#include
"stdafx.h"// - - - - - - - - -
enum
enumRecordType {RecordType_Unknown,
RecordType_Begin,
RecordType_Insert,
RecordType_Delete,
RecordType_End
};
struct
structSampleRecord {enumRecordType RecordType;
};
struct
structSampleRecord_Begin :public structSampleRecord {int Time;};
struct
structSampleRecord_Insert :public structSampleRecord {std::string Item;
int Id;};
struct
structSampleRecord_Delete :public structSampleRecord {int Id;};
struct
structSampleRecord_End :public structSampleRecord {int Time;};
typedef
std::vector<structSampleRecord *> typeSampleDataVector;// - - - - - - - - -
// - - - - - - - - -
class
classRecords {public
:typedefvoid (classRecords::*typeShowRecord)(structSampleRecord *);
typedef std::map<int, typeShowRecord> typeShowRecordMap;typeShowRecordMap ShowRecordMap;
bool ProcessRecord(structSampleRecord *);void CreateRecordProcessorMap(void);typeSampleDataVector SampleDataVector;
void Show_Begin(structSampleRecord *);void Show_Insert(structSampleRecord *);void Show_Delete(structSampleRecord *);void Show_End(structSampleRecord *);};
// - - - - - - - - -
// - - - - - - - - -
void
classRecords::Show_Begin(structSampleRecord *pSampleRecord) {structSampleRecord_Begin *psrb;
psrb =
static_cast<structSampleRecord_Begin *> (pSampleRecord);std::cout <<
"Begin: " << psrb->Time <<'\n';}
// - - - - - - - - -
// - - - - - - - - -
void
classRecords::Show_Insert(structSampleRecord *pSampleRecord) {structSampleRecord_Insert *psri;
psri =
static_cast<structSampleRecord_Insert *> (pSampleRecord);std::cout <<
"Insert: " << psri->Item <<' ' << psri->Id <<'\n';}
// - - - - - - - - -
// - - - - - - - - -
void
classRecords::Show_Delete(structSampleRecord *pSampleRecord) {structSampleRecord_Delete *psrd;
psrd =
static_cast<structSampleRecord_Delete *> (pSampleRecord);std::cout <<
"Delete: " << psrd->Id <<'\n';}
// - - - - - - - - -
// - - - - - - - - -
void
classRecords::Show_End(structSampleRecord *pSampleRecord) {structSampleRecord_End *psre;
psre =
static_cast<structSampleRecord_End *> (pSampleRecord);std::cout <<
"End: " << psre->Time <<'\n';}
// - - - - - - - - -
// - - - - - - - - -
void
classRecords::CreateRecordProcessorMap(void) {ShowRecordMap[RecordType_Begin] = &classRecords::Show_Begin;
ShowRecordMap[RecordType_Insert] = &classRecords::Show_Insert;
ShowRecordMap[RecordType_Delete] = &classRecords::Show_Delete;
ShowRecordMap[RecordType_End] = &classRecords::Show_End;
}
// - - - - - - - - -
// - - - - - - - - -
bool
classRecords::ProcessRecord(structSampleRecord *pSampleDataData) {typeShowRecordMap::const_iterator i;
i = ShowRecordMap.find(pSampleDataData->RecordType);
if
(i == ShowRecordMap.end()) {std::cerr <<
"Record type " << pSampleDataData->RecordType<<
" is not found\n";returnfalse;}
(
this->*i->second)(pSampleDataData);return
true;}
// - - - - - - - - -
// - - - - - - - - -
int
main(int argc,char* argv[]) {(
void)argc, argv;classRecords Records;
typeSampleDataVector::const_iterator i;
structSampleRecord_Begin *psrb(
new structSampleRecord_Begin);structSampleRecord_Insert *psri(
new structSampleRecord_Insert);structSampleRecord_Delete *psrd(
new structSampleRecord_Delete);structSampleRecord_End *psre(
new structSampleRecord_End);psrb->RecordType = RecordType_Begin;
psrb->Time = 1200;
Records.SampleDataVector.push_back(psrb);
psri->RecordType = RecordType_Insert;
psri->Item =
"Widget";psri->Id = 96;
Records.SampleDataVector.push_back(psri);
psrd->RecordType = RecordType_Delete;
psrd->Id = 96;
Records.SampleDataVector.push_back(psrd);
psre->RecordType = RecordType_End;
psre->Time = 1230;
Records.SampleDataVector.push_back(psre);
//
Records.CreateRecordProcessorMap();
for
(i=Records.SampleDataVector.begin();i!=Records.SampleDataVector.end(); ++i)
Records.ProcessRecord(*i);
//
delete
psrb;delete
psri;delete
psrd;delete
psre;return
0;}
// - - - - - - - - -

