example_dep_tag.cc
This example demonstrates how to handle dependency tags. It displays information about the 'security' and 'world' sets.
#include <paludis/paludis.hh>
#include "example_command_line.hh"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <set>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::setw;
using std::left;
using std::boolalpha;
typedef std::set<std::string> SeenCategories;
namespace
{
class TagDisplayer :
public ConstVisitor<DepTagVisitorTypes>
{
public:
void visit(const DependencyDepTag & tag)
{
if (tag.package_id())
cout << left << setw(20) << " Package ID:" << " " << *tag.package_id() << endl;
if (tag.dependency())
cout << left << setw(20) << " Dependency:" << " " << *tag.dependency() << endl;
}
void visit(const GLSADepTag & tag)
{
cout << left << setw(20) << " GLSA title:" << " " << tag.glsa_title() << endl;
}
void visit(const GeneralSetDepTag & tag)
{
cout << left << setw(20) << " Source:" << " " << tag.source() << endl;
}
void visit(const TargetDepTag &)
{
}
};
void display_set(
const std::tr1::shared_ptr<const Environment> & env,
const SetName & name,
SeenCategories & seen_categories)
{
std::tr1::shared_ptr<const SetSpecTree::ConstItem> set(env->set(name));
if (! set)
return;
DepSpecFlattener<SetSpecTree, PackageDepSpec> set_flat(env.get());
set->accept(set_flat);
cout << "Set '" << name << "':" << endl;
for (DepSpecFlattener<SetSpecTree, PackageDepSpec>::ConstIterator s(set_flat.begin()),
s_end(set_flat.end()) ; s != s_end ; ++s)
{
if (! (*s)->tag())
continue;
cout << " " << **s << ": " << endl;
cout << left << setw(20) << " Short text:" << " " << (*s)->tag()->short_text() << endl;
cout << left << setw(20) << " Category:" << " " << (*s)->tag()->category() << endl;
seen_categories.insert((*s)->tag()->category());
TagDisplayer displayer;
(*s)->tag()->accept(displayer);
cout << endl;
}
cout << endl;
}
}
int main(int argc, char * argv[])
{
try
{
CommandLine::get_instance()->run(argc, argv,
"example_dep_tag", "EXAMPLE_DEP_TAG_OPTIONS", "EXAMPLE_DEP_TAG_CMDLINE");
std::tr1::shared_ptr<Environment> env(EnvironmentFactory::get_instance()->create(
CommandLine::get_instance()->a_environment.argument()));
SeenCategories seen_categories;
display_set(env, SetName("security"), seen_categories);
display_set(env, SetName("world"), seen_categories);
cout << "Seen categories:" << endl;
for (SeenCategories::const_iterator s(seen_categories.begin()), s_end(seen_categories.end()) ;
s != s_end ; ++s)
{
cout << " " << *s << ":" << endl;
std::tr1::shared_ptr<const DepTagCategory> category(DepTagCategoryFactory::get_instance()->create(*s));
cout << left << setw(20) << " Visible:" << " " << boolalpha << category->visible() << endl;
cout << left << setw(20) << " ID:" << " " << category->id() << endl;
cout << left << setw(20) << " Title:" << " " << category->title() << endl;
cout << left << setw(20) << " Pre text:" << " " << category->pre_text() << endl;
cout << left << setw(20) << " Post text:" << " " << category->post_text() << endl;
cout << endl;
}
}
catch (const Exception & e)
{
cout << endl;
cout << "Unhandled exception:" << endl
<< " * " << e.backtrace("\n * ")
<< e.message() << " (" << e.what() << ")" << 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;
}