These snippets provide several miscellaneous tools that ease the developper's life.
Using the Tool class for quick tool development
This snippet shows how to quickly create a new tool by subclassing the Tool class.
You can try it online here.
Using the Tool class will give to use some useful features for developing our toy tool.
Code is from example ToyTool.cpp:
static const char* STR_RANGE_MIN = "-min";
static const char* STR_RANGE_MAX = "-max";
class ToyTool : public Tool
{
public:
ToyTool () : Tool ("ToyTool")
{
getParser()->push_front (new OptionOneParam (STR_RANGE_MIN, "lower range bound", false, "1"));
getParser()->push_front (new OptionOneParam (STR_RANGE_MAX, "upper range bound", true));
}
void execute ()
{
Range<u_int64_t> range (
getInput()->getInt(STR_RANGE_MIN),
getInput()->getInt(STR_RANGE_MAX)
);
Iterator<u_int64_t>* iter = createIterator<u_int64_t> (range, "iterate range");
u_int64_t totalSum = 0;
getTimeInfo().start ("computation");
IDispatcher::Status status = getDispatcher()->iterate (iter, [&] (const u_int64_t& i)
{
u_int64_t sum = 0;
for (u_int64_t j=0; j<i; j++) { sum += j; }
__sync_fetch_and_add (&totalSum, sum);
});
getTimeInfo().stop ("computation");
getInfo()->add (1, "output");
getInfo()->add (2, "sum", "%ld", totalSum);
getInfo()->add (1, getTimeInfo().getProperties("time"));
}
};
int main (int argc, char* argv[])
{
try
{
ToyTool().run (argc, argv);
}
catch (Exception& e)
{
std::cout << "EXCEPTION: " << e.getMessage() << std::endl;
return EXIT_FAILURE;
}
}