I am looking for a good implementation for a dry-run runtime option, so that the results from a computation are not written to disc, but discarded without interfering with the rest of the code. Currently I have this:
void write(queue_type &queue, bool dry_run, const struct options &opts){
auto dataset = opts.dataset;
buffer_type buf;
auto dwrite = dry_run
?[](Dataset &, const buffer_type &){}
:[](Dataset &dset, const buffer_type &buf){dset.write(buf);}
while(queue.pop(buf)){
dwrite(dataset, buf);
}
}
int main() {
/* ... */;
bool dry_run = /*...*/;
queue_type queue{};
struct options opts{...};
std:thread(write, std::ref(queue), dry_run, std::cref(opts));
for(...){
/* fill queue */
queue.append(...);
}
}
dataset is a handler to an open file. queue is feeding the write(bool dry_run,...) thread with data. So in essence, this function should only pop some data from queue and (dry) write it.
Is there another way to implement this, I got the feeling this is not very elegant. I hope this suffices as an example.
Is using std::function or a function pointer here better?
Aucun commentaire:
Enregistrer un commentaire