mirror of
https://github.com/boostorg/mysql.git
synced 2026-02-14 00:42:53 +00:00
Field view
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <boost/mysql/value.hpp>
|
||||
#include <boost/mysql/field_view.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#define ASSERT(expr) \
|
||||
@@ -18,14 +18,14 @@
|
||||
void example_get()
|
||||
{
|
||||
//[value_example_get
|
||||
boost::mysql::value v ("hello"); // v contains type boost::string_view
|
||||
boost::mysql::field_view v ("hello"); // v contains type boost::string_view
|
||||
boost::string_view typed_val = v.get<boost::string_view>(); // retrieves the underlying string
|
||||
ASSERT(typed_val == "hello");
|
||||
try
|
||||
{
|
||||
v.get<double>(); // wrong type! throws boost::mysql::bad_value_access
|
||||
}
|
||||
catch (const boost::mysql::bad_value_access&)
|
||||
catch (const boost::mysql::bad_field_access&)
|
||||
{
|
||||
}
|
||||
//]
|
||||
@@ -34,7 +34,7 @@ void example_get()
|
||||
void example_get_optional()
|
||||
{
|
||||
//[value_example_get_optional
|
||||
boost::mysql::value v (3.14); // v contains type double
|
||||
boost::mysql::field_view v (3.14); // v contains type double
|
||||
boost::optional<double> typed_val = v.get_optional<double>();
|
||||
ASSERT(typed_val.has_value()); // the optional is not empty
|
||||
ASSERT(*typed_val == 3.14); // and contains the right value
|
||||
@@ -49,7 +49,7 @@ void example_get_optional()
|
||||
void example_get_std_optional()
|
||||
{
|
||||
//[value_example_get_std_optional
|
||||
boost::mysql::value v (3.14); // v contains type double
|
||||
boost::mysql::field_view v (3.14); // v contains type double
|
||||
std::optional<double> typed_val = v.get_std_optional<double>();
|
||||
ASSERT(typed_val.has_value()); // the optional is not empty
|
||||
ASSERT(*typed_val == 3.14); // and contains the right value
|
||||
@@ -64,7 +64,7 @@ void example_get_std_optional()
|
||||
void example_get_conversions()
|
||||
{
|
||||
//[value_example_get_conversions
|
||||
boost::mysql::value v (std::uint64_t(42)); // v contains type std::uint64_t
|
||||
boost::mysql::field_view v (std::uint64_t(42)); // v contains type std::uint64_t
|
||||
ASSERT(v.get<std::uint64_t>() == 42u); // exact type match
|
||||
ASSERT(v.get<std::int64_t>() == 42); // converts from std::uint64_t -> std::int64_t
|
||||
//]
|
||||
@@ -74,7 +74,7 @@ void example_inefficient()
|
||||
{
|
||||
//[value_example_inefficient
|
||||
// WARNING!! Inefficient, do NOT do
|
||||
boost::mysql::value v (3.14); // get the value e.g. using query
|
||||
boost::mysql::field_view v (3.14); // get the value e.g. using query
|
||||
if (v.is_convertible_to<double>()) // it should be double, but we are not use
|
||||
{
|
||||
double val = v.get<double>();
|
||||
@@ -91,7 +91,7 @@ void example_inefficient()
|
||||
void example_inefficient_ok()
|
||||
{
|
||||
//[value_example_inefficient_ok
|
||||
boost::mysql::value v (3.14); // get the value e.g. using query
|
||||
boost::mysql::field_view v (3.14); // get the value e.g. using query
|
||||
boost::optional<double> val = v.get_optional<double>();
|
||||
// it should be double, but we are not use
|
||||
if (val)
|
||||
@@ -109,7 +109,7 @@ void example_inefficient_ok()
|
||||
void example_is()
|
||||
{
|
||||
//[value_example_is
|
||||
boost::mysql::value v (std::uint64_t(42)); // v contains type std::uint64_t
|
||||
boost::mysql::field_view v (std::uint64_t(42)); // v contains type std::uint64_t
|
||||
ASSERT(v.is<std::uint64_t>()); // exact type match
|
||||
ASSERT(!v.is<std::int64_t>()); // does not consider conversions
|
||||
ASSERT(v.is_convertible_to<std::uint64_t>()); // exact type match
|
||||
|
||||
Reference in New Issue
Block a user