I am working on a data parser for a project. I have to parse a file that may contain two different types of objects:
Type-1: sb0 hardrectilinear 4 (0, 0) (0, 82) (199, 82) (199, 0)
Type-1 has to be stored as a class block, with the following attributes: BlockID, BlockType, number_of_edges, lowerleft, lowerright, upperleft, upperright.
Type-2: sb1 softrectangular 24045 0.300 3.000
Type-2 also has to be stored as a class block, with the following attributes: BlockID, BlockType, area, min_aspectRatio, max_aspectRatio.
Is it possible to build a single class called "block", with a different set of arguments depending on the attribute "BlockType"? I have built a parser but I used two different classes for each BlockType using sstream.
I have shown my implementation of the parser when the text file to be parsed contains only type-2. Any ideas on how I can do this using a single class?
SoftBlock.h:
#ifndef SOFTBLOCKLIST_H_
#define SOFTBLOCKLIST_H_
#include <string>
#include <vector>
#include "SoftBlock.h"
#include <fstream>
class SoftBlockList {
public:
SoftBlockList(std::string input_file);
std::vector<SoftBlock> get_softblocklist();
private:
std::vector<SoftBlock> softblocklist;
};
#endif /* SOFTBLOCKLIST_H_ */
SoftBlock.cpp:
#include "SoftBlockList.h"
using namespace std;
SoftBlockList::SoftBlockList(string input_file) {
ifstream filehandle;
filehandle.open(input_file.c_str());
string temp;
while(filehandle.good()){
getline(filehandle, temp);
SoftBlock block(temp);
softblocklist.push_back(block);
}
filehandle.close();
}
vector<SoftBlock> SoftBlockList::get_softblocklist(){return
softblocklist;}