forked from lix-project/lix
Stop vendoring toml11
We don't apply any patches to it, and vendoring it locks users into
bugs (it hasn't been updated since its introduction in late 2021).
Closes lix-project/lix#164
Change-Id: Ied071c841fc30b0dfb575151afd1e7f66970fdb9
This commit is contained in:
parent
edba570664
commit
80405d0626
|
@ -342,6 +342,13 @@ AC_SUBST(doc_generate)
|
|||
# Look for lowdown library.
|
||||
PKG_CHECK_MODULES([LOWDOWN], [lowdown >= 0.9.0], [CXXFLAGS="$LOWDOWN_CFLAGS $CXXFLAGS"])
|
||||
|
||||
# Look for toml11, a required dependency.
|
||||
AC_ARG_VAR([TOML11_HEADERS], [include path of toml11 headers])
|
||||
AC_LANG_PUSH(C++)
|
||||
[CXXFLAGS="-I $TOML11_HEADERS $CXXFLAGS"]
|
||||
AC_CHECK_HEADER([toml.hpp], [], [AC_MSG_ERROR([toml11 is not found.])])
|
||||
AC_LANG_POP(C++)
|
||||
|
||||
# Setuid installations.
|
||||
AC_CHECK_FUNCS([setresuid setreuid lchown])
|
||||
|
||||
|
|
7
doc/manual/rl-next/drop-vendored-toml11.md
Normal file
7
doc/manual/rl-next/drop-vendored-toml11.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
synopsis: Stop vendoring toml11
|
||||
cls: 675
|
||||
---
|
||||
|
||||
We don't apply any patches to it, and vendoring it locks users into
|
||||
bugs (it hasn't been updated since its introduction in late 2021).
|
|
@ -199,6 +199,9 @@ gtest = [
|
|||
]
|
||||
deps += gtest
|
||||
|
||||
toml11 = dependency('toml11', version : '>=3.7.0', required : true)
|
||||
deps += toml11
|
||||
|
||||
#
|
||||
# Build-time tools
|
||||
#
|
||||
|
|
12
package.nix
12
package.nix
|
@ -12,6 +12,7 @@
|
|||
boost,
|
||||
brotli,
|
||||
bzip2,
|
||||
cmake,
|
||||
curl,
|
||||
doxygen,
|
||||
editline,
|
||||
|
@ -35,6 +36,7 @@
|
|||
pkg-config,
|
||||
rapidcheck,
|
||||
sqlite,
|
||||
toml11,
|
||||
util-linuxMinimal ? utillinuxMinimal,
|
||||
utillinuxMinimal ? null,
|
||||
xz,
|
||||
|
@ -142,6 +144,9 @@ in stdenv.mkDerivation (finalAttrs: {
|
|||
"-Dsandbox-shell=${lib.getBin busybox-sandbox-shell}/bin/busybox"
|
||||
];
|
||||
|
||||
# We only include CMake so that Meson can locate toml11, which only ships CMake dependency metadata.
|
||||
dontUseCmakeConfigure = true;
|
||||
|
||||
nativeBuildInputs = [
|
||||
bison
|
||||
flex
|
||||
|
@ -164,6 +169,7 @@ in stdenv.mkDerivation (finalAttrs: {
|
|||
++ lib.optionals buildWithMeson [
|
||||
meson
|
||||
ninja
|
||||
cmake
|
||||
];
|
||||
|
||||
buildInputs = [
|
||||
|
@ -178,6 +184,7 @@ in stdenv.mkDerivation (finalAttrs: {
|
|||
boost
|
||||
lowdown
|
||||
libsodium
|
||||
toml11
|
||||
]
|
||||
++ lib.optionals stdenv.hostPlatform.isLinux [ libseccomp busybox-sandbox-shell ]
|
||||
++ lib.optional stdenv.hostPlatform.isx86_64 libcpuid
|
||||
|
@ -239,7 +246,10 @@ in stdenv.mkDerivation (finalAttrs: {
|
|||
++ lib.optionals (finalAttrs.doCheck || internalApiDocs) testConfigureFlags
|
||||
++ lib.optional (!canRunInstalled) "--disable-doc-gen"
|
||||
++ [ (lib.enableFeature internalApiDocs "internal-api-docs") ]
|
||||
++ lib.optional (!forDevShell) "--sysconfdir=/etc";
|
||||
++ lib.optional (!forDevShell) "--sysconfdir=/etc"
|
||||
++ [
|
||||
"TOML11_HEADERS=${lib.getDev toml11}/include"
|
||||
];
|
||||
|
||||
mesonBuildType = lib.optional (buildWithMeson || forDevShell) "debugoptimized";
|
||||
|
||||
|
|
|
@ -131,6 +131,7 @@ libexpr = library(
|
|||
liblixfetchers,
|
||||
boehm,
|
||||
boost,
|
||||
toml11,
|
||||
],
|
||||
# for shared.hh
|
||||
include_directories : [
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
#include "primops.hh"
|
||||
#include "eval-inline.hh"
|
||||
|
||||
#include "../../toml11/toml.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <toml.hpp>
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Toru Niina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
1966
src/toml11/README.md
1966
src/toml11/README.md
File diff suppressed because it is too large
Load diff
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2017 Toru Niina
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// TODO(Qyriad): let's get vendored toml11 out of here.
|
||||
#pragma GCC system_header
|
||||
|
||||
#ifndef TOML_FOR_MODERN_CPP
|
||||
#define TOML_FOR_MODERN_CPP
|
||||
|
||||
#ifndef __cplusplus
|
||||
# error "__cplusplus is not defined"
|
||||
#endif
|
||||
|
||||
#if __cplusplus < 201103L && _MSC_VER < 1900
|
||||
# error "toml11 requires C++11 or later."
|
||||
#endif
|
||||
|
||||
#define TOML11_VERSION_MAJOR 3
|
||||
#define TOML11_VERSION_MINOR 7
|
||||
#define TOML11_VERSION_PATCH 0
|
||||
|
||||
#include "toml/parser.hpp"
|
||||
#include "toml/literal.hpp"
|
||||
#include "toml/serializer.hpp"
|
||||
#include "toml/get.hpp"
|
||||
#include "toml/macros.hpp"
|
||||
|
||||
#endif// TOML_FOR_MODERN_CPP
|
|
@ -1,64 +0,0 @@
|
|||
#ifndef TOML11_COLOR_HPP
|
||||
#define TOML11_COLOR_HPP
|
||||
#include <cstdint>
|
||||
#include <ostream>
|
||||
|
||||
#ifdef TOML11_COLORIZE_ERROR_MESSAGE
|
||||
#define TOML11_ERROR_MESSAGE_COLORIZED true
|
||||
#else
|
||||
#define TOML11_ERROR_MESSAGE_COLORIZED false
|
||||
#endif
|
||||
|
||||
namespace toml
|
||||
{
|
||||
|
||||
// put ANSI escape sequence to ostream
|
||||
namespace color_ansi
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
inline int colorize_index()
|
||||
{
|
||||
static const int index = std::ios_base::xalloc();
|
||||
return index;
|
||||
}
|
||||
} // detail
|
||||
|
||||
inline std::ostream& colorize(std::ostream& os)
|
||||
{
|
||||
// by default, it is zero.
|
||||
os.iword(detail::colorize_index()) = 1;
|
||||
return os;
|
||||
}
|
||||
inline std::ostream& nocolorize(std::ostream& os)
|
||||
{
|
||||
os.iword(detail::colorize_index()) = 0;
|
||||
return os;
|
||||
}
|
||||
inline std::ostream& reset (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[00m";} return os;}
|
||||
inline std::ostream& bold (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[01m";} return os;}
|
||||
inline std::ostream& grey (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[30m";} return os;}
|
||||
inline std::ostream& red (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[31m";} return os;}
|
||||
inline std::ostream& green (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[32m";} return os;}
|
||||
inline std::ostream& yellow (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[33m";} return os;}
|
||||
inline std::ostream& blue (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[34m";} return os;}
|
||||
inline std::ostream& magenta(std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[35m";} return os;}
|
||||
inline std::ostream& cyan (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[36m";} return os;}
|
||||
inline std::ostream& white (std::ostream& os)
|
||||
{if(os.iword(detail::colorize_index()) == 1) {os << "\033[37m";} return os;}
|
||||
} // color_ansi
|
||||
|
||||
// ANSI escape sequence is the only and default colorization method currently
|
||||
namespace color = color_ansi;
|
||||
|
||||
} // toml
|
||||
#endif// TOML11_COLOR_HPP
|
|
@ -1,306 +0,0 @@
|
|||
// Copyright Toru Niina 2017.
|
||||
// Distributed under the MIT License.
|
||||
#ifndef TOML11_COMBINATOR_HPP
|
||||
#define TOML11_COMBINATOR_HPP
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
#include "region.hpp"
|
||||
#include "result.hpp"
|
||||
#include "traits.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
// they scans characters and returns region if it matches to the condition.
|
||||
// when they fail, it does not change the location.
|
||||
// in lexer.hpp, these are used.
|
||||
|
||||
namespace toml
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
// to output character as an error message.
|
||||
inline std::string show_char(const char c)
|
||||
{
|
||||
// It suppresses an error that occurs only in Debug mode of MSVC++ on Windows.
|
||||
// I'm not completely sure but they check the value of char to be in the
|
||||
// range [0, 256) and some of the COMPLETELY VALID utf-8 character sometimes
|
||||
// has negative value (if char has sign). So here it re-interprets c as
|
||||
// unsigned char through pointer. In general, converting pointer to a
|
||||
// pointer that has different type cause UB, but `(signed|unsigned)?char`
|
||||
// are one of the exceptions. Converting pointer only to char and std::byte
|
||||
// (c++17) are valid.
|
||||
if(std::isgraph(*reinterpret_cast<unsigned char const*>(std::addressof(c))))
|
||||
{
|
||||
return std::string(1, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::array<char, 5> buf;
|
||||
buf.fill('\0');
|
||||
const auto r = std::snprintf(
|
||||
buf.data(), buf.size(), "0x%02x", static_cast<int>(c) & 0xFF);
|
||||
(void) r; // Unused variable warning
|
||||
assert(r == static_cast<int>(buf.size()) - 1);
|
||||
return std::string(buf.data());
|
||||
}
|
||||
}
|
||||
|
||||
template<char C>
|
||||
struct character
|
||||
{
|
||||
static constexpr char target = C;
|
||||
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
if(loc.iter() == loc.end()) {return none();}
|
||||
const auto first = loc.iter();
|
||||
|
||||
const char c = *(loc.iter());
|
||||
if(c != target)
|
||||
{
|
||||
return none();
|
||||
}
|
||||
loc.advance(); // update location
|
||||
|
||||
return ok(region(loc, first, loc.iter()));
|
||||
}
|
||||
};
|
||||
template<char C>
|
||||
constexpr char character<C>::target;
|
||||
|
||||
// closed interval [Low, Up]. both Low and Up are included.
|
||||
template<char Low, char Up>
|
||||
struct in_range
|
||||
{
|
||||
// assuming ascii part of UTF-8...
|
||||
static_assert(Low <= Up, "lower bound should be less than upper bound.");
|
||||
|
||||
static constexpr char upper = Up;
|
||||
static constexpr char lower = Low;
|
||||
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
if(loc.iter() == loc.end()) {return none();}
|
||||
const auto first = loc.iter();
|
||||
|
||||
const char c = *(loc.iter());
|
||||
if(c < lower || upper < c)
|
||||
{
|
||||
return none();
|
||||
}
|
||||
|
||||
loc.advance();
|
||||
return ok(region(loc, first, loc.iter()));
|
||||
}
|
||||
};
|
||||
template<char L, char U> constexpr char in_range<L, U>::upper;
|
||||
template<char L, char U> constexpr char in_range<L, U>::lower;
|
||||
|
||||
// keep iterator if `Combinator` matches. otherwise, increment `iter` by 1 char.
|
||||
// for detecting invalid characters, like control sequences in toml string.
|
||||
template<typename Combinator>
|
||||
struct exclude
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
if(loc.iter() == loc.end()) {return none();}
|
||||
auto first = loc.iter();
|
||||
|
||||
auto rslt = Combinator::invoke(loc);
|
||||
if(rslt.is_ok())
|
||||
{
|
||||
loc.reset(first);
|
||||
return none();
|
||||
}
|
||||
loc.reset(std::next(first)); // XXX maybe loc.advance() is okay but...
|
||||
return ok(region(loc, first, loc.iter()));
|
||||
}
|
||||
};
|
||||
|
||||
// increment `iter`, if matches. otherwise, just return empty string.
|
||||
template<typename Combinator>
|
||||
struct maybe
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
const auto rslt = Combinator::invoke(loc);
|
||||
if(rslt.is_ok())
|
||||
{
|
||||
return rslt;
|
||||
}
|
||||
return ok(region(loc));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ... Ts>
|
||||
struct sequence;
|
||||
|
||||
template<typename Head, typename ... Tail>
|
||||
struct sequence<Head, Tail...>
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
const auto first = loc.iter();
|
||||
auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return none();
|
||||
}
|
||||
return sequence<Tail...>::invoke(loc, std::move(rslt.unwrap()), first);
|
||||
}
|
||||
|
||||
// called from the above function only, recursively.
|
||||
template<typename Iterator>
|
||||
static result<region, none_t>
|
||||
invoke(location& loc, region reg, Iterator first)
|
||||
{
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return none();
|
||||
}
|
||||
reg += rslt.unwrap(); // concat regions
|
||||
return sequence<Tail...>::invoke(loc, std::move(reg), first);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Head>
|
||||
struct sequence<Head>
|
||||
{
|
||||
// would be called from sequence<T ...>::invoke only.
|
||||
template<typename Iterator>
|
||||
static result<region, none_t>
|
||||
invoke(location& loc, region reg, Iterator first)
|
||||
{
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return none();
|
||||
}
|
||||
reg += rslt.unwrap(); // concat regions
|
||||
return ok(reg);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename ... Ts>
|
||||
struct either;
|
||||
|
||||
template<typename Head, typename ... Tail>
|
||||
struct either<Head, Tail...>
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
const auto rslt = Head::invoke(loc);
|
||||
if(rslt.is_ok()) {return rslt;}
|
||||
return either<Tail...>::invoke(loc);
|
||||
}
|
||||
};
|
||||
template<typename Head>
|
||||
struct either<Head>
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
return Head::invoke(loc);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, typename N>
|
||||
struct repeat;
|
||||
|
||||
template<std::size_t N> struct exactly{};
|
||||
template<std::size_t N> struct at_least{};
|
||||
struct unlimited{};
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
struct repeat<T, exactly<N>>
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
region retval(loc);
|
||||
const auto first = loc.iter();
|
||||
for(std::size_t i=0; i<N; ++i)
|
||||
{
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return none();
|
||||
}
|
||||
retval += rslt.unwrap();
|
||||
}
|
||||
return ok(std::move(retval));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
struct repeat<T, at_least<N>>
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
region retval(loc);
|
||||
|
||||
const auto first = loc.iter();
|
||||
for(std::size_t i=0; i<N; ++i)
|
||||
{
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
loc.reset(first);
|
||||
return none();
|
||||
}
|
||||
retval += rslt.unwrap();
|
||||
}
|
||||
while(true)
|
||||
{
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
return ok(std::move(retval));
|
||||
}
|
||||
retval += rslt.unwrap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct repeat<T, unlimited>
|
||||
{
|
||||
static result<region, none_t>
|
||||
invoke(location& loc)
|
||||
{
|
||||
region retval(loc);
|
||||
while(true)
|
||||
{
|
||||
auto rslt = T::invoke(loc);
|
||||
if(rslt.is_err())
|
||||
{
|
||||
return ok(std::move(retval));
|
||||
}
|
||||
retval += rslt.unwrap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // detail
|
||||
} // toml
|
||||
#endif// TOML11_COMBINATOR_HPP
|
|
@ -1,472 +0,0 @@
|
|||
// Copyright Toru Niina 2019.
|
||||
// Distributed under the MIT License.
|
||||
#ifndef TOML11_COMMENTS_HPP
|
||||
#define TOML11_COMMENTS_HPP
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#ifdef TOML11_PRESERVE_COMMENTS_BY_DEFAULT
|
||||
# define TOML11_DEFAULT_COMMENT_STRATEGY ::toml::preserve_comments
|
||||
#else
|
||||
# define TOML11_DEFAULT_COMMENT_STRATEGY ::toml::discard_comments
|
||||
#endif
|
||||
|
||||
// This file provides mainly two classes, `preserve_comments` and `discard_comments`.
|
||||
// Those two are a container that have the same interface as `std::vector<std::string>`
|
||||
// but bahaves in the opposite way. `preserve_comments` is just the same as
|
||||
// `std::vector<std::string>` and each `std::string` corresponds to a comment line.
|
||||
// Conversely, `discard_comments` discards all the strings and ignores everything
|
||||
// assigned in it. `discard_comments` is always empty and you will encounter an
|
||||
// error whenever you access to the element.
|
||||
namespace toml
|
||||
{
|
||||
struct discard_comments; // forward decl
|
||||
|
||||
// use it in the following way
|
||||
//
|
||||
// const toml::basic_value<toml::preserve_comments> data =
|
||||
// toml::parse<toml::preserve_comments>("example.toml");
|
||||
//
|
||||
// the interface is almost the same as std::vector<std::string>.
|
||||
struct preserve_comments
|
||||
{
|
||||
// `container_type` is not provided in discard_comments.
|
||||
// do not use this inner-type in a generic code.
|
||||
using container_type = std::vector<std::string>;
|
||||
|
||||
using size_type = container_type::size_type;
|
||||
using difference_type = container_type::difference_type;
|
||||
using value_type = container_type::value_type;
|
||||
using reference = container_type::reference;
|
||||
using const_reference = container_type::const_reference;
|
||||
using pointer = container_type::pointer;
|
||||
using const_pointer = container_type::const_pointer;
|
||||
using iterator = container_type::iterator;
|
||||
using const_iterator = container_type::const_iterator;
|
||||
using reverse_iterator = container_type::reverse_iterator;
|
||||
using const_reverse_iterator = container_type::const_reverse_iterator;
|
||||
|
||||
preserve_comments() = default;
|
||||
~preserve_comments() = default;
|
||||
preserve_comments(preserve_comments const&) = default;
|
||||
preserve_comments(preserve_comments &&) = default;
|
||||
preserve_comments& operator=(preserve_comments const&) = default;
|
||||
preserve_comments& operator=(preserve_comments &&) = default;
|
||||
|
||||
explicit preserve_comments(const std::vector<std::string>& c): comments(c){}
|
||||
explicit preserve_comments(std::vector<std::string>&& c)
|
||||
: comments(std::move(c))
|
||||
{}
|
||||
preserve_comments& operator=(const std::vector<std::string>& c)
|
||||
{
|
||||
comments = c;
|
||||
return *this;
|
||||
}
|
||||
preserve_comments& operator=(std::vector<std::string>&& c)
|
||||
{
|
||||
comments = std::move(c);
|
||||
return *this;
|
||||
}
|
||||
|
||||
explicit preserve_comments(const discard_comments&) {}
|
||||
|
||||
explicit preserve_comments(size_type n): comments(n) {}
|
||||
preserve_comments(size_type n, const std::string& x): comments(n, x) {}
|
||||
preserve_comments(std::initializer_list<std::string> x): comments(x) {}
|
||||
template<typename InputIterator>
|
||||
preserve_comments(InputIterator first, InputIterator last)
|
||||
: comments(first, last)
|
||||
{}
|
||||
|
||||
template<typename InputIterator>
|
||||
void assign(InputIterator first, InputIterator last) {comments.assign(first, last);}
|
||||
void assign(std::initializer_list<std::string> ini) {comments.assign(ini);}
|
||||
void assign(size_type n, const std::string& val) {comments.assign(n, val);}
|
||||
|
||||
// Related to the issue #97.
|
||||
//
|
||||
// It is known that `std::vector::insert` and `std::vector::erase` in
|
||||
// the standard library implementation included in GCC 4.8.5 takes
|
||||
// `std::vector::iterator` instead of `std::vector::const_iterator`.
|
||||
// Because of the const-correctness, we cannot convert a `const_iterator` to
|
||||
// an `iterator`. It causes compilation error in GCC 4.8.5.
|
||||
#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) && !defined(__clang__)
|
||||
# if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) <= 40805
|
||||
# define TOML11_WORKAROUND_GCC_4_8_X_STANDARD_LIBRARY_IMPLEMENTATION
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifdef TOML11_WORKAROUND_GCC_4_8_X_STANDARD_LIBRARY_IMPLEMENTATION
|
||||
iterator insert(iterator p, const std::string& x)
|
||||
{
|
||||
return comments.insert(p, x);
|
||||
}
|
||||
iterator insert(iterator p, std::string&& x)
|
||||
{
|
||||
return comments.insert(p, std::move(x));
|
||||
}
|
||||
void insert(iterator p, size_type n, const std::string& x)
|
||||
{
|
||||
return comments.insert(p, n, x);
|
||||
}
|
||||
template<typename InputIterator>
|
||||
void insert(iterator p, InputIterator first, InputIterator last)
|
||||
{
|
||||
return comments.insert(p, first, last);
|
||||
}
|
||||
void insert(iterator p, std::initializer_list<std::string> ini)
|
||||
{
|
||||
return comments.insert(p, ini);
|
||||
}
|
||||
|
||||
template<typename ... Ts>
|
||||
iterator emplace(iterator p, Ts&& ... args)
|
||||
{
|
||||
return comments.emplace(p, std::forward<Ts>(args)...);
|
||||
}
|
||||
|
||||
iterator erase(iterator pos) {return comments.erase(pos);}
|
||||
iterator erase(iterator first, iterator last)
|
||||
{
|
||||
return comments.erase(first, last);
|
||||
}
|
||||
#else
|
||||
iterator insert(const_iterator p, const std::string& x)
|
||||
{
|
||||
return comments.insert(p, x);
|
||||
}
|
||||
iterator insert(const_iterator p, std::string&& x)
|
||||
{
|
||||
return comments.insert(p, std::move(x));
|
||||
}
|
||||
iterator insert(const_iterator p, size_type n, const std::string& x)
|
||||
{
|
||||
return comments.insert(p, n, x);
|
||||
}
|
||||
template<typename InputIterator>
|
||||
iterator insert(const_iterator p, InputIterator first, InputIterator last)
|
||||
{
|
||||
return comments.insert(p, first, last);
|
||||
}
|
||||
iterator insert(const_iterator p, std::initializer_list<std::string> ini)
|
||||
{
|
||||
return comments.insert(p, ini);
|
||||
}
|
||||
|
||||
template<typename ... Ts>
|
||||
iterator emplace(const_iterator p, Ts&& ... args)
|
||||
{
|
||||
return comments.emplace(p, std::forward<Ts>(args)...);
|
||||
}
|
||||
|
||||
iterator erase(const_iterator pos) {return comments.erase(pos);}
|
||||
iterator erase(const_iterator first, const_iterator last)
|
||||
{
|
||||
return comments.erase(first, last);
|
||||
}
|
||||
#endif
|
||||
|
||||
void swap(preserve_comments& other) {comments.swap(other.comments);}
|
||||
|
||||
void push_back(const std::string& v) {comments.push_back(v);}
|
||||
void push_back(std::string&& v) {comments.push_back(std::move(v));}
|
||||
void pop_back() {comments.pop_back();}
|
||||
|
||||
template<typename ... Ts>
|
||||
void emplace_back(Ts&& ... args) {comments.emplace_back(std::forward<Ts>(args)...);}
|
||||
|
||||
void clear() {comments.clear();}
|
||||
|
||||
size_type size() const noexcept {return comments.size();}
|
||||
size_type max_size() const noexcept {return comments.max_size();}
|
||||
size_type capacity() const noexcept {return comments.capacity();}
|
||||
bool empty() const noexcept {return comments.empty();}
|
||||
|
||||
void reserve(size_type n) {comments.reserve(n);}
|
||||
void resize(size_type n) {comments.resize(n);}
|
||||
void resize(size_type n, const std::string& c) {comments.resize(n, c);}
|
||||
void shrink_to_fit() {comments.shrink_to_fit();}
|
||||
|
||||
reference operator[](const size_type n) noexcept {return comments[n];}
|
||||
const_reference operator[](const size_type n) const noexcept {return comments[n];}
|
||||
reference at(const size_type n) {return comments.at(n);}
|
||||
const_reference at(const size_type n) const {return comments.at(n);}
|
||||
reference front() noexcept {return comments.front();}
|
||||
const_reference front() const noexcept {return comments.front();}
|
||||
reference back() noexcept {return comments.back();}
|
||||
const_reference back() const noexcept {return comments.back();}
|
||||
|
||||
pointer data() noexcept {return comments.data();}
|
||||
const_pointer data() const noexcept {return comments.data();}
|
||||
|
||||
iterator begin() noexcept {return comments.begin();}
|
||||
iterator end() noexcept {return comments.end();}
|
||||
const_iterator begin() const noexcept {return comments.begin();}
|
||||
const_iterator end() const noexcept {return comments.end();}
|
||||
const_iterator cbegin() const noexcept {return comments.cbegin();}
|
||||
const_iterator cend() const noexcept {return comments.cend();}
|
||||
|
||||
reverse_iterator rbegin() noexcept {return comments.rbegin();}
|
||||
reverse_iterator rend() noexcept {return comments.rend();}
|
||||
const_reverse_iterator rbegin() const noexcept {return comments.rbegin();}
|
||||
const_reverse_iterator rend() const noexcept {return comments.rend();}
|
||||
const_reverse_iterator crbegin() const noexcept {return comments.crbegin();}
|
||||
const_reverse_iterator crend() const noexcept {return comments.crend();}
|
||||
|
||||
friend bool operator==(const preserve_comments&, const preserve_comments&);
|
||||
friend bool operator!=(const preserve_comments&, const preserve_comments&);
|
||||
friend bool operator< (const preserve_comments&, const preserve_comments&);
|
||||
friend bool operator<=(const preserve_comments&, const preserve_comments&);
|
||||
friend bool operator> (const preserve_comments&, const preserve_comments&);
|
||||
friend bool operator>=(const preserve_comments&, const preserve_comments&);
|
||||
|
||||
friend void swap(preserve_comments&, std::vector<std::string>&);
|
||||
friend void swap(std::vector<std::string>&, preserve_comments&);
|
||||
|
||||
private:
|
||||
|
||||
container_type comments;
|
||||
};
|
||||
|
||||
inline bool operator==(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments == rhs.comments;}
|
||||
inline bool operator!=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments != rhs.comments;}
|
||||
inline bool operator< (const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments < rhs.comments;}
|
||||
inline bool operator<=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments <= rhs.comments;}
|
||||
inline bool operator> (const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments > rhs.comments;}
|
||||
inline bool operator>=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments >= rhs.comments;}
|
||||
|
||||
inline void swap(preserve_comments& lhs, preserve_comments& rhs)
|
||||
{
|
||||
lhs.swap(rhs);
|
||||
return;
|
||||
}
|
||||
inline void swap(preserve_comments& lhs, std::vector<std::string>& rhs)
|
||||
{
|
||||
lhs.comments.swap(rhs);
|
||||
return;
|
||||
}
|
||||
inline void swap(std::vector<std::string>& lhs, preserve_comments& rhs)
|
||||
{
|
||||
lhs.swap(rhs.comments);
|
||||
return;
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os, const preserve_comments& com)
|
||||
{
|
||||
for(const auto& c : com)
|
||||
{
|
||||
os << '#' << c << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
// To provide the same interface with `preserve_comments`, `discard_comments`
|
||||
// should have an iterator. But it does not contain anything, so we need to
|
||||
// add an iterator that points nothing.
|
||||
//
|
||||
// It always points null, so DO NOT unwrap this iterator. It always crashes
|
||||
// your program.
|
||||
template<typename T, bool is_const>
|
||||
struct empty_iterator
|
||||
{
|
||||
using value_type = T;
|
||||
using reference_type = typename std::conditional<is_const, T const&, T&>::type;
|
||||
using pointer_type = typename std::conditional<is_const, T const*, T*>::type;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
|
||||
empty_iterator() = default;
|
||||
~empty_iterator() = default;
|
||||
empty_iterator(empty_iterator const&) = default;
|
||||
empty_iterator(empty_iterator &&) = default;
|
||||
empty_iterator& operator=(empty_iterator const&) = default;
|
||||
empty_iterator& operator=(empty_iterator &&) = default;
|
||||
|
||||
// DO NOT call these operators.
|
||||
reference_type operator*() const noexcept {std::terminate();}
|
||||
pointer_type operator->() const noexcept {return nullptr;}
|
||||
reference_type operator[](difference_type) const noexcept {return this->operator*();}
|
||||
|
||||
// These operators do nothing.
|
||||
empty_iterator& operator++() noexcept {return *this;}
|
||||
empty_iterator operator++(int) noexcept {return *this;}
|
||||
empty_iterator& operator--() noexcept {return *this;}
|
||||
empty_iterator operator--(int) noexcept {return *this;}
|
||||
|
||||
empty_iterator& operator+=(difference_type) noexcept {return *this;}
|
||||
empty_iterator& operator-=(difference_type) noexcept {return *this;}
|
||||
|
||||
empty_iterator operator+(difference_type) const noexcept {return *this;}
|
||||
empty_iterator operator-(difference_type) const noexcept {return *this;}
|
||||
};
|
||||
|
||||
template<typename T, bool C>
|
||||
bool operator==(const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return true;}
|
||||
template<typename T, bool C>
|
||||
bool operator!=(const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return false;}
|
||||
template<typename T, bool C>
|
||||
bool operator< (const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return false;}
|
||||
template<typename T, bool C>
|
||||
bool operator<=(const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return true;}
|
||||
template<typename T, bool C>
|
||||
bool operator> (const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return false;}
|
||||
template<typename T, bool C>
|
||||
bool operator>=(const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return true;}
|
||||
|
||||
template<typename T, bool C>
|
||||
typename empty_iterator<T, C>::difference_type
|
||||
operator-(const empty_iterator<T, C>&, const empty_iterator<T, C>&) noexcept {return 0;}
|
||||
|
||||
template<typename T, bool C>
|
||||
empty_iterator<T, C>
|
||||
operator+(typename empty_iterator<T, C>::difference_type, const empty_iterator<T, C>& rhs) noexcept {return rhs;}
|
||||
template<typename T, bool C>
|
||||
empty_iterator<T, C>
|
||||
operator+(const empty_iterator<T, C>& lhs, typename empty_iterator<T, C>::difference_type) noexcept {return lhs;}
|
||||
|
||||
} // detail
|
||||
|
||||
// The default comment type. It discards all the comments. It requires only one
|
||||
// byte to contain, so the memory footprint is smaller than preserve_comments.
|
||||
//
|
||||
// It just ignores `push_back`, `insert`, `erase`, and any other modifications.
|
||||
// IT always returns size() == 0, the iterator taken by `begin()` is always the
|
||||
// same as that of `end()`, and accessing through `operator[]` or iterators
|
||||
// always causes a segmentation fault. DO NOT access to the element of this.
|
||||
//
|
||||
// Why this is chose as the default type is because the last version (2.x.y)
|
||||
// does not contain any comments in a value. To minimize the impact on the
|
||||
// efficiency, this is chosen as a default.
|
||||
//
|
||||
// To reduce the memory footprint, later we can try empty base optimization (EBO).
|
||||
struct discard_comments
|
||||
{
|
||||
using size_type = std::size_t;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = std::string;
|
||||
using reference = std::string&;
|
||||
using const_reference = std::string const&;
|
||||
using pointer = std::string*;
|
||||
using const_pointer = std::string const*;
|
||||
using iterator = detail::empty_iterator<std::string, false>;
|
||||
using const_iterator = detail::empty_iterator<std::string, true>;
|
||||
using reverse_iterator = detail::empty_iterator<std::string, false>;
|
||||
using const_reverse_iterator = detail::empty_iterator<std::string, true>;
|
||||
|
||||
discard_comments() = default;
|
||||
~discard_comments() = default;
|
||||
discard_comments(discard_comments const&) = default;
|
||||
discard_comments(discard_comments &&) = default;
|
||||
discard_comments& operator=(discard_comments const&) = default;
|
||||
discard_comments& operator=(discard_comments &&) = default;
|
||||
|
||||
explicit discard_comments(const std::vector<std::string>&) noexcept {}
|
||||
explicit discard_comments(std::vector<std::string>&&) noexcept {}
|
||||
discard_comments& operator=(const std::vector<std::string>&) noexcept {return *this;}
|
||||
discard_comments& operator=(std::vector<std::string>&&) noexcept {return *this;}
|
||||
|
||||
explicit discard_comments(const preserve_comments&) noexcept {}
|
||||
|
||||
explicit discard_comments(size_type) noexcept {}
|
||||
discard_comments(size_type, const std::string&) noexcept {}
|
||||
discard_comments(std::initializer_list<std::string>) noexcept {}
|
||||
template<typename InputIterator>
|
||||
discard_comments(InputIterator, InputIterator) noexcept {}
|
||||
|
||||
template<typename InputIterator>
|
||||
void assign(InputIterator, InputIterator) noexcept {}
|
||||
void assign(std::initializer_list<std::string>) noexcept {}
|
||||
void assign(size_type, const std::string&) noexcept {}
|
||||
|
||||
iterator insert(const_iterator, const std::string&) {return iterator{};}
|
||||
iterator insert(const_iterator, std::string&&) {return iterator{};}
|
||||
iterator insert(const_iterator, size_type, const std::string&) {return iterator{};}
|
||||
template<typename InputIterator>
|
||||
iterator insert(const_iterator, InputIterator, InputIterator) {return iterator{};}
|
||||
iterator insert(const_iterator, std::initializer_list<std::string>) {return iterator{};}
|
||||
|
||||
template<typename ... Ts>
|
||||
iterator emplace(const_iterator, Ts&& ...) {return iterator{};}
|
||||
iterator erase(const_iterator) {return iterator{};}
|
||||
iterator erase(const_iterator, const_iterator) {return iterator{};}
|
||||
|
||||
void swap(discard_comments&) {return;}
|
||||
|
||||
void push_back(const std::string&) {return;}
|
||||
void push_back(std::string&& ) {return;}
|
||||
void pop_back() {return;}
|
||||
|
||||
template<typename ... Ts>
|
||||
void emplace_back(Ts&& ...) {return;}
|
||||
|
||||
void clear() {return;}
|
||||
|
||||
size_type size() const noexcept {return 0;}
|
||||
size_type max_size() const noexcept {return 0;}
|
||||
size_type capacity() const noexcept {return 0;}
|
||||
bool empty() const noexcept {return true;}
|
||||
|
||||
void reserve(size_type) {return;}
|
||||
void resize(size_type) {return;}
|
||||
void resize(size_type, const std::string&) {return;}
|
||||
void shrink_to_fit() {return;}
|
||||
|
||||
// DO NOT access to the element of this container. This container is always
|
||||
// empty, so accessing through operator[], front/back, data causes address
|
||||
// error.
|
||||
|
||||
reference operator[](const size_type) noexcept {return *data();}
|
||||
const_reference operator[](const size_type) const noexcept {return *data();}
|
||||
reference at(const size_type) {throw std::out_of_range("toml::discard_comment is always empty.");}
|
||||
const_reference at(const size_type) const {throw std::out_of_range("toml::discard_comment is always empty.");}
|
||||
reference front() noexcept {return *data();}
|
||||
const_reference front() const noexcept {return *data();}
|
||||
reference back() noexcept {return *data();}
|
||||
const_reference back() const noexcept {return *data();}
|
||||
|
||||
pointer data() noexcept {return nullptr;}
|
||||
const_pointer data() const noexcept {return nullptr;}
|
||||
|
||||
iterator begin() noexcept {return iterator{};}
|
||||
iterator end() noexcept {return iterator{};}
|
||||
const_iterator begin() const noexcept {return const_iterator{};}
|
||||
const_iterator end() const noexcept {return const_iterator{};}
|
||||
const_iterator cbegin() const noexcept {return const_iterator{};}
|
||||
const_iterator cend() const noexcept {return const_iterator{};}
|
||||
|
||||
reverse_iterator rbegin() noexcept {return iterator{};}
|
||||
reverse_iterator rend() noexcept {return iterator{};}
|
||||
const_reverse_iterator rbegin() const noexcept {return const_iterator{};}
|
||||
const_reverse_iterator rend() const noexcept {return const_iterator{};}
|
||||
const_reverse_iterator crbegin() const noexcept {return const_iterator{};}
|
||||
const_reverse_iterator crend() const noexcept {return const_iterator{};}
|
||||
};
|
||||
|
||||
inline bool operator==(const discard_comments&, const discard_comments&) noexcept {return true;}
|
||||
inline bool operator!=(const discard_comments&, const discard_comments&) noexcept {return false;}
|
||||
inline bool operator< (const discard_comments&, const discard_comments&) noexcept {return false;}
|
||||
inline bool operator<=(const discard_comments&, const discard_comments&) noexcept {return true;}
|
||||
inline bool operator> (const discard_comments&, const discard_comments&) noexcept {return false;}
|
||||
inline bool operator>=(const discard_comments&, const discard_comments&) noexcept {return true;}
|
||||
|
||||
inline void swap(const discard_comments&, const discard_comments&) noexcept {return;}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os, const discard_comments&)
|
||||
{
|
||||
return os;
|
||||
}
|
||||
|
||||
} // toml11
|
||||
#endif// TOML11_COMMENTS_HPP
|
|
@ -1,631 +0,0 @@
|
|||
// Copyright Toru Niina 2017.
|
||||
// Distributed under the MIT License.
|
||||
#ifndef TOML11_DATETIME_HPP
|
||||
#define TOML11_DATETIME_HPP
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <ostream>
|
||||
#include <tuple>
|
||||
|
||||
namespace toml
|
||||
{
|
||||
|
||||
// To avoid non-threadsafe std::localtime. In C11 (not C++11!), localtime_s is
|
||||
// provided in the absolutely same purpose, but C++11 is actually not compatible
|
||||
// with C11. We need to dispatch the function depending on the OS.
|
||||
namespace detail
|
||||
{
|
||||
// TODO: find more sophisticated way to handle this
|
||||
#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1) || defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_POSIX_SOURCE)
|
||||
inline std::tm localtime_s(const std::time_t* src)
|
||||
{
|
||||
std::tm dst;
|
||||
const auto result = ::localtime_r(src, &dst);
|
||||
if (!result) { throw std::runtime_error("localtime_r failed."); }
|
||||
return dst;
|
||||
}
|
||||
inline std::tm gmtime_s(const std::time_t* src)
|
||||
{
|
||||
std::tm dst;
|
||||
const auto result = ::gmtime_r(src, &dst);
|
||||
if (!result) { throw std::runtime_error("gmtime_r failed."); }
|
||||
return dst;
|
||||
}
|
||||
#elif defined(_MSC_VER)
|
||||
inline std::tm localtime_s(const std::time_t* src)
|
||||
{
|
||||
std::tm dst;
|
||||
const auto result = ::localtime_s(&dst, src);
|
||||
if (result) { throw std::runtime_error("localtime_s failed."); }
|
||||
return dst;
|
||||
}
|
||||
inline std::tm gmtime_s(const std::time_t* src)
|
||||
{
|
||||
std::tm dst;
|
||||
const auto result = ::gmtime_s(&dst, src);
|
||||
if (result) { throw std::runtime_error("gmtime_s failed."); }
|
||||
return dst;
|
||||
}
|
||||
#else // fallback. not threadsafe
|
||||
inline std::tm localtime_s(const std::time_t* src)
|
||||
{
|
||||
const auto result = std::localtime(src);
|
||||
if (!result) { throw std::runtime_error("localtime failed."); }
|
||||
return *result;
|
||||
}
|
||||
inline std::tm gmtime_s(const std::time_t* src)
|
||||
{
|
||||
const auto result = std::gmtime(src);
|
||||
if (!result) { throw std::runtime_error("gmtime failed."); }
|
||||
return *result;
|
||||
}
|
||||
#endif
|
||||
} // detail
|
||||
|
||||
enum class month_t : std::uint8_t
|
||||
{
|
||||
Jan = 0,
|
||||
Feb = 1,
|
||||
Mar = 2,
|
||||
Apr = 3,
|
||||
May = 4,
|
||||
Jun = 5,
|
||||
Jul = 6,
|
||||
Aug = 7,
|
||||
Sep = 8,
|
||||
Oct = 9,
|
||||
Nov = 10,
|
||||
Dec = 11
|
||||
};
|
||||
|
||||
struct local_date
|
||||
{
|
||||
std::int16_t year; // A.D. (like, 2018)
|
||||
std::uint8_t month; // [0, 11]
|
||||
std::uint8_t day; // [1, 31]
|
||||
|
||||
local_date(int y, month_t m, int d)
|
||||
: year (static_cast<std::int16_t>(y)),
|
||||
month(static_cast<std::uint8_t>(m)),
|
||||
day (static_cast<std::uint8_t>(d))
|
||||
{}
|
||||
|
||||
explicit local_date(const std::tm& t)
|
||||
: year (static_cast<std::int16_t>(t.tm_year + 1900)),
|
||||
month(static_cast<std::uint8_t>(t.tm_mon)),
|
||||
day (static_cast<std::uint8_t>(t.tm_mday))
|
||||
{}
|
||||
|
||||
explicit local_date(const std::chrono::system_clock::time_point& tp)
|
||||
{
|
||||
const auto t = std::chrono::system_clock::to_time_t(tp);
|
||||
const auto time = detail::localtime_s(&t);
|
||||
*this = local_date(time);
|
||||
}
|
||||
|
||||
explicit local_date(const std::time_t t)
|
||||
: local_date(std::chrono::system_clock::from_time_t(t))
|
||||
{}
|
||||
|
||||
operator std::chrono::system_clock::time_point() const
|
||||
{
|
||||
// std::mktime returns date as local time zone. no conversion needed
|
||||
std::tm t;
|
||||
t.tm_sec = 0;
|
||||
t.tm_min = 0;
|
||||
t.tm_hour = 0;
|
||||
t.tm_mday = static_cast<int>(this->day);
|
||||
t.tm_mon = static_cast<int>(this->month);
|
||||
t.tm_year = static_cast<int>(this->year) - 1900;
|
||||
t.tm_wday = 0; // the value will be ignored
|
||||
t.tm_yday = 0; // the value will be ignored
|
||||
t.tm_isdst = -1;
|
||||
return std::chrono::system_clock::from_time_t(std::mktime(&t));
|
||||
}
|
||||
|
||||
operator std::time_t() const
|
||||
{
|
||||
return std::chrono::system_clock::to_time_t(
|
||||
std::chrono::system_clock::time_point(*this));
|
||||
}
|
||||
|
||||
local_date() = default;
|
||||
~local_date() = default;
|
||||
local_date(local_date const&) = default;
|
||||
local_date(local_date&&) = default;
|
||||
local_date& operator=(local_date const&) = default;
|
||||
local_date& operator=(local_date&&) = default;
|
||||
};
|
||||
|
||||
inline bool operator==(const local_date& lhs, const local_date& rhs)
|
||||
{
|
||||
return std::make_tuple(lhs.year, lhs.month, lhs.day) ==
|
||||
std::make_tuple(rhs.year, rhs.month, rhs.day);
|
||||
}
|
||||
inline bool operator!=(const local_date& lhs, const local_date& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
inline bool operator< (const local_date& lhs, const local_date& rhs)
|
||||
{
|
||||
return std::make_tuple(lhs.year, lhs.month, lhs.day) <
|
||||
std::make_tuple(rhs.year, rhs.month, rhs.day);
|
||||
}
|
||||
inline bool operator<=(const local_date& lhs, const local_date& rhs)
|
||||
{
|
||||
return (lhs < rhs) || (lhs == rhs);
|
||||
}
|
||||
inline bool operator> (const local_date& lhs, const local_date& rhs)
|
||||
{
|
||||
return !(lhs <= rhs);
|
||||
}
|
||||
inline bool operator>=(const local_date& lhs, const local_date& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
template<typename charT, typename traits>
|
||||
std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os, const local_date& date)
|
||||
{
|
||||
os << std::setfill('0') << std::setw(4) << static_cast<int>(date.year ) << '-';
|
||||
os << std::setfill('0') << std::setw(2) << static_cast<int>(date.month) + 1 << '-';
|
||||
os << std::setfill('0') << std::setw(2) << static_cast<int>(date.day ) ;
|
||||
return os;
|
||||
}
|
||||
|
||||
struct local_time
|
||||
{
|
||||
std::uint8_t hour; // [0, 23]
|
||||
std::uint8_t minute; // [0, 59]
|
||||
std::uint8_t second; // [0, 60]
|
||||
std::uint16_t millisecond; // [0, 999]
|
||||
std::uint16_t microsecond; // [0, 999]
|
||||
std::uint16_t nanosecond; // [0, 999]
|
||||
|
||||
local_time(int h, int m, int s,
|
||||
int ms = 0, int us = 0, int ns = 0)
|
||||
: hour (static_cast<std::uint8_t>(h)),
|
||||
minute(static_cast<std::uint8_t>(m)),
|
||||
second(static_cast<std::uint8_t>(s)),
|
||||
millisecond(static_cast<std::uint16_t>(ms)),
|
||||
microsecond(static_cast<std::uint16_t>(us)),
|
||||
nanosecond (static_cast<std::uint16_t>(ns))
|
||||
{}
|
||||
|
||||
explicit local_time(const std::tm& t)
|
||||
: hour (static_cast<std::uint8_t>(t.tm_hour)),
|
||||
minute(static_cast<std::uint8_t>(t.tm_min)),
|
||||
second(static_cast<std::uint8_t>(t.tm_sec)),
|
||||
millisecond(0), microsecond(0), nanosecond(0)
|
||||
{}
|
||||
|
||||
template<typename Rep, typename Period>
|
||||
explicit local_time(const std::chrono::duration<Rep, Period>& t)
|
||||
{
|
||||
const auto h = std::chrono::duration_cast<std::chrono::hours>(t);
|
||||
this->hour = static_cast<std::uint8_t>(h.count());
|
||||
const auto t2 = t - h;
|
||||
const auto m = std::chrono::duration_cast<std::chrono::minutes>(t2);
|
||||
this->minute = static_cast<std::uint8_t>(m.count());
|
||||
const auto t3 = t2 - m;
|
||||
const auto s = std::chrono::duration_cast<std::chrono::seconds>(t3);
|
||||
this->second = static_cast<std::uint8_t>(s.count());
|
||||
const auto t4 = t3 - s;
|
||||
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t4);
|
||||
this->millisecond = static_cast<std::uint16_t>(ms.count());
|
||||
const auto t5 = t4 - ms;
|
||||
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(t5);
|
||||
this->microsecond = static_cast<std::uint16_t>(us.count());
|
||||
const auto t6 = t5 - us;
|
||||
const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(t6);
|
||||
this->nanosecond = static_cast<std::uint16_t>(ns.count());
|
||||
}
|
||||
|
||||
operator std::chrono::nanoseconds() const
|
||||
{
|
||||
return std::chrono::nanoseconds (this->nanosecond) +
|
||||
std::chrono::microseconds(this->microsecond) +
|
||||
std::chrono::milliseconds(this->millisecond) +
|
||||
std::chrono::seconds(this->second) +
|
||||
std::chrono::minutes(this->minute) +
|
||||
std::chrono::hours(this->hour);
|
||||
}
|
||||
|
||||
local_time() = default;
|
||||
~local_time() = default;
|
||||
local_time(local_time const&) = default;
|
||||
local_time(local_time&&) = default;
|
||||
local_time& operator=(local_time const&) = default;
|
||||
local_time& operator=(local_time&&) = default;
|
||||
};
|
||||
|
||||
inline bool operator==(const local_time& lhs, const local_time& rhs)
|
||||
{
|
||||
return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) ==
|
||||
std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond);
|
||||
}
|
||||
inline bool operator!=(const local_time& lhs, const local_time& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
inline bool operator< (const local_time& lhs, const local_time& rhs)
|
||||
{
|
||||
return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) <
|
||||
std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond);
|
||||
}
|
||||
inline bool operator<=(const local_time& lhs, const local_time& rhs)
|
||||
{
|
||||
return (lhs < rhs) || (lhs == rhs);
|
||||
}
|
||||
inline bool operator> (const local_time& lhs, const local_time& rhs)
|
||||
{
|
||||
return !(lhs <= rhs);
|
||||
}
|
||||
inline bool operator>=(const local_time& lhs, con |