vg
tools for working with variation graphs
Classes | Static Public Member Functions | Static Public Attributes | Static Private Member Functions | List of all members
vg::io::Registry Class Reference

#include <registry.hpp>

Classes

struct  Tables
 

Static Public Member Functions

static bool register_everything ()
 
template<typename Message >
static void register_protobuf (const string &tag)
 
template<typename Handled , typename... Bases>
static void register_loader_saver (const string &tag, load_function_t loader, save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_loader_saver (const std::vector< std::string > &tags, load_function_t loader, save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_bare_loader_saver (const string &tag, bare_load_function_t loader, bare_save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_bare_loader_saver_with_magic (const string &tag, const string &magic, bare_load_function_t loader, bare_save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_bare_loader_saver_with_magics (const string &tag, const vector< string > &magics, bare_load_function_t loader, bare_save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_bare_loader_saver_with_header_check (const string &tag, function< bool(istream &)> sniff_header, bare_load_function_with_filename_t loader, bare_save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_bare_loader_saver_with_magic_and_filename (const string &tag, const string &magic, bare_load_function_with_filename_t loader, bare_save_function_t saver)
 
template<typename Handled , typename... Bases>
static void register_loader (const string &tag, load_function_t loader)
 
template<typename Handled , typename... Bases>
static void register_loader (const std::vector< std::string > &tags, load_function_t loader)
 
static bool is_valid_tag (const string &tag)
 
template<typename Message >
static const string & get_protobuf_tag ()
 
template<typename Message >
static bool check_protobuf_tag (const string &tag)
 
static bool sniff_magic (istream &stream, const string &magic)
 
template<typename Want >
static const load_function_tfind_loader (const string &tag)
 
template<typename Want >
static const vector< pair< bare_load_function_with_filename_t, function< bool(istream &)> > > * find_bare_loaders ()
 
template<typename Have >
static const pair< string, save_function_t > * find_saver ()
 

Static Public Attributes

static const size_t MAX_TAG_LENGTH = 25
 

Static Private Member Functions

static Tablesget_tables ()
 
::google::protobuf::util::TypeResolver & get_resolver ()
 
template<typename Handled , typename... Bases>
static void register_bare_loader (bare_load_function_t loader, function< bool(istream &)> sniff_header=nullptr)
 
template<typename Handled , typename... Bases>
static void register_bare_loader_with_filename (bare_load_function_with_filename_t loader, function< bool(istream &)> sniff_header=nullptr)
 
template<typename Handled >
static void register_saver (const string &tag, save_function_t saver)
 

Detailed Description

A registry mapping between tag strings for serialized message groups and the Protobuf types or serialization/deserialization handlers to use for them. All registration anmd lookup is done through static methods. Static methods are safe to call from other static initialization code.

We handle two kinds of registration right now:

Member Function Documentation

◆ check_protobuf_tag()

template<typename Message >
bool vg::io::Registry::check_protobuf_tag ( const string &  tag)
static

Check to see if the given tag is expected when deserializing Protobuf messages of the given tag.

◆ find_bare_loaders()

template<typename Want >
const vector< pair< bare_load_function_with_filename_t, function< bool(istream &)> > > * vg::io::Registry::find_bare_loaders
static

Look up the appropriate loader functions to use to load an object of the given type from a bare stream. If there are any registered, return a pointer to a vector of functions and their possibly empty stream prefixes that they require. The caller has to call the appropriate function and and cast the result to the right type. If there are no functions available, returns nullptr.

◆ find_loader()

template<typename Want >
const load_function_t * vg::io::Registry::find_loader ( const string &  tag)
static

Look up the appropriate loader function to use to load an object of the given type from data with the given tag. If there is one registered, return a pointer to it. The caller has to call it and cast the result to the right type. If there isn't, returns nullptr.

◆ find_saver()

template<typename Have >
const pair< string, save_function_t > * vg::io::Registry::find_saver
static

Look up the appropriate saver function to use to save an object of the given type. If there is one registered, return a pointer to a pair of the tag to use and the function. The caller has to call it and cast the result to the right type. If there isn't, returns nullptr.

◆ get_protobuf_tag()

template<typename Message >
const string & vg::io::Registry::get_protobuf_tag
static

Get the correct tag to use when serializing Protobuf messages of the given type.

◆ get_resolver()

auto vg::io::Registry::get_resolver ( )
staticprivate

Get a single shared Protobuf resolver we will use for the duration of the program. The Protobuf docs say the resolver must be thread safe.

◆ get_tables()

auto vg::io::Registry::get_tables ( )
staticprivate

Get or create the registry tables in which things are registerd.

◆ is_valid_tag()

auto vg::io::Registry::is_valid_tag ( const string &  tag)
static

Determine if the given tag string loaded from a file is a valid/possible tag, or if it should be interpreted as message data from a pre-tags VG file instead. Only tag values literally registered with the registry are valid. NOT thread-safe to run simultaneously with tag registrations.

◆ register_bare_loader()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader ( bare_load_function_t  loader,
function< bool(istream &)>  sniff_header = nullptr 
)
staticprivate

Register a load function for loading the given types from demultiplexed streams. If a header sniffing function is provided, the function will also be used on the whole input stream if it matches the function, skipping demultiplexing of type-tagged messages.

◆ register_bare_loader_saver()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader_saver ( const string &  tag,
bare_load_function_t  loader,
bare_save_function_t  saver 
)
static

Register a loading function and a saving function with the given tag for the given object type and list of base classes. The functions operate on bare streams; conversion to type-tagged messages of chunks of stream data is performed automatically.

Because, without a magic value or header check function, we can't reject type-tagged message files, loaders registered with this function won't actually be used when a non-type-tagged file is loaded. See register_bare_loader_saver_with_magic(), or use register_bare_loader_saver_with_header_check() with an always true check function if you do not need to load type-tagged messages.

◆ register_bare_loader_saver_with_header_check()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader_saver_with_header_check ( const string &  tag,
function< bool(istream &)>  sniff_header,
bare_load_function_with_filename_t  loader,
bare_save_function_t  saver 
)
static

Generealization of register_bare_loader_saver_with_magic, where user can supply their own header-checking function. Check function must peek/get/unget and may not seek/tell.

◆ register_bare_loader_saver_with_magic()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader_saver_with_magic ( const string &  tag,
const string &  magic,
bare_load_function_t  loader,
bare_save_function_t  saver 
)
static

Like register_bare_loader_saver(), except that additionally the function will be used when attempting to load any of the base classes when the file begins with the specified magic bytes. Type-tagged files with the given tag that do not match the magic will be decoded into a stream and loaded with the loader.

◆ register_bare_loader_saver_with_magic_and_filename()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader_saver_with_magic_and_filename ( const string &  tag,
const string &  magic,
bare_load_function_with_filename_t  loader,
bare_save_function_t  saver 
)
static

Generalization of register_bare_loader_saver_with_magic that can also take a filename

◆ register_bare_loader_saver_with_magics()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader_saver_with_magics ( const string &  tag,
const vector< string > &  magics,
bare_load_function_t  loader,
bare_save_function_t  saver 
)
static

Like register_bare_loader_saver_with_magic(), except that multiple magic values are supported.

◆ register_bare_loader_with_filename()

template<typename Handled , typename... Bases>
void vg::io::Registry::register_bare_loader_with_filename ( bare_load_function_with_filename_t  loader,
function< bool(istream &)>  sniff_header = nullptr 
)
staticprivate

Slight generalization of above that allows filename to be passed alongside the input stream.

◆ register_everything()

auto vg::io::Registry::register_everything ( )
static

Register everything. Main entry point. When you have new things to register, add calls to your function to this method. Returns true on success.

◆ register_loader() [1/2]

template<typename Handled , typename... Bases>
void vg::io::Registry::register_loader ( const std::vector< std::string > &  tags,
load_function_t  loader 
)
static

Register a loading function for the given collection of tags. If "" appears in the list of tags, the loader can be deployed on untagged message groups (for backward compatibility).

◆ register_loader() [2/2]

template<typename Handled , typename... Bases>
void vg::io::Registry::register_loader ( const string &  tag,
load_function_t  loader 
)
static

Register a load function for a tag. The empty tag means it can run on untagged message groups. If any Bases are passed, we will use this loader to load when one of those types is requested and this tag is encountered.

◆ register_loader_saver() [1/2]

template<typename Handled , typename... Bases>
void vg::io::Registry::register_loader_saver ( const std::vector< std::string > &  tags,
load_function_t  loader,
save_function_t  saver 
)
static

Register a loading function and a saving function with the given collection of tags for the given object type and list of base classes. The first tag in the collection will be used for saving. If "" appears in the list of tags, the loader can be deployed on untagged message groups (for backward compatibility).

◆ register_loader_saver() [2/2]

template<typename Handled , typename... Bases>
void vg::io::Registry::register_loader_saver ( const string &  tag,
load_function_t  loader,
save_function_t  saver 
)
static

Register a loading function and a saving function with the given tag for the given object type and the given base classes.

◆ register_protobuf()

template<typename Message >
void vg::io::Registry::register_protobuf ( const string &  tag)
static

Register a Protobuf Message type to be associated with the given string tag. By default, Protobuf types use the name string from the Protobuf library as a tag. But this can and should be overridden with something short that will never change.

◆ register_saver()

template<typename Handled >
void vg::io::Registry::register_saver ( const string &  tag,
save_function_t  saver 
)
staticprivate

Register a save function to save a type with a given tag. The empty tag is not permitted.

◆ sniff_magic()

bool vg::io::Registry::sniff_magic ( istream &  stream,
const string &  magic 
)
static

Return true of the given stream starts with the given magic number prefix, and false otherwise. Returns the stream to its initial position regardless of the result.

Member Data Documentation

◆ MAX_TAG_LENGTH

const size_t vg::io::Registry::MAX_TAG_LENGTH = 25
static

To help with telling messages from tags, we enforce a max tag length. If we ever allow tags of 139 bytes or longer, we risk having uncompressed files starting with the gzip magic number.


The documentation for this class was generated from the following files: