Dmitigr Pgfe  1.1beta
The modern C++ API to PostgreSQL
List of all members
Conversions< T > Struct Template Reference

Represents a centralized "namespace" for implementation of conversion algorithms. More...

#include <dmitigr/pgfe/conversions_api.hpp>

Inheritance diagram for Conversions< T >:
Basic_conversions< T, detail::Generic_string_conversions< T >, detail::Generic_data_conversions< T > >

Additional Inherited Members

- Public Types inherited from Basic_conversions< T, detail::Generic_string_conversions< T >, detail::Generic_data_conversions< T > >
using Type = T
 The native type. More...
 
- Static Public Member Functions inherited from Basic_conversions< T, detail::Generic_string_conversions< T >, detail::Generic_data_conversions< T > >
static Type to_type (const Data *const data, Types &&... args)
 
static Type to_type (std::unique_ptr< Data > &&data, Types &&... args)
 
static std::enable_if_t< std::is_same_v< std::string, std::decay_t< String > >, Typeto_type (String &&text, Types &&... args)
 
static std::enable_if_t< std::is_same_v< Type, std::decay_t< U > >, std::unique_ptr< Data > > to_data (U &&value, Types &&... args)
 
static std::enable_if_t< std::is_same_v< Type, std::decay_t< U > >, std::string > to_string (U &&value, Types &&... args)
 

Detailed Description

template<typename T>
struct dmitigr::pgfe::Conversions< T >

Represents a centralized "namespace" for implementation of conversion algorithms.

The generic implementation of the conversion algorithms is based on the Standard C++ library's string streams. Thus if the

operator<<(std::ostream&, const T&)

and the

operator>>(std::istream&, T&)

are implemented then the data conversions are already available and there is no necessity to specialize the structure template Conversions.

The overhead of standard streams can be avoided by using template specializations. Each specialization for the type T of the template structure Conversions must define:

template<> struct Conversions<T> {
static T to_type(const std::string& text, Types&& ... args); // 1
static std::string to_string(const T& value, Types&& ... args); // 2
static T to_type(const Data* data, Types&& ... args); // 3
static std::unique_ptr<Data> to_data(const T& value, Types&& ... args); // 4
};

Optionally (for convenience/optimization is some cases) the following can be defined:

template<> struct Conversions<T> {
static T to_type(std::string&& text, Types&& ... args); // 5
static std::string to_string(T&& value, Types&& ... args); // 6
static T to_type(std::unique_ptr<Data>&& data, Types&& ... args); // 7
static std::unique_ptr<Data> to_data(T&& value, Types&& ... args); // 8
};

These functions are used in the different contexts:

Variadic arguments (args) are optional and may be used in cases when some extra information (for example, a server version) need to be passed into the conversion routine. Absence of these arguments for conversion routines provided by Pgfe means "the latest default". In particular it means that there is no guarantee that conversion will work properly with data from PostgreSQL servers of older versions than the latest one. If the application works with the PostgreSQL servers of the same (latest) versions these arguments can be just ignored in most cases.

Remarks
When using the generic conversions implementation std::runtime_error will be thrown if the following is not fulfilled:
  • effect of operator>> must be (stream.eof() == true);
  • effect of operator<< must be (stream.fail() == false).
In most cases there is no need to use the template structure Conversions directly. Template functions to() and to_data() should be used instead.
See also
to(), to_data()

The documentation for this struct was generated from the following file: