This example demonstrates how to use MetadataKey. It displays all the metadata keys for a particular PackageID and all repositories.
#include <iostream>
#include <iomanip>
#include <set>
#include <cstdlib>
using namespace paludis;
using namespace examples;
using std::cout;
using std::endl;
using std::left;
using std::setw;
namespace
{
void show_key(
const MetadataKey & key,
const std::string & indent =
"");
std::string stringify_string_pair(const std::pair<const std::string, std::string> & s)
{
if (s.first.empty())
return s.second;
else
return s.first + "=" + s.second;
}
class MetadataKeyInformationVisitor
{
private:
std::string indent;
public:
MetadataKeyInformationVisitor(const std::string & i = "") :
indent(i)
{
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataValueKey<std::string>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " << key.
parse_value() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataValueKey<SlotName>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " << key.
parse_value().raw_value() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataValueKey<long>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " << key.
parse_value() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataValueKey<bool>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " << key.
parse_value() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataValueKey<FSPath>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " << key.
parse_value() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " <<
"MetadataValueKey<std::shared_ptr<const PackageID> >" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " << *key.
parse_value() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataTimeKey" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " <<
"MetadataValueKey<std::shared_ptr<const Choices> > " << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSpecTreeKey<PlainTextSpecTree>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSpecTreeKey<RequiredUseSpecTree>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSpecTreeKey<LicenseSpecTree>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSpecTreeKey<SimpleURISpecTree>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSpecTreeKey<DependencySpecTree>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSpecTreeKey<FetchableURISpecTree>" << endl;
cout << indent << left << setw(30) <<
" Initial label:" <<
" " << key.
initial_label()->text() << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<KeywordNameSet>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<Set<std::string> >" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " <<
join(value->begin(), value->end(),
" ") << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<Map<std::string, std::string> >" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " <<
join(value->begin(), value->end(),
" ", stringify_string_pair) << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<Sequence<std::string> >" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " <<
join(value->begin(), value->end(),
" ") << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<Maintainers>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " <<
join(value->begin(), value->end(),
" ") << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<FSPathSequence>" << endl;
cout << indent << left << setw(30) <<
" Value:" <<
" " <<
join(value->begin(), value->end(),
" ") << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataCollectionKey<PackageIDSequence>" << endl;
}
{
cout << indent << left << setw(30) << " Class:" << " " << "MetadataSectionKey" << endl;
cout << indent << left << setw(30) << " Keys:" << endl;
for (MetadataSectionKey::MetadataConstIterator k(key.
begin_metadata()), k_end(key.
end_metadata()) ;
k != k_end ; ++k)
{
show_key(**k, indent + " ");
cout << endl;
}
}
};
void show_key(
const MetadataKey & key,
const std::string & indent)
{
cout << indent << left << setw(30) <<
" Raw name:" <<
" " << key.
raw_name() << endl;
cout << indent << left << setw(30) <<
" Human name:" <<
" " << key.
human_name() << endl;
cout << indent << left << setw(30) <<
" Type:" <<
" " << key.
type() << endl;
MetadataKeyInformationVisitor v(indent);
}
}
int main(int argc, char * argv[])
{
int exit_status(0);
try
{
CommandLine::get_instance()->run(argc, argv,
"example_metadata_key", "EXAMPLE_METADATA_KEY_OPTIONS", "EXAMPLE_METADATA_KEY_CMDLINE");
CommandLine::get_instance()->a_environment.argument()));
i != i_end ; ++i)
{
cout << **i << ":" << endl;
for (PackageID::MetadataConstIterator k((*i)->begin_metadata()), k_end((*i)->end_metadata()) ;
k != k_end ; ++k)
{
show_key(**k);
cout << endl;
}
cout << endl;
}
for (auto r(env->begin_repositories()), r_end(env->end_repositories()) ;
r != r_end ; ++r)
{
cout << (*r)->name() << ":" << endl;
for (Repository::MetadataConstIterator k((*r)->begin_metadata()), k_end((*r)->end_metadata()) ;
k != k_end ; ++k)
{
show_key(**k);
cout << endl;
}
cout << 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_status;
}