util: stop including any/optional/variant from backports.hpp

Give each utility its own public header util/{any,optional,variant}.hpp

Change-Id: I4850e7bbff3ce1aa4fe7bbb64e7262c39af387aa
diff --git a/ndn-cxx/util/nonstd/any.hpp b/ndn-cxx/detail/nonstd/any-lite.hpp
similarity index 100%
rename from ndn-cxx/util/nonstd/any.hpp
rename to ndn-cxx/detail/nonstd/any-lite.hpp
diff --git a/ndn-cxx/util/nonstd/optional.hpp b/ndn-cxx/detail/nonstd/optional-lite.hpp
similarity index 100%
rename from ndn-cxx/util/nonstd/optional.hpp
rename to ndn-cxx/detail/nonstd/optional-lite.hpp
diff --git a/ndn-cxx/util/nonstd/scope-lite.hpp b/ndn-cxx/detail/nonstd/scope-lite.hpp
similarity index 100%
rename from ndn-cxx/util/nonstd/scope-lite.hpp
rename to ndn-cxx/detail/nonstd/scope-lite.hpp
diff --git a/ndn-cxx/util/nonstd/variant.hpp b/ndn-cxx/detail/nonstd/variant-lite.hpp
similarity index 100%
rename from ndn-cxx/util/nonstd/variant.hpp
rename to ndn-cxx/detail/nonstd/variant-lite.hpp
diff --git a/ndn-cxx/key-locator.hpp b/ndn-cxx/key-locator.hpp
index d502d3d..1643b5b 100644
--- a/ndn-cxx/key-locator.hpp
+++ b/ndn-cxx/key-locator.hpp
@@ -23,6 +23,7 @@
 #define NDN_CXX_KEY_LOCATOR_HPP
 
 #include "ndn-cxx/name.hpp"
+#include "ndn-cxx/util/variant.hpp"
 
 namespace ndn {
 
diff --git a/ndn-cxx/meta-info.hpp b/ndn-cxx/meta-info.hpp
index 833e315..9503e44 100644
--- a/ndn-cxx/meta-info.hpp
+++ b/ndn-cxx/meta-info.hpp
@@ -25,6 +25,7 @@
 #include "ndn-cxx/name-component.hpp"
 #include "ndn-cxx/encoding/block.hpp"
 #include "ndn-cxx/encoding/encoding-buffer.hpp"
+#include "ndn-cxx/util/optional.hpp"
 #include "ndn-cxx/util/time.hpp"
 
 #include <list>
diff --git a/ndn-cxx/mgmt/nfd/face-query-filter.hpp b/ndn-cxx/mgmt/nfd/face-query-filter.hpp
index 742b6ba..4362acc 100644
--- a/ndn-cxx/mgmt/nfd/face-query-filter.hpp
+++ b/ndn-cxx/mgmt/nfd/face-query-filter.hpp
@@ -24,6 +24,7 @@
 
 #include "ndn-cxx/encoding/block.hpp"
 #include "ndn-cxx/encoding/nfd-constants.hpp"
+#include "ndn-cxx/util/optional.hpp"
 
 namespace ndn {
 namespace nfd {
diff --git a/ndn-cxx/mgmt/nfd/face-status.hpp b/ndn-cxx/mgmt/nfd/face-status.hpp
index 3f28aa6..5419694 100644
--- a/ndn-cxx/mgmt/nfd/face-status.hpp
+++ b/ndn-cxx/mgmt/nfd/face-status.hpp
@@ -23,6 +23,7 @@
 #define NDN_CXX_MGMT_NFD_FACE_STATUS_HPP
 
 #include "ndn-cxx/mgmt/nfd/face-traits.hpp"
+#include "ndn-cxx/util/optional.hpp"
 #include "ndn-cxx/util/time.hpp"
 
 namespace ndn {
diff --git a/ndn-cxx/name.hpp b/ndn-cxx/name.hpp
index fde6ebe..4acd323 100644
--- a/ndn-cxx/name.hpp
+++ b/ndn-cxx/name.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2020 Regents of the University of California.
+ * Copyright (c) 2013-2021 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -27,6 +27,7 @@
 #define NDN_CXX_NAME_HPP
 
 #include "ndn-cxx/name-component.hpp"
+#include "ndn-cxx/util/optional.hpp"
 
 #include <iterator>
 
diff --git a/ndn-cxx/net/impl/netlink-message.hpp b/ndn-cxx/net/impl/netlink-message.hpp
index d960b93..dc80320 100644
--- a/ndn-cxx/net/impl/netlink-message.hpp
+++ b/ndn-cxx/net/impl/netlink-message.hpp
@@ -26,6 +26,7 @@
 
 #include "ndn-cxx/detail/common.hpp"
 #include "ndn-cxx/net/ethernet.hpp"
+#include "ndn-cxx/util/optional.hpp"
 
 #ifndef NDN_CXX_HAVE_NETLINK
 #error "This file should not be included ..."
diff --git a/ndn-cxx/util/any.hpp b/ndn-cxx/util/any.hpp
new file mode 100644
index 0000000..ef27c6e
--- /dev/null
+++ b/ndn-cxx/util/any.hpp
@@ -0,0 +1,40 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2021 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_CXX_UTIL_ANY_HPP
+#define NDN_CXX_UTIL_ANY_HPP
+
+#define any_CONFIG_SELECT_ANY any_ANY_NONSTD
+#include "ndn-cxx/detail/nonstd/any-lite.hpp"
+
+namespace ndn {
+
+using ::nonstd::any;
+using ::nonstd::any_cast;
+using ::nonstd::bad_any_cast;
+using ::nonstd::make_any;
+
+using ::nonstd::in_place;
+using ::nonstd::in_place_t;
+
+} // namespace ndn
+
+#endif // NDN_CXX_UTIL_ANY_HPP
diff --git a/ndn-cxx/util/backports.hpp b/ndn-cxx/util/backports.hpp
index 9150523..6948f38 100644
--- a/ndn-cxx/util/backports.hpp
+++ b/ndn-cxx/util/backports.hpp
@@ -79,10 +79,6 @@
 #  define NDN_CXX_UNREACHABLE std::abort()
 #endif
 
-#include "ndn-cxx/util/nonstd/any.hpp"
-#include "ndn-cxx/util/nonstd/optional.hpp"
-#include "ndn-cxx/util/nonstd/variant.hpp"
-
 #ifndef NDN_CXX_HAVE_STD_TO_STRING
 #include <boost/lexical_cast.hpp>
 #endif
@@ -130,42 +126,21 @@
 
 //
 // https://wg21.link/P1682
-// std::to_underlying() (approved for LWG as of July 2019)
+// std::to_underlying() (C++23)
 //
-#if __cpp_lib_to_underlying >= 202002L
+#if __cpp_lib_to_underlying >= 202102L
 using std::to_underlying;
 #else
 template<typename T>
-constexpr std::underlying_type_t<T>
+NDN_CXX_NODISCARD constexpr std::underlying_type_t<T>
 to_underlying(T val) noexcept
 {
+  // instantiating underlying_type with a non-enum type is UB before C++20
   static_assert(std::is_enum<T>::value, "");
   return static_cast<std::underlying_type_t<T>>(val);
 }
 #endif // __cpp_lib_to_underlying
 
-using ::nonstd::any;
-using ::nonstd::any_cast;
-using ::nonstd::bad_any_cast;
-using ::nonstd::make_any;
-
-using ::nonstd::optional;
-using ::nonstd::bad_optional_access;
-using ::nonstd::nullopt;
-using ::nonstd::nullopt_t;
-using ::nonstd::in_place;
-using ::nonstd::in_place_t;
-using ::nonstd::make_optional;
-
-using ::nonstd::variant;
-using ::nonstd::bad_variant_access;
-using ::nonstd::monostate;
-using ::nonstd::variant_npos;
-using ::nonstd::get;
-using ::nonstd::get_if;
-using ::nonstd::holds_alternative;
-using ::nonstd::visit;
-
 } // namespace ndn
 
 #endif // NDN_CXX_UTIL_BACKPORTS_HPP
diff --git a/ndn-cxx/util/io.hpp b/ndn-cxx/util/io.hpp
index f032062..99b798d 100644
--- a/ndn-cxx/util/io.hpp
+++ b/ndn-cxx/util/io.hpp
@@ -24,6 +24,7 @@
 
 #include "ndn-cxx/encoding/block.hpp"
 #include "ndn-cxx/util/concepts.hpp"
+#include "ndn-cxx/util/optional.hpp"
 
 #include <fstream>
 
diff --git a/ndn-cxx/util/optional.hpp b/ndn-cxx/util/optional.hpp
new file mode 100644
index 0000000..ab3d88c
--- /dev/null
+++ b/ndn-cxx/util/optional.hpp
@@ -0,0 +1,41 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2021 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_CXX_UTIL_OPTIONAL_HPP
+#define NDN_CXX_UTIL_OPTIONAL_HPP
+
+#define optional_CONFIG_SELECT_OPTIONAL optional_OPTIONAL_NONSTD
+#include "ndn-cxx/detail/nonstd/optional-lite.hpp"
+
+namespace ndn {
+
+using ::nonstd::optional;
+using ::nonstd::bad_optional_access;
+using ::nonstd::nullopt;
+using ::nonstd::nullopt_t;
+using ::nonstd::make_optional;
+
+using ::nonstd::in_place;
+using ::nonstd::in_place_t;
+
+} // namespace ndn
+
+#endif // NDN_CXX_UTIL_OPTIONAL_HPP
diff --git a/ndn-cxx/util/scope.hpp b/ndn-cxx/util/scope.hpp
index 2a2edd9..9133eda 100644
--- a/ndn-cxx/util/scope.hpp
+++ b/ndn-cxx/util/scope.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2020 Regents of the University of California.
+ * Copyright (c) 2013-2021 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -23,7 +23,7 @@
 #define NDN_CXX_UTIL_SCOPE_HPP
 
 #define scope_CONFIG_SELECT_SCOPE scope_SCOPE_NONSTD
-#include "ndn-cxx/util/nonstd/scope-lite.hpp"
+#include "ndn-cxx/detail/nonstd/scope-lite.hpp"
 
 namespace ndn {
 
diff --git a/ndn-cxx/util/variant.hpp b/ndn-cxx/util/variant.hpp
new file mode 100644
index 0000000..51acacb
--- /dev/null
+++ b/ndn-cxx/util/variant.hpp
@@ -0,0 +1,51 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2021 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_CXX_UTIL_VARIANT_HPP
+#define NDN_CXX_UTIL_VARIANT_HPP
+
+#define variant_CONFIG_SELECT_VARIANT variant_VARIANT_NONSTD
+#define variant_CONFIG_OMIT_VARIANT_SIZE_V_MACRO 1
+#define variant_CONFIG_OMIT_VARIANT_ALTERNATIVE_T_MACRO 1
+#include "ndn-cxx/detail/nonstd/variant-lite.hpp"
+
+namespace ndn {
+
+using ::nonstd::variant;
+using ::nonstd::monostate;
+using ::nonstd::bad_variant_access;
+using ::nonstd::variant_npos;
+using ::nonstd::variant_size;
+using ::nonstd::variant_size_v;
+using ::nonstd::variant_alternative;
+using ::nonstd::variant_alternative_t;
+
+using ::nonstd::visit;
+using ::nonstd::holds_alternative;
+using ::nonstd::get;
+using ::nonstd::get_if;
+
+using ::nonstd::in_place;
+using ::nonstd::in_place_t;
+
+} // namespace ndn
+
+#endif // NDN_CXX_UTIL_VARIANT_HPP