Re: runtime type issues (or, reading complicated logfiles)
Benjamin Collins <aggieben@gmail.com> wrote:
f()
{
/* ... */
std::ifstream ifs("filename");
common_header info;
while(ifs)
{
ifs >> info;
void *header = read_header(ifs, info); /* allocate header
here, and read log using ifs */
/* do stuff with 'info' and 'header' */
}
}
What has me thinking is how to handle allocation of the format
header. Right now, I'm contemplating having read_header allocate the
header and return it as a void*, but I'd rather be able to use
std::auto_ptr or
the common heaser determins what to read and which function to use, so
a table of boost or tr1 function allows a table of functors of the
same signature to be stored in an array or map , or tr1::usnsorted_map
to be used for lookup
read the common header
lookup proper function and call it
class log_reader
{
typdef std::tr1::function<void(std::istream &,const common_header
&)> function_type;
typedef std::vector<function_type> table_type;
table_type perform;
public:
log_reader() {/* build table */}
void operator () (std::istream &in)
{
common_header info;
while(in >> info)
{
perform[info.format_type](is,info);
}
}
};
// ...
log_reader reader;
reader(is);
should work with table_type
std::vector<function_type>,std::map<int,function_type>, or
std::tr1;:unsorted_map<int,function_type>.
the functions read the sub header and perform according to subsection's
data.
probably easier to maintain than a cascade of if's.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]