This example demonstrates how to handle dependency labels. It produces a summary of distfiles for all installed packages, together with a notice of whether that distfile is fetch-restricted.
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <map>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::setw;
using std::left;
typedef std::map<std::string, bool> ResultsMap;
namespace
{
class IsLabelRestrictedVisitor
{
public:
bool result;
IsLabelRestrictedVisitor(const bool initial) :
result(initial)
{
}
{
result = false;
}
{
result = false;
}
{
result = false;
}
{
result = false;
}
{
result = true;
}
{
result = true;
}
};
class DistfilesCollector
{
private:
ResultsMap & _results;
std::list<bool> _restricted;
public:
DistfilesCollector(ResultsMap & r, const bool initial) :
_results(r)
{
_restricted.push_back(initial);
}
{
_restricted.push_back(_restricted.back());
_restricted.pop_back();
}
{
_restricted.push_back(_restricted.back());
_restricted.pop_back();
}
{
_results.insert(std::make_pair(node.spec()->filename(), _restricted.back()));
}
{
IsLabelRestrictedVisitor v(_restricted.back());
_restricted.back() = v.result;
}
};
}
int main(int argc, char * argv[])
{
try
{
CommandLine::get_instance()->run(argc, argv,
"example_dep_label", "EXAMPLE_DEP_LABEL_OPTIONS", "EXAMPLE_DEP_LABEL_CMDLINE");
CommandLine::get_instance()->a_environment.argument()));
ResultsMap results;
i != i_end ; ++i)
{
if (! (*i)->fetches_key())
continue;
IsLabelRestrictedVisitor is_initial_label_restricted(false);
(*i)->fetches_key()->initial_label()->accept(is_initial_label_restricted);
DistfilesCollector collector(results, is_initial_label_restricted.result);
(*i)->fetches_key()->parse_value()->top()->accept(collector);
}
cout << left << setw(59) << "Distfile Name" << "| " << "Fetch Restricted?" << endl;
cout << std::string(59, '-') << "+" << std::string(18, '-') << endl;
for (ResultsMap::const_iterator r(results.begin()), r_end(results.end()) ;
r != r_end ; ++r)
cout << left << setw(59) << r->first << "| " << (r->second ? "yes" : "no") << endl;
}
{
cout << endl;
cout << "Unhandled exception:" << endl
return EXIT_FAILURE;
}
catch (const std::exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * Unknown exception type. Ouch..." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}