Skip to content

Commit

Permalink
Read fields as std::optional, if available.
Browse files Browse the repository at this point in the history
New field method: get<T>(), returns an optional.  If the field is null,
the optional is empty.  Otherwise, it contains the field's value.
  • Loading branch information
jtv committed Dec 11, 2016
1 parent 51812f1 commit d6f3057
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2016-12-11 Jeroen T. Vermeulen <jtv@xs4all.nl>
configitems, configure.ac.in, include/pqxx/field.hxx:
- Support std::optional or std::experimental::optional if available.
- New field::get returns optional.
2016-12-11 Jeroen T. Vermeulen <jtv@xs4all.nl>
include/pqxx/binarystring.hxx, include/pqxx/result.hxx, include/pqxx/row.hxx,
src/result.cxx, src/row.cxx:
Expand Down
6 changes: 3 additions & 3 deletions config/ltmain.sh
Expand Up @@ -31,7 +31,7 @@

PROGRAM=libtool
PACKAGE=libtool
VERSION="2.4.6 Debian-2.4.6-0.1"
VERSION="2.4.6 Debian-2.4.6-1"
package_revision=2.4.6


Expand Down Expand Up @@ -1977,7 +1977,7 @@ func_version ()
# End:

# Set a version string.
scriptversion='(GNU libtool) 2.4.6'
scriptversion='(GNU libtool) 2.4.6 Debian-2.4.6-1'


# func_echo ARG...
Expand Down Expand Up @@ -2068,7 +2068,7 @@ include the following information:
compiler: $LTCC
compiler flags: $LTCFLAGS
linker: $LD (gnu? $with_gnu_ld)
version: $progname (GNU libtool) 2.4.6
version: $progname $scriptversion
automake: `($AUTOMAKE --version) 2>/dev/null |$SED 1q`
autoconf: `($AUTOCONF --version) 2>/dev/null |$SED 1q`
Expand Down
1 change: 0 additions & 1 deletion config/m4/libtool.m4
Expand Up @@ -728,7 +728,6 @@ _LT_CONFIG_SAVE_COMMANDS([
cat <<_LT_EOF >> "$cfgfile"
#! $SHELL
# Generated automatically by $as_me ($PACKAGE) $VERSION
# Libtool was configured on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
# NOTE: Changes made to this file will be lost: look at ltmain.sh.
# Provide generalized library-building support services.
Expand Down
2 changes: 2 additions & 0 deletions configitems
Expand Up @@ -20,6 +20,8 @@ PQXX_HAVE_FINAL public compiler
PQXX_HAVE_GCC_VISIBILITY internal compiler
PQXX_HAVE_ISINF internal compiler
PQXX_HAVE_ISNAN internal compiler
PQXX_HAVE_OPTIONAL public compiler
PQXX_HAVE_EXP_OPTIONAL public compiler
PQXX_HAVE_MOVE public compiler
PQXX_HAVE_NOEXCEPT public compiler
PQXX_HAVE_NORETURN public compiler
Expand Down
51 changes: 50 additions & 1 deletion configure
Expand Up @@ -16602,6 +16602,56 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $final_keyword" >&5
$as_echo "$final_keyword" >&6; }

{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for C++17 std::optional support" >&5
$as_echo_n "checking for C++17 std::optional support... " >&6; }
have_optional=yes
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#include <optional>
int
main ()
{
return std::optional<int>(0).value()
;
return 0;
}
_ACEOF
if ac_fn_cxx_try_compile "$LINENO"; then :

$as_echo "#define PQXX_HAVE_OPTIONAL 1" >>confdefs.h

else
have_optional=no
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_optional" >&5
$as_echo "$have_optional" >&6; }

{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for experimental C++17 std::optional support" >&5
$as_echo_n "checking for experimental C++17 std::optional support... " >&6; }
have_exp_optional=yes
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#include <experimental/optional>
int
main ()
{
return std::experimental::optional<int>(0).value()
;
return 0;
}
_ACEOF
if ac_fn_cxx_try_compile "$LINENO"; then :

$as_echo "#define PQXX_HAVE_EXP_OPTIONAL 1" >>confdefs.h

else
have_exp_optional=no
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $have_exp_optional" >&5
$as_echo "$have_exp_optional" >&6; }

boost_smart_ptr=yes

ac_fn_cxx_check_header_mongrel "$LINENO" "boost/smart_ptr.hpp" "ac_cv_header_boost_smart_ptr_hpp" "$ac_includes_default"
Expand Down Expand Up @@ -19715,7 +19765,6 @@ $as_echo X"$file" |
cat <<_LT_EOF >> "$cfgfile"
#! $SHELL
# Generated automatically by $as_me ($PACKAGE) $VERSION
# Libtool was configured on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
# NOTE: Changes made to this file will be lost: look at ltmain.sh.

# Provide generalized library-building support services.
Expand Down
24 changes: 24 additions & 0 deletions configure.ac
Expand Up @@ -261,6 +261,30 @@ AC_TRY_COMPILE(
final_keyword=no)
AC_MSG_RESULT($final_keyword)

AC_MSG_CHECKING([for C++17 std::optional support])
have_optional=yes
AC_TRY_COMPILE(
[#include <optional>],
[return std::optional<int>(0).value()],
AC_DEFINE(
[PQXX_HAVE_OPTIONAL],
1,
[Define if the compiler supports std::optional.]),
have_optional=no)
AC_MSG_RESULT($have_optional)

AC_MSG_CHECKING([for experimental C++17 std::optional support])
have_exp_optional=yes
AC_TRY_COMPILE(
[#include <experimental/optional>],
[return std::experimental::optional<int>(0).value()],
AC_DEFINE(
[PQXX_HAVE_EXP_OPTIONAL],
1,
[Define if the compiler supports std::experimental::optional.]),
have_exp_optional=no)
AC_MSG_RESULT($have_exp_optional)

boost_smart_ptr=yes
AC_CHECK_HEADER(boost/smart_ptr.hpp, AC_DEFINE(PQXX_HAVE_BOOST_SMART_PTR, 1,
[Define if you have the <boost/smart_ptr.hpp> header]),
Expand Down
24 changes: 24 additions & 0 deletions configure.ac.in
Expand Up @@ -261,6 +261,30 @@ AC_TRY_COMPILE(
final_keyword=no)
AC_MSG_RESULT($final_keyword)

AC_MSG_CHECKING([for C++17 std::optional support])
have_optional=yes
AC_TRY_COMPILE(
[#include <optional>],
[return std::optional<int>(0).value()],
AC_DEFINE(
[PQXX_HAVE_OPTIONAL],
1,
[Define if the compiler supports std::optional.]),
have_optional=no)
AC_MSG_RESULT($have_optional)

AC_MSG_CHECKING([for experimental C++17 std::optional support])
have_exp_optional=yes
AC_TRY_COMPILE(
[#include <experimental/optional>],
[return std::experimental::optional<int>(0).value()],
AC_DEFINE(
[PQXX_HAVE_EXP_OPTIONAL],
1,
[Define if the compiler supports std::experimental::optional.]),
have_exp_optional=no)
AC_MSG_RESULT($have_exp_optional)

boost_smart_ptr=yes
AC_CHECK_HEADER(boost/smart_ptr.hpp, AC_DEFINE(PQXX_HAVE_BOOST_SMART_PTR, 1,
[Define if you have the <boost/smart_ptr.hpp> header]),
Expand Down
6 changes: 6 additions & 0 deletions include/pqxx/config.h.in
Expand Up @@ -75,6 +75,9 @@
/* Define if distance() works according to the standard */
#undef PQXX_HAVE_DISTANCE

/* Define if the compiler supports std::experimental::optional. */
#undef PQXX_HAVE_EXP_OPTIONAL

/* Define if the compiler supports the final keyword. */
#undef PQXX_HAVE_FINAL

Expand Down Expand Up @@ -108,6 +111,9 @@
/* Define if compiler supports [[noreturn]] attribute */
#undef PQXX_HAVE_NORETURN

/* Define if the compiler supports std::optional. */
#undef PQXX_HAVE_OPTIONAL

/* Define if the compiler supports the override keyword. */
#undef PQXX_HAVE_OVERRIDE

Expand Down
34 changes: 34 additions & 0 deletions include/pqxx/field.hxx
Expand Up @@ -22,6 +22,14 @@
#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"

#if defined(PQXX_HAVE_OPTIONAL)
#include <optional>
#endif

#if defined(PQXX_HAVE_EXP_OPTIONAL)
#include <experimental/optional>
#endif

#include "pqxx/strconv"


Expand Down Expand Up @@ -165,7 +173,33 @@ public:
return Obj;
}

#if defined(PQXX_HAVE_OPTIONAL)
/// Return value as std::optional, or blank value if null.
template<typename T> std::optional<T> get() const
{
typedef std::optional<T> optional;
if (is_null()) return optional();
else return optional(as<T>());
}
#endif

#if defined(PQXX_HAVE_EXP_OPTIONAL)
/// Return value as std::experimental::optional, or blank value if null.
template<typename T> std::experimental::optional<T> get() const
{
typedef std::experimental::optional<T> optional;
if (is_null()) return optional();
else return optional(as<T>());
}
#endif

/// Is this field's value null?
bool is_null() const PQXX_NOEXCEPT; //[t12]

/// Return number of bytes taken up by the field's value.
/**
* Includes the terminating zero byte.
*/
size_type size() const PQXX_NOEXCEPT; //[t11]
//@}

Expand Down
2 changes: 2 additions & 0 deletions test/unit/Makefile.am
Expand Up @@ -34,7 +34,9 @@ runner_SOURCES = \
test_pipeline.cxx \
test_prepared_statement.cxx \
test_read_transaction.cxx \
test_result_iteration.cxx \
test_result_slicing.cxx \
test_row.cxx \
test_simultaneous_transactions.cxx \
test_sql_cursor.cxx \
test_stateless_cursor.cxx \
Expand Down
9 changes: 7 additions & 2 deletions test/unit/Makefile.in
Expand Up @@ -126,8 +126,9 @@ am_runner_OBJECTS = test_binarystring.$(OBJEXT) \
test_exceptions.$(OBJEXT) test_float.$(OBJEXT) \
test_notification.$(OBJEXT) test_parameterized.$(OBJEXT) \
test_pipeline.$(OBJEXT) test_prepared_statement.$(OBJEXT) \
test_read_transaction.$(OBJEXT) test_result_slicing.$(OBJEXT) \
test_simultaneous_transactions.$(OBJEXT) \
test_read_transaction.$(OBJEXT) \
test_result_iteration.$(OBJEXT) test_result_slicing.$(OBJEXT) \
test_row.$(OBJEXT) test_simultaneous_transactions.$(OBJEXT) \
test_sql_cursor.$(OBJEXT) test_stateless_cursor.$(OBJEXT) \
test_string_conversion.$(OBJEXT) test_subtransaction.$(OBJEXT) \
test_test_helpers.$(OBJEXT) test_thread_safety_model.$(OBJEXT) \
Expand Down Expand Up @@ -560,7 +561,9 @@ runner_SOURCES = \
test_pipeline.cxx \
test_prepared_statement.cxx \
test_read_transaction.cxx \
test_result_iteration.cxx \
test_result_slicing.cxx \
test_row.cxx \
test_simultaneous_transactions.cxx \
test_sql_cursor.cxx \
test_stateless_cursor.cxx \
Expand Down Expand Up @@ -637,7 +640,9 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_pipeline.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_prepared_statement.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_read_transaction.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_result_iteration.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_result_slicing.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_row.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_simultaneous_transactions.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_sql_cursor.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_stateless_cursor.Po@am__quote@
Expand Down
6 changes: 6 additions & 0 deletions win32/vc-test-unit.mak
Expand Up @@ -92,7 +92,9 @@ OBJS= \
$(INTDIR)\test_pipeline.obj \
$(INTDIR)\test_prepared_statement.obj \
$(INTDIR)\test_read_transaction.obj \
$(INTDIR)\test_result_iteration.obj \
$(INTDIR)\test_result_slicing.obj \
$(INTDIR)\test_row.obj \
$(INTDIR)\test_simultaneous_transactions.obj \
$(INTDIR)\test_sql_cursor.obj \
$(INTDIR)\test_stateless_cursor.obj \
Expand Down Expand Up @@ -158,8 +160,12 @@ $(INTDIR)\test_prepared_statement.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_prepared_statement.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_read_transaction.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_read_transaction.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_result_iteration.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_result_iteration.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_result_slicing.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_result_slicing.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_row.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_row.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_simultaneous_transactions.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_simultaneous_transactions.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_sql_cursor.obj:
Expand Down

0 comments on commit d6f3057

Please sign in to comment.