diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f26006d87..1cf83c065 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: yapf files: \.py$ exclude: (?=third_party).*(\.py)$ - + - repo: https://github.com/pre-commit/pre-commit-hooks rev: a11d9314b22d8f8c7556443875b731ef05965464 hooks: @@ -76,4 +76,4 @@ repos: entry: bash .pre-commit-hooks/cpplint.hook language: system files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$ - exclude: (?=speechx/speechx/kaldi|speechx/patch|speechx/tools/fstbin|speechx/tools/lmbin).*(\.cpp|\.cc|\.h|\.py)$ \ No newline at end of file + exclude: (?=speechx/speechx/kaldi|speechx/patch|speechx/tools/fstbin|speechx/tools/lmbin|paddlespeech/audio/src/optional).*(\.cpp|\.cc|\.h|\.hpp)$ \ No newline at end of file diff --git a/audio/audio/csrc/pybind/pybind.cpp b/audio/audio/csrc/pybind/pybind.cpp index c278b7ade..53aaa13bf 100644 --- a/audio/audio/csrc/pybind/pybind.cpp +++ b/audio/audio/csrc/pybind/pybind.cpp @@ -1,8 +1,10 @@ #include "pybind/sox/io.h" PYBIND11_MODULE(_paddleaudio, m) { - m.def("get_info_file", &paddleaudio::sox_io::get_info_file, - "Get metadata of audio file."); - m.def("get_info_fileobj", &paddleaudio::sox_io::get_info_fileobj, - "Get metadata of audio in file object."); + m.def("get_info_file", + &paddleaudio::sox_io::get_info_file, + "Get metadata of audio file."); + m.def("get_info_fileobj", + &paddleaudio::sox_io::get_info_fileobj, + "Get metadata of audio in file object."); } \ No newline at end of file diff --git a/audio/audio/csrc/pybind/sox/io.cpp b/audio/audio/csrc/pybind/sox/io.cpp index 57cff93b8..3d753c4cd 100644 --- a/audio/audio/csrc/pybind/sox/io.cpp +++ b/audio/audio/csrc/pybind/sox/io.cpp @@ -8,51 +8,54 @@ namespace sox_io { auto get_info_file(const std::string &path, const std::string &format) -> std::tuple { - SoxFormat sf(sox_open_read(path.data(), - /*signal=*/nullptr, - /*encoding=*/nullptr, - /*filetype=*/format.empty() ? nullptr : format.data())); - - - validate_input_file(sf, path); - - return std::make_tuple( - static_cast(sf->signal.rate), - static_cast(sf->signal.length / sf->signal.channels), - static_cast(sf->signal.channels), - static_cast(sf->encoding.bits_per_sample), - get_encoding(sf->encoding.encoding)); + SoxFormat sf( + sox_open_read(path.data(), + /*signal=*/nullptr, + /*encoding=*/nullptr, + /*filetype=*/format.empty() ? nullptr : format.data())); + + + validate_input_file(sf, path); + + return std::make_tuple( + static_cast(sf->signal.rate), + static_cast(sf->signal.length / sf->signal.channels), + static_cast(sf->signal.channels), + static_cast(sf->encoding.bits_per_sample), + get_encoding(sf->encoding.encoding)); } auto get_info_fileobj(py::object fileobj, const std::string &format) -> std::tuple { - const auto capacity = [&]() { - const auto bufsiz = get_buffer_size(); - const int64_t kDefaultCapacityInBytes = 4096; - return (bufsiz > kDefaultCapacityInBytes) ? bufsiz - : kDefaultCapacityInBytes; - }(); - std::string buffer(capacity, '\0'); - auto *buf = const_cast(buffer.data()); - auto num_read = read_fileobj(&fileobj, capacity, buf); - // If the file is shorter than 256, then libsox cannot read the header. - auto buf_size = (num_read > 256) ? num_read : 256; - - SoxFormat sf(sox_open_mem_read(buf, buf_size, - /*signal=*/nullptr, - /*encoding=*/nullptr, - /*filetype=*/format.empty() ? nullptr : format.data())); - - // In case of streamed data, length can be 0 - validate_input_memfile(sf); - - return std::make_tuple( - static_cast(sf->signal.rate), - static_cast(sf->signal.length / sf->signal.channels), - static_cast(sf->signal.channels), - static_cast(sf->encoding.bits_per_sample), - get_encoding(sf->encoding.encoding)); + const auto capacity = [&]() { + const auto bufsiz = get_buffer_size(); + const int64_t kDefaultCapacityInBytes = 4096; + return (bufsiz > kDefaultCapacityInBytes) ? bufsiz + : kDefaultCapacityInBytes; + }(); + std::string buffer(capacity, '\0'); + auto *buf = const_cast(buffer.data()); + auto num_read = read_fileobj(&fileobj, capacity, buf); + // If the file is shorter than 256, then libsox cannot read the header. + auto buf_size = (num_read > 256) ? num_read : 256; + + SoxFormat sf(sox_open_mem_read( + buf, + buf_size, + /*signal=*/nullptr, + /*encoding=*/nullptr, + /*filetype=*/format.empty() ? nullptr : format.data())); + + // In case of streamed data, length can be 0 + validate_input_memfile(sf); + + return std::make_tuple( + static_cast(sf->signal.rate), + static_cast(sf->signal.length / sf->signal.channels), + static_cast(sf->signal.channels), + static_cast(sf->encoding.bits_per_sample), + get_encoding(sf->encoding.encoding)); } -} // namespace paddleaudio -} // namespace sox_io +} // namespace paddleaudio +} // namespace sox_io diff --git a/audio/audio/csrc/pybind/sox/io.h b/audio/audio/csrc/pybind/sox/io.h index de97c0123..0140fb962 100644 --- a/audio/audio/csrc/pybind/sox/io.h +++ b/audio/audio/csrc/pybind/sox/io.h @@ -12,7 +12,7 @@ auto get_info_file(const std::string &path, const std::string &format) auto get_info_fileobj(py::object fileobj, const std::string &format) -> std::tuple; -} // namespace paddleaudio -} // namespace sox_io +} // namespace paddleaudio +} // namespace sox_io #endif diff --git a/audio/audio/csrc/pybind/sox/utils.cpp b/audio/audio/csrc/pybind/sox/utils.cpp index 03d5580bf..d34d733d1 100644 --- a/audio/audio/csrc/pybind/sox/utils.cpp +++ b/audio/audio/csrc/pybind/sox/utils.cpp @@ -12,86 +12,87 @@ sox_format_t *SoxFormat::operator->() const noexcept { return fd_; } SoxFormat::operator sox_format_t *() const noexcept { return fd_; } void SoxFormat::close() { - if (fd_ != nullptr) { - sox_close(fd_); - fd_ = nullptr; - } + if (fd_ != nullptr) { + sox_close(fd_); + fd_ = nullptr; + } } auto read_fileobj(py::object *fileobj, const uint64_t size, char *buffer) -> uint64_t { - uint64_t num_read = 0; - while (num_read < size) { - auto request = size - num_read; - auto chunk = static_cast( - static_cast(fileobj->attr("read")(request))); - auto chunk_len = chunk.length(); - if (chunk_len == 0) { - break; - } - if (chunk_len > request) { - std::ostringstream message; - message << "Requested up to " << request << " bytes but, " - << "received " << chunk_len << " bytes. " - << "The given object does not confirm to read protocol of file " - "object."; - throw std::runtime_error(message.str()); + uint64_t num_read = 0; + while (num_read < size) { + auto request = size - num_read; + auto chunk = static_cast( + static_cast(fileobj->attr("read")(request))); + auto chunk_len = chunk.length(); + if (chunk_len == 0) { + break; + } + if (chunk_len > request) { + std::ostringstream message; + message + << "Requested up to " << request << " bytes but, " + << "received " << chunk_len << " bytes. " + << "The given object does not confirm to read protocol of file " + "object."; + throw std::runtime_error(message.str()); + } + memcpy(buffer, chunk.data(), chunk_len); + buffer += chunk_len; + num_read += chunk_len; } - memcpy(buffer, chunk.data(), chunk_len); - buffer += chunk_len; - num_read += chunk_len; - } - return num_read; + return num_read; } int64_t get_buffer_size() { return sox_get_globals()->bufsiz; } void validate_input_file(const SoxFormat &sf, const std::string &path) { - if (static_cast(sf) == nullptr) { - throw std::runtime_error("Error loading audio file: failed to open file " + - path); - } - if (sf->encoding.encoding == SOX_ENCODING_UNKNOWN) { - throw std::runtime_error("Error loading audio file: unknown encoding."); - } + if (static_cast(sf) == nullptr) { + throw std::runtime_error( + "Error loading audio file: failed to open file " + path); + } + if (sf->encoding.encoding == SOX_ENCODING_UNKNOWN) { + throw std::runtime_error("Error loading audio file: unknown encoding."); + } } void validate_input_memfile(const SoxFormat &sf) { - return validate_input_file(sf, ""); + return validate_input_file(sf, ""); } std::string get_encoding(sox_encoding_t encoding) { - switch (encoding) { - case SOX_ENCODING_UNKNOWN: - return "UNKNOWN"; - case SOX_ENCODING_SIGN2: - return "PCM_S"; - case SOX_ENCODING_UNSIGNED: - return "PCM_U"; - case SOX_ENCODING_FLOAT: - return "PCM_F"; - case SOX_ENCODING_FLAC: - return "FLAC"; - case SOX_ENCODING_ULAW: - return "ULAW"; - case SOX_ENCODING_ALAW: - return "ALAW"; - case SOX_ENCODING_MP3: - return "MP3"; - case SOX_ENCODING_VORBIS: - return "VORBIS"; - case SOX_ENCODING_AMR_WB: - return "AMR_WB"; - case SOX_ENCODING_AMR_NB: - return "AMR_NB"; - case SOX_ENCODING_OPUS: - return "OPUS"; - case SOX_ENCODING_GSM: - return "GSM"; - default: - return "UNKNOWN"; - } + switch (encoding) { + case SOX_ENCODING_UNKNOWN: + return "UNKNOWN"; + case SOX_ENCODING_SIGN2: + return "PCM_S"; + case SOX_ENCODING_UNSIGNED: + return "PCM_U"; + case SOX_ENCODING_FLOAT: + return "PCM_F"; + case SOX_ENCODING_FLAC: + return "FLAC"; + case SOX_ENCODING_ULAW: + return "ULAW"; + case SOX_ENCODING_ALAW: + return "ALAW"; + case SOX_ENCODING_MP3: + return "MP3"; + case SOX_ENCODING_VORBIS: + return "VORBIS"; + case SOX_ENCODING_AMR_WB: + return "AMR_WB"; + case SOX_ENCODING_AMR_NB: + return "AMR_NB"; + case SOX_ENCODING_OPUS: + return "OPUS"; + case SOX_ENCODING_GSM: + return "GSM"; + default: + return "UNKNOWN"; + } } -} // namespace paddleaudio -} // namespace sox_utils +} // namespace paddleaudio +} // namespace sox_utils diff --git a/audio/audio/csrc/pybind/sox/utils.h b/audio/audio/csrc/pybind/sox/utils.h index efa21fc63..072ac8838 100644 --- a/audio/audio/csrc/pybind/sox/utils.h +++ b/audio/audio/csrc/pybind/sox/utils.h @@ -11,19 +11,19 @@ namespace sox_utils { /// helper class to automatically close sox_format_t* struct SoxFormat { - explicit SoxFormat(sox_format_t *fd) noexcept; - SoxFormat(const SoxFormat &other) = delete; - SoxFormat(SoxFormat &&other) = delete; - SoxFormat &operator=(const SoxFormat &other) = delete; - SoxFormat &operator=(SoxFormat &&other) = delete; - ~SoxFormat(); - sox_format_t *operator->() const noexcept; - operator sox_format_t *() const noexcept; - - void close(); - -private: - sox_format_t *fd_; + explicit SoxFormat(sox_format_t *fd) noexcept; + SoxFormat(const SoxFormat &other) = delete; + SoxFormat(SoxFormat &&other) = delete; + SoxFormat &operator=(const SoxFormat &other) = delete; + SoxFormat &operator=(SoxFormat &&other) = delete; + ~SoxFormat(); + sox_format_t *operator->() const noexcept; + operator sox_format_t *() const noexcept; + + void close(); + + private: + sox_format_t *fd_; }; auto read_fileobj(py::object *fileobj, uint64_t size, char *buffer) -> uint64_t; @@ -36,7 +36,7 @@ void validate_input_memfile(const SoxFormat &sf); std::string get_encoding(sox_encoding_t encoding); -} // namespace paddleaudio -} // namespace sox_utils +} // namespace paddleaudio +} // namespace sox_utils #endif diff --git a/paddlespeech/__init__.py b/paddlespeech/__init__.py index 4b1c0ef3d..b781c4a8e 100644 --- a/paddlespeech/__init__.py +++ b/paddlespeech/__init__.py @@ -14,5 +14,3 @@ import _locale _locale._getdefaultlocale = (lambda *args: ['en_US', 'utf8']) - - diff --git a/paddlespeech/audio/README.md b/paddlespeech/audio/README.md index aeb89ce40..fc1e5942c 100644 --- a/paddlespeech/audio/README.md +++ b/paddlespeech/audio/README.md @@ -28,4 +28,4 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -``` \ No newline at end of file +``` diff --git a/paddlespeech/audio/__init__.py b/paddlespeech/audio/__init__.py index b84927f49..6184c1dd4 100644 --- a/paddlespeech/audio/__init__.py +++ b/paddlespeech/audio/__init__.py @@ -19,4 +19,4 @@ from . import io from . import metric from . import sox_effects from .backends import load -from .backends import save \ No newline at end of file +from .backends import save diff --git a/paddlespeech/audio/_class.py b/paddlespeech/audio/_class.py index ed4c0116e..22cf44a62 100644 --- a/paddlespeech/audio/_class.py +++ b/paddlespeech/audio/_class.py @@ -1,5 +1,6 @@ import types + class _ClassNamespace(types.ModuleType): def __init__(self, name): super(_ClassNamespace, self).__init__('paddlespeech.classes' + name) @@ -11,6 +12,7 @@ class _ClassNamespace(types.ModuleType): raise RuntimeError(f'Class {self.name}.{attr} not registered!') return proxy + class _Classes(types.ModuleType): __file__ = '_classes.py' @@ -43,5 +45,6 @@ class _Classes(types.ModuleType): """ paddlespeech.ops.load_library(path) + # The classes "namespace" -classes = _Classes() \ No newline at end of file +classes = _Classes() diff --git a/paddlespeech/audio/_extension.py b/paddlespeech/audio/_extension.py index b35b81b61..fccba8838 100644 --- a/paddlespeech/audio/_extension.py +++ b/paddlespeech/audio/_extension.py @@ -64,7 +64,8 @@ def _init_ffmpeg(): try: _load_lib("libpaddlleaudio_ffmpeg") except OSError as err: - raise ImportError("FFmpeg libraries are not found. Please install FFmpeg.") from err + raise ImportError( + "FFmpeg libraries are not found. Please install FFmpeg.") from err import paddllespeech._paddlleaudio_ffmpeg # noqa @@ -95,4 +96,4 @@ def _init_extension(): pass -_init_extension() \ No newline at end of file +_init_extension() diff --git a/paddlespeech/audio/_internal/module_utils.py b/paddlespeech/audio/_internal/module_utils.py index cf6764262..4da922d32 100644 --- a/paddlespeech/audio/_internal/module_utils.py +++ b/paddlespeech/audio/_internal/module_utils.py @@ -3,6 +3,7 @@ import warnings from functools import wraps from typing import Optional + def is_module_available(*modules: str) -> bool: r"""Returns if a top-level module with :attr:`name` exists *without** importing it. This is generally safer than try-catch block around a @@ -26,19 +27,21 @@ def requires_module(*modules: str): return func else: - req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}" + req = f"module: {missing[0]}" if len( + missing) == 1 else f"modules: {missing}" def decorator(func): @wraps(func) def wrapped(*args, **kwargs): - raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}") + raise RuntimeError( + f"{func.__module__}.{func.__name__} requires {req}") return wrapped return decorator -def deprecated(direction: str, version: Optional[str] = None): +def deprecated(direction: str, version: Optional[str]=None): """Decorator to add deprecation message Args: direction (str): Migration steps to be given to users. @@ -51,8 +54,7 @@ def deprecated(direction: str, version: Optional[str] = None): message = ( f"{func.__module__}.{func.__name__} has been deprecated " f'and will be removed from {"future" if version is None else version} release. ' - f"{direction}" - ) + f"{direction}") warnings.warn(message, stacklevel=2) return func(*args, **kwargs) @@ -62,7 +64,7 @@ def deprecated(direction: str, version: Optional[str] = None): def is_kaldi_available(): - return is_module_available("paddlespeech"._paddleaudio") and paddlespeech.ops.paddleaudio.is_kaldi_available() + return is_module_available("paddlespeech.audio._paddleaudio") def requires_kaldi(): @@ -76,7 +78,8 @@ def requires_kaldi(): def decorator(func): @wraps(func) def wrapped(*args, **kwargs): - raise RuntimeError(f"{func.__module__}.{func.__name__} requires kaldi") + raise RuntimeError( + f"{func.__module__}.{func.__name__} requires kaldi") return wrapped @@ -91,7 +94,8 @@ def _check_soundfile_importable(): return True except Exception: - warnings.warn("Failed to import soundfile. 'soundfile' backend is not available.") + warnings.warn( + "Failed to import soundfile. 'soundfile' backend is not available.") return False @@ -113,7 +117,8 @@ def requires_soundfile(): def decorator(func): @wraps(func) def wrapped(*args, **kwargs): - raise RuntimeError(f"{func.__module__}.{func.__name__} requires soundfile") + raise RuntimeError( + f"{func.__module__}.{func.__name__} requires soundfile") return wrapped @@ -121,7 +126,7 @@ def requires_soundfile(): def is_sox_available(): - return is_module_available("paddlespeech._paddleaudio") and paddlespeech.ops.paddleaudio.is_sox_available() + return is_module_available("paddlespeech.audio._paddleaudio") def requires_sox(): @@ -135,8 +140,9 @@ def requires_sox(): def decorator(func): @wraps(func) def wrapped(*args, **kwargs): - raise RuntimeError(f"{func.__module__}.{func.__name__} requires sox") + raise RuntimeError( + f"{func.__module__}.{func.__name__} requires sox") return wrapped - return \ No newline at end of file + return diff --git a/paddlespeech/audio/_ops.py b/paddlespeech/audio/_ops.py index 0ef37592d..6bcf25fe4 100644 --- a/paddlespeech/audio/_ops.py +++ b/paddlespeech/audio/_ops.py @@ -1,11 +1,12 @@ import contextlib import ctypes -import sys import os +import sys import types # Query `hasattr` only once. -_SET_GLOBAL_FLAGS = hasattr(sys, 'getdlopenflags') and hasattr(sys, 'setdlopenflags') +_SET_GLOBAL_FLAGS = hasattr(sys, 'getdlopenflags') and hasattr(sys, + 'setdlopenflags') @contextlib.contextmanager @@ -22,7 +23,7 @@ def dl_open_guard(): if _SET_GLOBAL_FLAGS: sys.setdlopenflags(old_flags) - + def resolve_library_path(path: str) -> str: return os.path.realpath(path) @@ -59,4 +60,4 @@ class _Ops(types.ModuleType): # The ops "namespace" -ops = _Ops() \ No newline at end of file +ops = _Ops() diff --git a/paddlespeech/audio/src/optional/optional.hpp b/paddlespeech/audio/src/optional/optional.hpp index 9e5346f8d..bceb41135 100644 --- a/paddlespeech/audio/src/optional/optional.hpp +++ b/paddlespeech/audio/src/optional/optional.hpp @@ -32,64 +32,69 @@ #define TL_OPTIONAL_MSVC2015 #endif -#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ !defined(__clang__)) #define TL_OPTIONAL_GCC49 #endif -#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && \ +#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && \ !defined(__clang__)) #define TL_OPTIONAL_GCC54 #endif -#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && \ +#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && \ !defined(__clang__)) #define TL_OPTIONAL_GCC55 #endif -#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \ !defined(__clang__)) // GCC < 5 doesn't support overloading on const&& for member functions #define TL_OPTIONAL_NO_CONSTRR // GCC < 5 doesn't support some standard C++11 type traits -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ - std::has_trivial_copy_constructor::value -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ + std::has_trivial_copy_constructor::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ + std::has_trivial_copy_assign::value // This one will be different for GCC 5.7 if it's ever supported -#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \ + std::is_trivially_destructible::value -// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector +// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks +// std::vector // for non-copyable types -#elif (defined(__GNUC__) && __GNUC__ < 8 && \ - !defined(__clang__)) +#elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__)) #ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX #define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX namespace tl { - namespace detail { - template - struct is_trivially_copy_constructible : std::is_trivially_copy_constructible{}; +namespace detail { +template +struct is_trivially_copy_constructible + : std::is_trivially_copy_constructible {}; #ifdef _GLIBCXX_VECTOR - template - struct is_trivially_copy_constructible> - : std::is_trivially_copy_constructible{}; -#endif - } +template +struct is_trivially_copy_constructible> + : std::is_trivially_copy_constructible {}; +#endif +} } #endif -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ tl::detail::is_trivially_copy_constructible::value -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ - std::is_trivially_copy_assignable::value -#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ + std::is_trivially_copy_assignable::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \ + std::is_trivially_destructible::value #else -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ - std::is_trivially_copy_constructible::value -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ - std::is_trivially_copy_assignable::value -#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ + std::is_trivially_copy_constructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ + std::is_trivially_copy_assignable::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \ + std::is_trivially_destructible::value #endif #if __cplusplus > 201103L @@ -97,7 +102,7 @@ namespace tl { #endif // constexpr implies const in C++11, not C++14 -#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || \ +#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || \ defined(TL_OPTIONAL_GCC49)) #define TL_OPTIONAL_11_CONSTEXPR #else @@ -112,30 +117,35 @@ class monostate {}; /// A tag type to tell optional to construct its value in-place struct in_place_t { - explicit in_place_t() = default; + explicit in_place_t() = default; }; /// A tag to tell optional to construct its value in-place static constexpr in_place_t in_place{}; #endif -template class optional; +template +class optional; namespace detail { #ifndef TL_TRAITS_MUTEX #define TL_TRAITS_MUTEX // C++14-style aliases for brevity -template using remove_const_t = typename std::remove_const::type; +template +using remove_const_t = typename std::remove_const::type; template using remove_reference_t = typename std::remove_reference::type; -template using decay_t = typename std::decay::type; +template +using decay_t = typename std::decay::type; template using enable_if_t = typename std::enable_if::type; template using conditional_t = typename std::conditional::type; // std::conjunction from C++17 -template struct conjunction : std::true_type {}; -template struct conjunction : B {}; +template +struct conjunction : std::true_type {}; +template +struct conjunction : B {}; template struct conjunction : std::conditional, B>::type {}; @@ -148,56 +158,72 @@ struct conjunction // which results in a hard-error when using it in a noexcept expression // in some cases. This is a check to workaround the common failing case. #ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND -template struct is_pointer_to_non_const_member_func : std::false_type{}; +template +struct is_pointer_to_non_const_member_func : std::false_type {}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func + : std::true_type {}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func + : std::true_type {}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func + : std::true_type {}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func + : std::true_type {}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func + : std::true_type {}; template -struct is_pointer_to_non_const_member_func : std::true_type{}; +struct is_pointer_to_non_const_member_func + : std::true_type {}; -template struct is_const_or_const_ref : std::false_type{}; -template struct is_const_or_const_ref : std::true_type{}; -template struct is_const_or_const_ref : std::true_type{}; +template +struct is_const_or_const_ref : std::false_type {}; +template +struct is_const_or_const_ref : std::true_type {}; +template +struct is_const_or_const_ref : std::true_type {}; #endif // std::invoke from C++17 // https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround -template ::value - && is_const_or_const_ref::value)>, + typename = enable_if_t::value && + is_const_or_const_ref::value)>, #endif - typename = enable_if_t>::value>, - int = 0> + typename = enable_if_t>::value>, + int = 0> constexpr auto invoke(Fn &&f, Args &&... args) noexcept( noexcept(std::mem_fn(f)(std::forward(args)...))) -> decltype(std::mem_fn(f)(std::forward(args)...)) { - return std::mem_fn(f)(std::forward(args)...); + return std::mem_fn(f)(std::forward(args)...); } -template >::value>> constexpr auto invoke(Fn &&f, Args &&... args) noexcept( noexcept(std::forward(f)(std::forward(args)...))) -> decltype(std::forward(f)(std::forward(args)...)) { - return std::forward(f)(std::forward(args)...); + return std::forward(f)(std::forward(args)...); } // std::invoke_result from C++17 -template struct invoke_result_impl; +template +struct invoke_result_impl; template struct invoke_result_impl< - F, decltype(detail::invoke(std::declval(), std::declval()...), void()), + F, + decltype(detail::invoke(std::declval(), std::declval()...), void()), Us...> { - using type = decltype(detail::invoke(std::declval(), std::declval()...)); + using type = + decltype(detail::invoke(std::declval(), std::declval()...)); }; template @@ -208,9 +234,11 @@ using invoke_result_t = typename invoke_result::type; #if defined(_MSC_VER) && _MSC_VER <= 1900 // TODO make a version which works with MSVC 2015 -template struct is_swappable : std::true_type {}; +template +struct is_swappable : std::true_type {}; -template struct is_nothrow_swappable : std::true_type {}; +template +struct is_nothrow_swappable : std::true_type {}; #else // https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept namespace swap_adl_tests { @@ -218,18 +246,23 @@ namespace swap_adl_tests { // signature) struct tag {}; -template tag swap(T &, T &); -template tag swap(T (&a)[N], T (&b)[N]); +template +tag swap(T &, T &); +template +tag swap(T (&a)[N], T (&b)[N]); // helper functions to test if an unqualified swap is possible, and if it // becomes std::swap -template std::false_type can_swap(...) noexcept(false); -template +std::false_type can_swap(...) noexcept(false); +template (), std::declval()))> std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(), std::declval()))); -template std::false_type uses_std(...); +template +std::false_type uses_std(...); template std::is_same(), std::declval())), tag> uses_std(int); @@ -246,7 +279,7 @@ struct is_std_swap_noexcept : is_std_swap_noexcept {}; template struct is_adl_swap_noexcept : std::integral_constant(0))> {}; -} // namespace swap_adl_tests +} // namespace swap_adl_tests template struct is_swappable @@ -281,13 +314,20 @@ struct is_nothrow_swappable #endif // std::void_t from C++17 -template struct voider { using type = void; }; -template using void_t = typename voider::type; +template +struct voider { + using type = void; +}; +template +using void_t = typename voider::type; // Trait for checking if a type is a tl::optional -template struct is_optional_impl : std::false_type {}; -template struct is_optional_impl> : std::true_type {}; -template using is_optional = is_optional_impl>; +template +struct is_optional_impl : std::false_type {}; +template +struct is_optional_impl> : std::true_type {}; +template +using is_optional = is_optional_impl>; // Change void to tl::monostate template @@ -297,7 +337,8 @@ template > using get_map_return = optional>>; // Check if invoking F for some Us returns void -template struct returns_void_impl; +template +struct returns_void_impl; template struct returns_void_impl>, U...> : std::is_void> {}; @@ -357,86 +398,92 @@ using enable_assign_from_other = detail::enable_if_t< // destructible. template ::value> struct optional_storage_base { - TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept - : m_dummy(), m_has_value(false) {} - - template - TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) - : m_value(std::forward(u)...), m_has_value(true) {} - - ~optional_storage_base() { - if (m_has_value) { - m_value.~T(); - m_has_value = false; + TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept + : m_dummy(), + m_has_value(false) {} + + template + TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) + : m_value(std::forward(u)...), m_has_value(true) {} + + ~optional_storage_base() { + if (m_has_value) { + m_value.~T(); + m_has_value = false; + } } - } - struct dummy {}; - union { - dummy m_dummy; - T m_value; - }; + struct dummy {}; + union { + dummy m_dummy; + T m_value; + }; - bool m_has_value; + bool m_has_value; }; // This case is for when T is trivially destructible. -template struct optional_storage_base { - TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept - : m_dummy(), m_has_value(false) {} +template +struct optional_storage_base { + TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept + : m_dummy(), + m_has_value(false) {} - template - TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) - : m_value(std::forward(u)...), m_has_value(true) {} + template + TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) + : m_value(std::forward(u)...), m_has_value(true) {} - // No destructor, so this class is trivially destructible + // No destructor, so this class is trivially destructible - struct dummy {}; - union { - dummy m_dummy; - T m_value; - }; + struct dummy {}; + union { + dummy m_dummy; + T m_value; + }; - bool m_has_value = false; + bool m_has_value = false; }; // This base class provides some handy member functions which can be used in // further derived classes -template struct optional_operations_base : optional_storage_base { - using optional_storage_base::optional_storage_base; - - void hard_reset() noexcept { - get().~T(); - this->m_has_value = false; - } - - template void construct(Args &&... args) noexcept { - new (std::addressof(this->m_value)) T(std::forward(args)...); - this->m_has_value = true; - } - - template void assign(Opt &&rhs) { - if (this->has_value()) { - if (rhs.has_value()) { - this->m_value = std::forward(rhs).get(); - } else { - this->m_value.~T(); +template +struct optional_operations_base : optional_storage_base { + using optional_storage_base::optional_storage_base; + + void hard_reset() noexcept { + get().~T(); this->m_has_value = false; - } } - else if (rhs.has_value()) { - construct(std::forward(rhs).get()); + template + void construct(Args &&... args) noexcept { + new (std::addressof(this->m_value)) T(std::forward(args)...); + this->m_has_value = true; } - } - bool has_value() const { return this->m_has_value; } + template + void assign(Opt &&rhs) { + if (this->has_value()) { + if (rhs.has_value()) { + this->m_value = std::forward(rhs).get(); + } else { + this->m_value.~T(); + this->m_has_value = false; + } + } + + else if (rhs.has_value()) { + construct(std::forward(rhs).get()); + } + } - TL_OPTIONAL_11_CONSTEXPR T &get() & { return this->m_value; } - TL_OPTIONAL_11_CONSTEXPR const T &get() const & { return this->m_value; } - TL_OPTIONAL_11_CONSTEXPR T &&get() && { return std::move(this->m_value); } + bool has_value() const { return this->m_has_value; } + + TL_OPTIONAL_11_CONSTEXPR T &get() & { return this->m_value; } + TL_OPTIONAL_11_CONSTEXPR const T &get() const & { return this->m_value; } + TL_OPTIONAL_11_CONSTEXPR T &&get() && { return std::move(this->m_value); } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr const T &&get() const && { return std::move(this->m_value); } + constexpr const T &&get() const && { return std::move(this->m_value); } #endif }; @@ -444,27 +491,27 @@ template struct optional_operations_base : optional_storage_base { // This specialization is for when T is trivially copy constructible template struct optional_copy_base : optional_operations_base { - using optional_operations_base::optional_operations_base; + using optional_operations_base::optional_operations_base; }; // This specialization is for when T is not trivially copy constructible template struct optional_copy_base : optional_operations_base { - using optional_operations_base::optional_operations_base; - - optional_copy_base() = default; - optional_copy_base(const optional_copy_base &rhs) - : optional_operations_base() { - if (rhs.has_value()) { - this->construct(rhs.get()); - } else { - this->m_has_value = false; + using optional_operations_base::optional_operations_base; + + optional_copy_base() = default; + optional_copy_base(const optional_copy_base &rhs) + : optional_operations_base() { + if (rhs.has_value()) { + this->construct(rhs.get()); + } else { + this->m_has_value = false; + } } - } - optional_copy_base(optional_copy_base &&rhs) = default; - optional_copy_base &operator=(const optional_copy_base &rhs) = default; - optional_copy_base &operator=(optional_copy_base &&rhs) = default; + optional_copy_base(optional_copy_base &&rhs) = default; + optional_copy_base &operator=(const optional_copy_base &rhs) = default; + optional_copy_base &operator=(optional_copy_base &&rhs) = default; }; // This class manages conditionally having a trivial move constructor @@ -475,51 +522,54 @@ struct optional_copy_base : optional_operations_base { #ifndef TL_OPTIONAL_GCC49 template ::value> struct optional_move_base : optional_copy_base { - using optional_copy_base::optional_copy_base; + using optional_copy_base::optional_copy_base; }; #else -template struct optional_move_base; +template +struct optional_move_base; #endif -template struct optional_move_base : optional_copy_base { - using optional_copy_base::optional_copy_base; - - optional_move_base() = default; - optional_move_base(const optional_move_base &rhs) = default; - - optional_move_base(optional_move_base &&rhs) noexcept( - std::is_nothrow_move_constructible::value) { - if (rhs.has_value()) { - this->construct(std::move(rhs.get())); - } else { - this->m_has_value = false; - } - } - optional_move_base &operator=(const optional_move_base &rhs) = default; - optional_move_base &operator=(optional_move_base &&rhs) = default; +template +struct optional_move_base : optional_copy_base { + using optional_copy_base::optional_copy_base; + + optional_move_base() = default; + optional_move_base(const optional_move_base &rhs) = default; + + optional_move_base(optional_move_base &&rhs) noexcept( + std::is_nothrow_move_constructible::value) { + if (rhs.has_value()) { + this->construct(std::move(rhs.get())); + } else { + this->m_has_value = false; + } + } + optional_move_base &operator=(const optional_move_base &rhs) = default; + optional_move_base &operator=(optional_move_base &&rhs) = default; }; // This class manages conditionally having a trivial copy assignment operator -template +template struct optional_copy_assign_base : optional_move_base { - using optional_move_base::optional_move_base; + using optional_move_base::optional_move_base; }; template struct optional_copy_assign_base : optional_move_base { - using optional_move_base::optional_move_base; - - optional_copy_assign_base() = default; - optional_copy_assign_base(const optional_copy_assign_base &rhs) = default; - - optional_copy_assign_base(optional_copy_assign_base &&rhs) = default; - optional_copy_assign_base &operator=(const optional_copy_assign_base &rhs) { - this->assign(rhs); - return *this; - } - optional_copy_assign_base & - operator=(optional_copy_assign_base &&rhs) = default; + using optional_move_base::optional_move_base; + + optional_copy_assign_base() = default; + optional_copy_assign_base(const optional_copy_assign_base &rhs) = default; + + optional_copy_assign_base(optional_copy_assign_base &&rhs) = default; + optional_copy_assign_base &operator=(const optional_copy_assign_base &rhs) { + this->assign(rhs); + return *this; + } + optional_copy_assign_base &operator=(optional_copy_assign_base &&rhs) = + default; }; // This class manages conditionally having a trivial move assignment operator @@ -528,79 +578,85 @@ struct optional_copy_assign_base : optional_move_base { // to make do with a non-trivial move assignment operator even if T is trivially // move assignable #ifndef TL_OPTIONAL_GCC49 -template ::value - &&std::is_trivially_move_constructible::value - &&std::is_trivially_move_assignable::value> +template ::value + &&std::is_trivially_move_constructible::value + &&std::is_trivially_move_assignable::value> struct optional_move_assign_base : optional_copy_assign_base { - using optional_copy_assign_base::optional_copy_assign_base; + using optional_copy_assign_base::optional_copy_assign_base; }; #else -template struct optional_move_assign_base; +template +struct optional_move_assign_base; #endif template struct optional_move_assign_base : optional_copy_assign_base { - using optional_copy_assign_base::optional_copy_assign_base; + using optional_copy_assign_base::optional_copy_assign_base; - optional_move_assign_base() = default; - optional_move_assign_base(const optional_move_assign_base &rhs) = default; + optional_move_assign_base() = default; + optional_move_assign_base(const optional_move_assign_base &rhs) = default; - optional_move_assign_base(optional_move_assign_base &&rhs) = default; + optional_move_assign_base(optional_move_assign_base &&rhs) = default; - optional_move_assign_base & - operator=(const optional_move_assign_base &rhs) = default; + optional_move_assign_base &operator=(const optional_move_assign_base &rhs) = + default; - optional_move_assign_base & - operator=(optional_move_assign_base &&rhs) noexcept( - std::is_nothrow_move_constructible::value - &&std::is_nothrow_move_assignable::value) { - this->assign(std::move(rhs)); - return *this; - } + optional_move_assign_base & + operator=(optional_move_assign_base &&rhs) noexcept( + std::is_nothrow_move_constructible::value + &&std::is_nothrow_move_assignable::value) { + this->assign(std::move(rhs)); + return *this; + } }; // optional_delete_ctor_base will conditionally delete copy and move // constructors depending on whether T is copy/move constructible -template ::value, +template ::value, bool EnableMove = std::is_move_constructible::value> struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = + default; + optional_delete_ctor_base &operator=( + optional_delete_ctor_base &&) noexcept = default; }; -template struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; +template +struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = + default; + optional_delete_ctor_base &operator=( + optional_delete_ctor_base &&) noexcept = default; }; -template struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; +template +struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = + default; + optional_delete_ctor_base &operator=( + optional_delete_ctor_base &&) noexcept = default; }; -template struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; +template +struct optional_delete_ctor_base { + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = + default; + optional_delete_ctor_base &operator=( + optional_delete_ctor_base &&) noexcept = default; }; // optional_delete_assign_base will conditionally delete copy and move @@ -611,64 +667,67 @@ template ::value && std::is_move_assignable::value)> struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = default; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base &operator=( + const optional_delete_assign_base &) = default; + optional_delete_assign_base &operator=( + optional_delete_assign_base &&) noexcept = default; }; -template struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = default; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = delete; +template +struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base &operator=( + const optional_delete_assign_base &) = default; + optional_delete_assign_base &operator=( + optional_delete_assign_base &&) noexcept = delete; }; -template struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = delete; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = default; +template +struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base &operator=( + const optional_delete_assign_base &) = delete; + optional_delete_assign_base &operator=( + optional_delete_assign_base &&) noexcept = default; }; -template struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = delete; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = delete; +template +struct optional_delete_assign_base { + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = + default; + optional_delete_assign_base &operator=( + const optional_delete_assign_base &) = delete; + optional_delete_assign_base &operator=( + optional_delete_assign_base &&) noexcept = delete; }; -} // namespace detail +} // namespace detail /// A tag type to represent an empty optional struct nullopt_t { - struct do_not_use {}; - constexpr explicit nullopt_t(do_not_use, do_not_use) noexcept {} + struct do_not_use {}; + constexpr explicit nullopt_t(do_not_use, do_not_use) noexcept {} }; /// Represents an empty optional static constexpr nullopt_t nullopt{nullopt_t::do_not_use{}, nullopt_t::do_not_use{}}; class bad_optional_access : public std::exception { -public: - bad_optional_access() = default; - const char *what() const noexcept { return "Optional has no value"; } + public: + bad_optional_access() = default; + const char *what() const noexcept { return "Optional has no value"; } }; /// An optional object is an object that contains the storage for another @@ -681,793 +740,823 @@ template class optional : private detail::optional_move_assign_base, private detail::optional_delete_ctor_base, private detail::optional_delete_assign_base { - using base = detail::optional_move_assign_base; + using base = detail::optional_move_assign_base; - static_assert(!std::is_same::value, - "instantiation of optional with in_place_t is ill-formed"); - static_assert(!std::is_same, nullopt_t>::value, - "instantiation of optional with nullopt_t is ill-formed"); + static_assert(!std::is_same::value, + "instantiation of optional with in_place_t is ill-formed"); + static_assert(!std::is_same, nullopt_t>::value, + "instantiation of optional with nullopt_t is ill-formed"); -public: + public: // The different versions for C++14 and 11 are needed because deduced return // types are not SFINAE-safe. This provides better support for things like // generic lambdas. C.f. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) - /// Carries out some operation which returns an optional on the stored - /// object if there is one. - template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } - - template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); - } - - template constexpr auto and_then(F &&f) const & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template + TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } + + template + constexpr auto and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template constexpr auto and_then(F &&f) const && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); - } + template + constexpr auto and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } #endif #else - /// Carries out some operation which returns an optional on the stored - /// object if there is one. - template - TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } - - template - TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); - } - - template - constexpr detail::invoke_result_t and_then(F &&f) const & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then( + F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } + + template + constexpr detail::invoke_result_t and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - constexpr detail::invoke_result_t and_then(F &&f) const && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); - } + template + constexpr detail::invoke_result_t and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : result(nullopt); + } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) - /// Carries out some operation on the stored object if there is one. - template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & { - return optional_map_impl(*this, std::forward(f)); - } - - template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) && { - return optional_map_impl(std::move(*this), std::forward(f)); - } - - template constexpr auto map(F &&f) const & { - return optional_map_impl(*this, std::forward(f)); - } - - template constexpr auto map(F &&f) const && { - return optional_map_impl(std::move(*this), std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr auto map(F &&f) const & { + return optional_map_impl(*this, std::forward(f)); + } + + template + constexpr auto map(F &&f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } #else - /// Carries out some operation on the stored object if there is one. - template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), - std::declval())) - map(F &&f) & { - return optional_map_impl(*this, std::forward(f)); - } - - template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), - std::declval())) - map(F &&f) && { - return optional_map_impl(std::move(*this), std::forward(f)); - } - - template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) - map(F &&f) const & { - return optional_map_impl(*this, std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype( + optional_map_impl(std::declval(), std::declval())) + map(F &&f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR decltype( + optional_map_impl(std::declval(), std::declval())) + map(F &&f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + map(F &&f) const & { + return optional_map_impl(*this, std::forward(f)); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) - map(F &&f) const && { - return optional_map_impl(std::move(*this), std::forward(f)); - } + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + map(F &&f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) - /// Carries out some operation on the stored object if there is one. - template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) & { - return optional_map_impl(*this, std::forward(f)); - } - - template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) && { - return optional_map_impl(std::move(*this), std::forward(f)); - } - - template constexpr auto transform(F&& f) const & { - return optional_map_impl(*this, std::forward(f)); - } - - template constexpr auto transform(F&& f) const && { - return optional_map_impl(std::move(*this), std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR auto transform(F &&f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR auto transform(F &&f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr auto transform(F &&f) const & { + return optional_map_impl(*this, std::forward(f)); + } + + template + constexpr auto transform(F &&f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } #else - /// Carries out some operation on the stored object if there is one. - template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) & { - return optional_map_impl(*this, std::forward(f)); - } - - template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) && { - return optional_map_impl(std::move(*this), std::forward(f)); - } - - template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) const & { - return optional_map_impl(*this, std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype( + optional_map_impl(std::declval(), std::declval())) + transform(F &&f) & { + return optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR decltype( + optional_map_impl(std::declval(), std::declval())) + transform(F &&f) && { + return optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + transform(F &&f) const & { + return optional_map_impl(*this, std::forward(f)); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) const && { - return optional_map_impl(std::move(*this), std::forward(f)); - } + template + constexpr decltype(optional_map_impl(std::declval(), + std::declval())) + transform(F &&f) const && { + return optional_map_impl(std::move(*this), std::forward(f)); + } #endif #endif - /// Calls `f` if the optional is empty - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { - if (has_value()) - return *this; + /// Calls `f` if the optional is empty + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + if (has_value()) return *this; - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { - return has_value() ? *this : std::forward(f)(); - } + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + return has_value() ? *this : std::forward(f)(); + } - template * = nullptr> - optional or_else(F &&f) && { - if (has_value()) - return std::move(*this); + template * = nullptr> + optional or_else(F &&f) && { + if (has_value()) return std::move(*this); - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) && { - return has_value() ? std::move(*this) : std::forward(f)(); - } + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) && { + return has_value() ? std::move(*this) : std::forward(f)(); + } - template * = nullptr> - optional or_else(F &&f) const & { - if (has_value()) - return *this; + template * = nullptr> + optional or_else(F &&f) const & { + if (has_value()) return *this; - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const & { - return has_value() ? *this : std::forward(f)(); - } + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const & { + return has_value() ? *this : std::forward(f)(); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template * = nullptr> - optional or_else(F &&f) const && { - if (has_value()) - return std::move(*this); + template * = nullptr> + optional or_else(F &&f) const && { + if (has_value()) return std::move(*this); - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional or_else(F &&f) const && { - return has_value() ? std::move(*this) : std::forward(f)(); - } + template * = nullptr> + optional or_else(F &&f) const && { + return has_value() ? std::move(*this) : std::forward(f)(); + } #endif - /// Maps the stored value with `f` if there is one, otherwise returns `u`. - template U map_or(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); - } + /// Maps the stored value with `f` if there is one, otherwise returns `u`. + template + U map_or(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } - template U map_or(F &&f, U &&u) && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); - } + template + U map_or(F &&f, U &&u) && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } - template U map_or(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); - } + template + U map_or(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template U map_or(F &&f, U &&u) const && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); - } + template + U map_or(F &&f, U &&u) const && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } #endif - /// Maps the stored value with `f` if there is one, otherwise calls - /// `u` and returns the result. - template - detail::invoke_result_t map_or_else(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); - } - - template - detail::invoke_result_t map_or_else(F &&f, U &&u) && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); - } - - template - detail::invoke_result_t map_or_else(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); - } + /// Maps the stored value with `f` if there is one, otherwise calls + /// `u` and returns the result. + template + detail::invoke_result_t map_or_else(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - detail::invoke_result_t map_or_else(F &&f, U &&u) const && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); - } + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } #endif - /// Returns `u` if `*this` has a value, otherwise an empty optional. - template - constexpr optional::type> conjunction(U &&u) const { - using result = optional>; - return has_value() ? result{u} : result{nullopt}; - } + /// Returns `u` if `*this` has a value, otherwise an empty optional. + template + constexpr optional::type> conjunction(U &&u) const { + using result = optional>; + return has_value() ? result{u} : result{nullopt}; + } - /// Returns `rhs` if `*this` is empty, otherwise the current value. - TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { - return has_value() ? *this : rhs; - } + /// Returns `rhs` if `*this` is empty, otherwise the current value. + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { + return has_value() ? *this : rhs; + } - constexpr optional disjunction(const optional &rhs) const & { - return has_value() ? *this : rhs; - } + constexpr optional disjunction(const optional &rhs) const & { + return has_value() ? *this : rhs; + } - TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { - return has_value() ? std::move(*this) : rhs; - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { + return has_value() ? std::move(*this) : rhs; + } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(const optional &rhs) const && { - return has_value() ? std::move(*this) : rhs; - } + constexpr optional disjunction(const optional &rhs) const && { + return has_value() ? std::move(*this) : rhs; + } #endif - TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { - return has_value() ? *this : std::move(rhs); - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { + return has_value() ? *this : std::move(rhs); + } - constexpr optional disjunction(optional &&rhs) const & { - return has_value() ? *this : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const & { + return has_value() ? *this : std::move(rhs); + } - TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { - return has_value() ? std::move(*this) : std::move(rhs); - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { + return has_value() ? std::move(*this) : std::move(rhs); + } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(optional &&rhs) const && { - return has_value() ? std::move(*this) : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const && { + return has_value() ? std::move(*this) : std::move(rhs); + } #endif - /// Takes the value out of the optional, leaving it empty - optional take() { - optional ret = std::move(*this); - reset(); - return ret; - } - - using value_type = T; - - /// Constructs an optional that does not contain a value. - constexpr optional() noexcept = default; - - constexpr optional(nullopt_t) noexcept {} - - /// Copy constructor - /// - /// If `rhs` contains a value, the stored value is direct-initialized with - /// it. Otherwise, the constructed optional is empty. - TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) = default; - - /// Move constructor - /// - /// If `rhs` contains a value, the stored value is direct-initialized with - /// it. Otherwise, the constructed optional is empty. - TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; - - /// Constructs the stored value in-place using the given arguments. - template - constexpr explicit optional( - detail::enable_if_t::value, in_place_t>, - Args &&... args) - : base(in_place, std::forward(args)...) {} - - template - TL_OPTIONAL_11_CONSTEXPR explicit optional( - detail::enable_if_t &, - Args &&...>::value, - in_place_t>, - std::initializer_list il, Args &&... args) { - this->construct(il, std::forward(args)...); - } - - /// Constructs the stored value with `u`. - template < - class U = T, - detail::enable_if_t::value> * = nullptr, - detail::enable_forward_value * = nullptr> - constexpr optional(U &&u) : base(in_place, std::forward(u)) {} - - template < - class U = T, - detail::enable_if_t::value> * = nullptr, - detail::enable_forward_value * = nullptr> - constexpr explicit optional(U &&u) : base(in_place, std::forward(u)) {} - - /// Converting copy constructor. - template < - class U, detail::enable_from_other * = nullptr, - detail::enable_if_t::value> * = nullptr> - optional(const optional &rhs) { - if (rhs.has_value()) { - this->construct(*rhs); - } - } - - template * = nullptr, - detail::enable_if_t::value> * = - nullptr> - explicit optional(const optional &rhs) { - if (rhs.has_value()) { - this->construct(*rhs); - } - } - - /// Converting move constructor. - template < - class U, detail::enable_from_other * = nullptr, - detail::enable_if_t::value> * = nullptr> - optional(optional &&rhs) { - if (rhs.has_value()) { - this->construct(std::move(*rhs)); - } - } - - template < - class U, detail::enable_from_other * = nullptr, - detail::enable_if_t::value> * = nullptr> - explicit optional(optional &&rhs) { - if (rhs.has_value()) { - this->construct(std::move(*rhs)); - } - } - - /// Destroys the stored value if there is one. - ~optional() = default; - - /// Assignment to empty. - /// - /// Destroys the current value if there is one. - optional &operator=(nullopt_t) noexcept { - if (has_value()) { - this->m_value.~T(); - this->m_has_value = false; - } - - return *this; - } - - /// Copy assignment. - /// - /// Copies the value from `rhs` if there is one. Otherwise resets the stored - /// value in `*this`. - optional &operator=(const optional &rhs) = default; - - /// Move assignment. - /// - /// Moves the value from `rhs` if there is one. Otherwise resets the stored - /// value in `*this`. - optional &operator=(optional &&rhs) = default; - - /// Assigns the stored value from `u`, destroying the old value if there was - /// one. - template * = nullptr> - optional &operator=(U &&u) { - if (has_value()) { - this->m_value = std::forward(u); - } else { - this->construct(std::forward(u)); - } - - return *this; - } - - /// Converting copy assignment operator. - /// - /// Copies the value from `rhs` if there is one. Otherwise resets the stored - /// value in `*this`. - template * = nullptr> - optional &operator=(const optional &rhs) { - if (has_value()) { - if (rhs.has_value()) { - this->m_value = *rhs; - } else { - this->hard_reset(); - } - } - - if (rhs.has_value()) { - this->construct(*rhs); - } - - return *this; - } - - // TODO check exception guarantee - /// Converting move assignment operator. - /// - /// Moves the value from `rhs` if there is one. Otherwise resets the stored - /// value in `*this`. - template * = nullptr> - optional &operator=(optional &&rhs) { - if (has_value()) { - if (rhs.has_value()) { - this->m_value = std::move(*rhs); - } else { - this->hard_reset(); - } - } - - if (rhs.has_value()) { - this->construct(std::move(*rhs)); - } - - return *this; - } - - /// Constructs the value in-place, destroying the current one if there is - /// one. - template T &emplace(Args &&... args) { - static_assert(std::is_constructible::value, - "T must be constructible with Args"); - - *this = nullopt; - this->construct(std::forward(args)...); - return value(); - } - - template - detail::enable_if_t< - std::is_constructible &, Args &&...>::value, - T &> - emplace(std::initializer_list il, Args &&... args) { - *this = nullopt; - this->construct(il, std::forward(args)...); - return value(); - } - - /// Swaps this optional with the other. - /// - /// If neither optionals have a value, nothing happens. - /// If both have a value, the values are swapped. - /// If one has a value, it is moved to the other and the movee is left - /// valueless. - void - swap(optional &rhs) noexcept(std::is_nothrow_move_constructible::value - &&detail::is_nothrow_swappable::value) { - using std::swap; - if (has_value()) { - if (rhs.has_value()) { - swap(**this, *rhs); - } else { - new (std::addressof(rhs.m_value)) T(std::move(this->m_value)); - this->m_value.T::~T(); - } - } else if (rhs.has_value()) { - new (std::addressof(this->m_value)) T(std::move(rhs.m_value)); - rhs.m_value.T::~T(); - } - swap(this->m_has_value, rhs.m_has_value); - } - - /// Returns a pointer to the stored value - constexpr const T *operator->() const { - return std::addressof(this->m_value); - } - - TL_OPTIONAL_11_CONSTEXPR T *operator->() { - return std::addressof(this->m_value); - } - - /// Returns the stored value - TL_OPTIONAL_11_CONSTEXPR T &operator*() & { return this->m_value; } - - constexpr const T &operator*() const & { return this->m_value; } - - TL_OPTIONAL_11_CONSTEXPR T &&operator*() && { - return std::move(this->m_value); - } + /// Takes the value out of the optional, leaving it empty + optional take() { + optional ret = std::move(*this); + reset(); + return ret; + } + + using value_type = T; + + /// Constructs an optional that does not contain a value. + constexpr optional() noexcept = default; + + constexpr optional(nullopt_t) noexcept {} + + /// Copy constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) = default; + + /// Move constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; + + /// Constructs the stored value in-place using the given arguments. + template + constexpr explicit optional( + detail::enable_if_t::value, + in_place_t>, + Args &&... args) + : base(in_place, std::forward(args)...) {} + + template + TL_OPTIONAL_11_CONSTEXPR explicit optional( + detail::enable_if_t &, + Args &&...>::value, + in_place_t>, + std::initializer_list il, + Args &&... args) { + this->construct(il, std::forward(args)...); + } + + /// Constructs the stored value with `u`. + template < + class U = T, + detail::enable_if_t::value> * = nullptr, + detail::enable_forward_value * = nullptr> + constexpr optional(U &&u) : base(in_place, std::forward(u)) {} + + template < + class U = T, + detail::enable_if_t::value> * = nullptr, + detail::enable_forward_value * = nullptr> + constexpr explicit optional(U &&u) : base(in_place, std::forward(u)) {} + + /// Converting copy constructor. + template * = nullptr, + detail::enable_if_t::value> * = + nullptr> + optional(const optional &rhs) { + if (rhs.has_value()) { + this->construct(*rhs); + } + } + + template * = nullptr, + detail::enable_if_t::value> * = + nullptr> + explicit optional(const optional &rhs) { + if (rhs.has_value()) { + this->construct(*rhs); + } + } + + /// Converting move constructor. + template < + class U, + detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> + optional(optional &&rhs) { + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } + } + + template < + class U, + detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> + explicit optional(optional &&rhs) { + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } + } + + /// Destroys the stored value if there is one. + ~optional() = default; + + /// Assignment to empty. + /// + /// Destroys the current value if there is one. + optional &operator=(nullopt_t) noexcept { + if (has_value()) { + this->m_value.~T(); + this->m_has_value = false; + } + + return *this; + } + + /// Copy assignment. + /// + /// Copies the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + optional &operator=(const optional &rhs) = default; + + /// Move assignment. + /// + /// Moves the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + optional &operator=(optional &&rhs) = default; + + /// Assigns the stored value from `u`, destroying the old value if there was + /// one. + template * = nullptr> + optional &operator=(U &&u) { + if (has_value()) { + this->m_value = std::forward(u); + } else { + this->construct(std::forward(u)); + } + + return *this; + } + + /// Converting copy assignment operator. + /// + /// Copies the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + template * = nullptr> + optional &operator=(const optional &rhs) { + if (has_value()) { + if (rhs.has_value()) { + this->m_value = *rhs; + } else { + this->hard_reset(); + } + } + + if (rhs.has_value()) { + this->construct(*rhs); + } + + return *this; + } + + // TODO check exception guarantee + /// Converting move assignment operator. + /// + /// Moves the value from `rhs` if there is one. Otherwise resets the stored + /// value in `*this`. + template * = nullptr> + optional &operator=(optional &&rhs) { + if (has_value()) { + if (rhs.has_value()) { + this->m_value = std::move(*rhs); + } else { + this->hard_reset(); + } + } + + if (rhs.has_value()) { + this->construct(std::move(*rhs)); + } + + return *this; + } + + /// Constructs the value in-place, destroying the current one if there is + /// one. + template + T &emplace(Args &&... args) { + static_assert(std::is_constructible::value, + "T must be constructible with Args"); + + *this = nullopt; + this->construct(std::forward(args)...); + return value(); + } + + template + detail::enable_if_t< + std::is_constructible &, Args &&...>::value, + T &> + emplace(std::initializer_list il, Args &&... args) { + *this = nullopt; + this->construct(il, std::forward(args)...); + return value(); + } + + /// Swaps this optional with the other. + /// + /// If neither optionals have a value, nothing happens. + /// If both have a value, the values are swapped. + /// If one has a value, it is moved to the other and the movee is left + /// valueless. + void swap(optional &rhs) noexcept( + std::is_nothrow_move_constructible::value + &&detail::is_nothrow_swappable::value) { + using std::swap; + if (has_value()) { + if (rhs.has_value()) { + swap(**this, *rhs); + } else { + new (std::addressof(rhs.m_value)) T(std::move(this->m_value)); + this->m_value.T::~T(); + } + } else if (rhs.has_value()) { + new (std::addressof(this->m_value)) T(std::move(rhs.m_value)); + rhs.m_value.T::~T(); + } + swap(this->m_has_value, rhs.m_has_value); + } + + /// Returns a pointer to the stored value + constexpr const T *operator->() const { + return std::addressof(this->m_value); + } + + TL_OPTIONAL_11_CONSTEXPR T *operator->() { + return std::addressof(this->m_value); + } + + /// Returns the stored value + TL_OPTIONAL_11_CONSTEXPR T &operator*() & { return this->m_value; } + + constexpr const T &operator*() const & { return this->m_value; } + + TL_OPTIONAL_11_CONSTEXPR T &&operator*() && { + return std::move(this->m_value); + } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr const T &&operator*() const && { return std::move(this->m_value); } + constexpr const T &&operator*() const && { + return std::move(this->m_value); + } #endif - /// Returns whether or not the optional has a value - constexpr bool has_value() const noexcept { return this->m_has_value; } - - constexpr explicit operator bool() const noexcept { - return this->m_has_value; - } - - /// Returns the contained value if there is one, otherwise throws bad_optional_access - TL_OPTIONAL_11_CONSTEXPR T &value() & { - if (has_value()) - return this->m_value; - throw bad_optional_access(); - } - TL_OPTIONAL_11_CONSTEXPR const T &value() const & { - if (has_value()) - return this->m_value; - throw bad_optional_access(); - } - TL_OPTIONAL_11_CONSTEXPR T &&value() && { - if (has_value()) - return std::move(this->m_value); - throw bad_optional_access(); - } + /// Returns whether or not the optional has a value + constexpr bool has_value() const noexcept { return this->m_has_value; } + + constexpr explicit operator bool() const noexcept { + return this->m_has_value; + } + + /// Returns the contained value if there is one, otherwise throws + /// bad_optional_access + TL_OPTIONAL_11_CONSTEXPR T &value() & { + if (has_value()) return this->m_value; + throw bad_optional_access(); + } + TL_OPTIONAL_11_CONSTEXPR const T &value() const & { + if (has_value()) return this->m_value; + throw bad_optional_access(); + } + TL_OPTIONAL_11_CONSTEXPR T &&value() && { + if (has_value()) return std::move(this->m_value); + throw bad_optional_access(); + } #ifndef TL_OPTIONAL_NO_CONSTRR - TL_OPTIONAL_11_CONSTEXPR const T &&value() const && { - if (has_value()) - return std::move(this->m_value); - throw bad_optional_access(); - } + TL_OPTIONAL_11_CONSTEXPR const T &&value() const && { + if (has_value()) return std::move(this->m_value); + throw bad_optional_access(); + } #endif - /// Returns the stored value if there is one, otherwise returns `u` - template constexpr T value_or(U &&u) const & { - static_assert(std::is_copy_constructible::value && - std::is_convertible::value, - "T must be copy constructible and convertible from U"); - return has_value() ? **this : static_cast(std::forward(u)); - } - - template TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && { - static_assert(std::is_move_constructible::value && - std::is_convertible::value, - "T must be move constructible and convertible from U"); - return has_value() ? **this : static_cast(std::forward(u)); - } - - /// Destroys the stored value if one exists, making the optional empty - void reset() noexcept { - if (has_value()) { - this->m_value.~T(); - this->m_has_value = false; - } - } -}; // namespace tl + /// Returns the stored value if there is one, otherwise returns `u` + template + constexpr T value_or(U &&u) const & { + static_assert(std::is_copy_constructible::value && + std::is_convertible::value, + "T must be copy constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + template + TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && { + static_assert(std::is_move_constructible::value && + std::is_convertible::value, + "T must be move constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + /// Destroys the stored value if one exists, making the optional empty + void reset() noexcept { + if (has_value()) { + this->m_value.~T(); + this->m_has_value = false; + } + } +}; // namespace tl /// Compares two optional objects template inline constexpr bool operator==(const optional &lhs, const optional &rhs) { - return lhs.has_value() == rhs.has_value() && - (!lhs.has_value() || *lhs == *rhs); + return lhs.has_value() == rhs.has_value() && + (!lhs.has_value() || *lhs == *rhs); } template inline constexpr bool operator!=(const optional &lhs, const optional &rhs) { - return lhs.has_value() != rhs.has_value() || - (lhs.has_value() && *lhs != *rhs); + return lhs.has_value() != rhs.has_value() || + (lhs.has_value() && *lhs != *rhs); } template inline constexpr bool operator<(const optional &lhs, const optional &rhs) { - return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs); + return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs); } template inline constexpr bool operator>(const optional &lhs, const optional &rhs) { - return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs); + return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs); } template inline constexpr bool operator<=(const optional &lhs, const optional &rhs) { - return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs); + return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs); } template inline constexpr bool operator>=(const optional &lhs, const optional &rhs) { - return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs); + return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs); } /// Compares an optional to a `nullopt` template inline constexpr bool operator==(const optional &lhs, nullopt_t) noexcept { - return !lhs.has_value(); + return !lhs.has_value(); } template inline constexpr bool operator==(nullopt_t, const optional &rhs) noexcept { - return !rhs.has_value(); + return !rhs.has_value(); } template inline constexpr bool operator!=(const optional &lhs, nullopt_t) noexcept { - return lhs.has_value(); + return lhs.has_value(); } template inline constexpr bool operator!=(nullopt_t, const optional &rhs) noexcept { - return rhs.has_value(); + return rhs.has_value(); } template inline constexpr bool operator<(const optional &, nullopt_t) noexcept { - return false; + return false; } template inline constexpr bool operator<(nullopt_t, const optional &rhs) noexcept { - return rhs.has_value(); + return rhs.has_value(); } template inline constexpr bool operator<=(const optional &lhs, nullopt_t) noexcept { - return !lhs.has_value(); + return !lhs.has_value(); } template inline constexpr bool operator<=(nullopt_t, const optional &) noexcept { - return true; + return true; } template inline constexpr bool operator>(const optional &lhs, nullopt_t) noexcept { - return lhs.has_value(); + return lhs.has_value(); } template inline constexpr bool operator>(nullopt_t, const optional &) noexcept { - return false; + return false; } template inline constexpr bool operator>=(const optional &, nullopt_t) noexcept { - return true; + return true; } template inline constexpr bool operator>=(nullopt_t, const optional &rhs) noexcept { - return !rhs.has_value(); + return !rhs.has_value(); } /// Compares the optional with a value. template inline constexpr bool operator==(const optional &lhs, const U &rhs) { - return lhs.has_value() ? *lhs == rhs : false; + return lhs.has_value() ? *lhs == rhs : false; } template inline constexpr bool operator==(const U &lhs, const optional &rhs) { - return rhs.has_value() ? lhs == *rhs : false; + return rhs.has_value() ? lhs == *rhs : false; } template inline constexpr bool operator!=(const optional &lhs, const U &rhs) { - return lhs.has_value() ? *lhs != rhs : true; + return lhs.has_value() ? *lhs != rhs : true; } template inline constexpr bool operator!=(const U &lhs, const optional &rhs) { - return rhs.has_value() ? lhs != *rhs : true; + return rhs.has_value() ? lhs != *rhs : true; } template inline constexpr bool operator<(const optional &lhs, const U &rhs) { - return lhs.has_value() ? *lhs < rhs : true; + return lhs.has_value() ? *lhs < rhs : true; } template inline constexpr bool operator<(const U &lhs, const optional &rhs) { - return rhs.has_value() ? lhs < *rhs : false; + return rhs.has_value() ? lhs < *rhs : false; } template inline constexpr bool operator<=(const optional &lhs, const U &rhs) { - return lhs.has_value() ? *lhs <= rhs : true; + return lhs.has_value() ? *lhs <= rhs : true; } template inline constexpr bool operator<=(const U &lhs, const optional &rhs) { - return rhs.has_value() ? lhs <= *rhs : false; + return rhs.has_value() ? lhs <= *rhs : false; } template inline constexpr bool operator>(const optional &lhs, const U &rhs) { - return lhs.has_value() ? *lhs > rhs : false; + return lhs.has_value() ? *lhs > rhs : false; } template inline constexpr bool operator>(const U &lhs, const optional &rhs) { - return rhs.has_value() ? lhs > *rhs : true; + return rhs.has_value() ? lhs > *rhs : true; } template inline constexpr bool operator>=(const optional &lhs, const U &rhs) { - return lhs.has_value() ? *lhs >= rhs : false; + return lhs.has_value() ? *lhs >= rhs : false; } template inline constexpr bool operator>=(const U &lhs, const optional &rhs) { - return rhs.has_value() ? lhs >= *rhs : true; + return rhs.has_value() ? lhs >= *rhs : true; } template ::value> * = nullptr> void swap(optional &lhs, optional &rhs) noexcept(noexcept(lhs.swap(rhs))) { - return lhs.swap(rhs); + return lhs.swap(rhs); } namespace detail { struct i_am_secret {}; -} // namespace detail +} // namespace detail -template ::value, - detail::decay_t, T>> + detail::decay_t, + T>> inline constexpr optional make_optional(U &&v) { - return optional(std::forward(v)); + return optional(std::forward(v)); } template inline constexpr optional make_optional(Args &&... args) { - return optional(in_place, std::forward(args)...); + return optional(in_place, std::forward(args)...); } template inline constexpr optional make_optional(std::initializer_list il, Args &&... args) { - return optional(in_place, il, std::forward(args)...); + return optional(in_place, il, std::forward(args)...); } #if __cplusplus >= 201703L -template optional(T)->optional; +template +optional(T)->optional; #endif /// \exclude namespace detail { #ifdef TL_OPTIONAL_CXX14 -template (), *std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto optional_map_impl(Opt &&opt, F &&f) { - return opt.has_value() - ? detail::invoke(std::forward(f), *std::forward(opt)) - : optional(nullopt); + return opt.has_value() + ? detail::invoke(std::forward(f), *std::forward(opt)) + : optional(nullopt); } -template (), *std::declval())), detail::enable_if_t::value> * = nullptr> auto optional_map_impl(Opt &&opt, F &&f) { - if (opt.has_value()) { - detail::invoke(std::forward(f), *std::forward(opt)); - return make_optional(monostate{}); - } + if (opt.has_value()) { + detail::invoke(std::forward(f), *std::forward(opt)); + return make_optional(monostate{}); + } - return optional(nullopt); + return optional(nullopt); } #else -template (), *std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto optional_map_impl(Opt &&opt, F &&f) -> optional { - return opt.has_value() - ? detail::invoke(std::forward(f), *std::forward(opt)) - : optional(nullopt); + return opt.has_value() + ? detail::invoke(std::forward(f), *std::forward(opt)) + : optional(nullopt); } -template (), *std::declval())), detail::enable_if_t::value> * = nullptr> auto optional_map_impl(Opt &&opt, F &&f) -> optional { - if (opt.has_value()) { - detail::invoke(std::forward(f), *std::forward(opt)); - return monostate{}; - } + if (opt.has_value()) { + detail::invoke(std::forward(f), *std::forward(opt)); + return monostate{}; + } - return nullopt; + return nullopt; } #endif -} // namespace detail +} // namespace detail /// Specialization for when `T` is a reference. `optional` acts similarly /// to a `T*`, but provides more operations and shows intent more clearly. -template class optional { -public: +template +class optional { + public: // The different versions for C++14 and 11 are needed because deduced return // types are not SFINAE-safe. This provides better support for things like // generic lambdas. C.f. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) - /// Carries out some operation which returns an optional on the stored - /// object if there is one. - template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } - template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + template + TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } - template constexpr auto and_then(F &&f) const & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + template + constexpr auto and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template constexpr auto and_then(F &&f) const && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + template + constexpr auto and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } #endif #else - /// Carries out some operation which returns an optional on the stored - /// object if there is one. - template - TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } - - template - TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } - - template - constexpr detail::invoke_result_t and_then(F &&f) const & { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + /// Carries out some operation which returns an optional on the stored + /// object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template + TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then( + F &&f) && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } + + template + constexpr detail::invoke_result_t and_then(F &&f) const & { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - constexpr detail::invoke_result_t and_then(F &&f) const && { - using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); - - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); - } + template + constexpr detail::invoke_result_t and_then(F &&f) const && { + using result = detail::invoke_result_t; + static_assert(detail::is_optional::value, + "F must return an optional"); + + return has_value() ? detail::invoke(std::forward(f), **this) + : result(nullopt); + } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) - /// Carries out some operation on the stored object if there is one. - template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & { - return detail::optional_map_impl(*this, std::forward(f)); - } - - template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } - - template constexpr auto map(F &&f) const & { - return detail::optional_map_impl(*this, std::forward(f)); - } - - template constexpr auto map(F &&f) const && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr auto map(F &&f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template + constexpr auto map(F &&f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } #else - /// Carries out some operation on the stored object if there is one. - template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), - std::declval())) - map(F &&f) & { - return detail::optional_map_impl(*this, std::forward(f)); - } - - template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), - std::declval())) - map(F &&f) && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } - - template - constexpr decltype(detail::optional_map_impl(std::declval(), - std::declval())) - map(F &&f) const & { - return detail::optional_map_impl(*this, std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( + std::declval(), std::declval())) + map(F &&f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( + std::declval(), std::declval())) + map(F &&f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(detail::optional_map_impl( + std::declval(), std::declval())) + map(F &&f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - constexpr decltype(detail::optional_map_impl(std::declval(), - std::declval())) - map(F &&f) const && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } + template + constexpr decltype(detail::optional_map_impl( + std::declval(), std::declval())) + map(F &&f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \ !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) - /// Carries out some operation on the stored object if there is one. - template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) & { - return detail::optional_map_impl(*this, std::forward(f)); - } - - template TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } - - template constexpr auto transform(F&& f) const & { - return detail::optional_map_impl(*this, std::forward(f)); - } - - template constexpr auto transform(F&& f) const && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR auto transform(F &&f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template + TL_OPTIONAL_11_CONSTEXPR auto transform(F &&f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr auto transform(F &&f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + template + constexpr auto transform(F &&f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } #else - /// Carries out some operation on the stored object if there is one. - template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) & { - return detail::optional_map_impl(*this, std::forward(f)); - } - - /// \group map - /// \synopsis template auto transform(F &&f) &&; - template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } - - template - constexpr decltype(detail::optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) const & { - return detail::optional_map_impl(*this, std::forward(f)); - } + /// Carries out some operation on the stored object if there is one. + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( + std::declval(), std::declval())) + transform(F &&f) & { + return detail::optional_map_impl(*this, std::forward(f)); + } + + /// \group map + /// \synopsis template auto transform(F &&f) &&; + template + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( + std::declval(), std::declval())) + transform(F &&f) && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } + + template + constexpr decltype(detail::optional_map_impl( + std::declval(), std::declval())) + transform(F &&f) const & { + return detail::optional_map_impl(*this, std::forward(f)); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - constexpr decltype(detail::optional_map_impl(std::declval(), - std::declval())) - transform(F&& f) const && { - return detail::optional_map_impl(std::move(*this), std::forward(f)); - } + template + constexpr decltype(detail::optional_map_impl( + std::declval(), std::declval())) + transform(F &&f) const && { + return detail::optional_map_impl(std::move(*this), std::forward(f)); + } #endif #endif - /// Calls `f` if the optional is empty - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { - if (has_value()) - return *this; + /// Calls `f` if the optional is empty + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + if (has_value()) return *this; - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { - return has_value() ? *this : std::forward(f)(); - } + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & { + return has_value() ? *this : std::forward(f)(); + } - template * = nullptr> - optional or_else(F &&f) && { - if (has_value()) - return std::move(*this); + template * = nullptr> + optional or_else(F &&f) && { + if (has_value()) return std::move(*this); - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) && { - return has_value() ? std::move(*this) : std::forward(f)(); - } + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) && { + return has_value() ? std::move(*this) : std::forward(f)(); + } - template * = nullptr> - optional or_else(F &&f) const & { - if (has_value()) - return *this; + template * = nullptr> + optional or_else(F &&f) const & { + if (has_value()) return *this; - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const & { - return has_value() ? *this : std::forward(f)(); - } + template * = nullptr> + optional TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const & { + return has_value() ? *this : std::forward(f)(); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template * = nullptr> - optional or_else(F &&f) const && { - if (has_value()) - return std::move(*this); + template * = nullptr> + optional or_else(F &&f) const && { + if (has_value()) return std::move(*this); - std::forward(f)(); - return nullopt; - } + std::forward(f)(); + return nullopt; + } - template * = nullptr> - optional or_else(F &&f) const && { - return has_value() ? std::move(*this) : std::forward(f)(); - } + template * = nullptr> + optional or_else(F &&f) const && { + return has_value() ? std::move(*this) : std::forward(f)(); + } #endif - /// Maps the stored value with `f` if there is one, otherwise returns `u` - template U map_or(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); - } + /// Maps the stored value with `f` if there is one, otherwise returns `u` + template + U map_or(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } - template U map_or(F &&f, U &&u) && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); - } + template + U map_or(F &&f, U &&u) && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } - template U map_or(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); - } + template + U map_or(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template U map_or(F &&f, U &&u) const && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); - } + template + U map_or(F &&f, U &&u) const && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u); + } #endif - /// Maps the stored value with `f` if there is one, otherwise calls - /// `u` and returns the result. - template - detail::invoke_result_t map_or_else(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); - } - - template - detail::invoke_result_t map_or_else(F &&f, U &&u) && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); - } - - template - detail::invoke_result_t map_or_else(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); - } + /// Maps the stored value with `f` if there is one, otherwise calls + /// `u` and returns the result. + template + detail::invoke_result_t map_or_else(F &&f, U &&u) & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } + + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const & { + return has_value() ? detail::invoke(std::forward(f), **this) + : std::forward(u)(); + } #ifndef TL_OPTIONAL_NO_CONSTRR - template - detail::invoke_result_t map_or_else(F &&f, U &&u) const && { - return has_value() ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); - } + template + detail::invoke_result_t map_or_else(F &&f, U &&u) const && { + return has_value() + ? detail::invoke(std::forward(f), std::move(**this)) + : std::forward(u)(); + } #endif - /// Returns `u` if `*this` has a value, otherwise an empty optional. - template - constexpr optional::type> conjunction(U &&u) const { - using result = optional>; - return has_value() ? result{u} : result{nullopt}; - } + /// Returns `u` if `*this` has a value, otherwise an empty optional. + template + constexpr optional::type> conjunction(U &&u) const { + using result = optional>; + return has_value() ? result{u} : result{nullopt}; + } - /// Returns `rhs` if `*this` is empty, otherwise the current value. - TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { - return has_value() ? *this : rhs; - } + /// Returns `rhs` if `*this` is empty, otherwise the current value. + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { + return has_value() ? *this : rhs; + } - constexpr optional disjunction(const optional &rhs) const & { - return has_value() ? *this : rhs; - } + constexpr optional disjunction(const optional &rhs) const & { + return has_value() ? *this : rhs; + } - TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { - return has_value() ? std::move(*this) : rhs; - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { + return has_value() ? std::move(*this) : rhs; + } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(const optional &rhs) const && { - return has_value() ? std::move(*this) : rhs; - } + constexpr optional disjunction(const optional &rhs) const && { + return has_value() ? std::move(*this) : rhs; + } #endif - TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { - return has_value() ? *this : std::move(rhs); - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { + return has_value() ? *this : std::move(rhs); + } - constexpr optional disjunction(optional &&rhs) const & { - return has_value() ? *this : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const & { + return has_value() ? *this : std::move(rhs); + } - TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { - return has_value() ? std::move(*this) : std::move(rhs); - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { + return has_value() ? std::move(*this) : std::move(rhs); + } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(optional &&rhs) const && { - return has_value() ? std::move(*this) : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const && { + return has_value() ? std::move(*this) : std::move(rhs); + } #endif - /// Takes the value out of the optional, leaving it empty - optional take() { - optional ret = std::move(*this); - reset(); - return ret; - } - - using value_type = T &; - - /// Constructs an optional that does not contain a value. - constexpr optional() noexcept : m_value(nullptr) {} - - constexpr optional(nullopt_t) noexcept : m_value(nullptr) {} - - /// Copy constructor - /// - /// If `rhs` contains a value, the stored value is direct-initialized with - /// it. Otherwise, the constructed optional is empty. - TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) noexcept = default; - - /// Move constructor - /// - /// If `rhs` contains a value, the stored value is direct-initialized with - /// it. Otherwise, the constructed optional is empty. - TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; - - /// Constructs the stored value with `u`. - template >::value> - * = nullptr> - constexpr optional(U &&u) noexcept : m_value(std::addressof(u)) { - static_assert(std::is_lvalue_reference::value, "U must be an lvalue"); - } - - template - constexpr explicit optional(const optional &rhs) noexcept : optional(*rhs) {} - - /// No-op - ~optional() = default; - - /// Assignment to empty. - /// - /// Destroys the current value if there is one. - optional &operator=(nullopt_t) noexcept { - m_value = nullptr; - return *this; - } - - /// Copy assignment. - /// - /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise - /// resets the stored value in `*this`. - optional &operator=(const optional &rhs) = default; - - /// Rebinds this optional to `u`. - template >::value> - * = nullptr> - optional &operator=(U &&u) { - static_assert(std::is_lvalue_reference::value, "U must be an lvalue"); - m_value = std::addressof(u); - return *this; - } - - /// Converting copy assignment operator. - /// - /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise - /// resets the stored value in `*this`. - template optional &operator=(const optional &rhs) noexcept { - m_value = std::addressof(rhs.value()); - return *this; - } - - /// Rebinds this optional to `u`. - template >::value> - * = nullptr> - optional &emplace(U &&u) noexcept { - return *this = std::forward(u); - } - - void swap(optional &rhs) noexcept { std::swap(m_value, rhs.m_value); } - - /// Returns a pointer to the stored value - constexpr const T *operator->() const noexcept { return m_value; } - - TL_OPTIONAL_11_CONSTEXPR T *operator->() noexcept { return m_value; } - - /// Returns the stored value - TL_OPTIONAL_11_CONSTEXPR T &operator*() noexcept { return *m_value; } - - constexpr const T &operator*() const noexcept { return *m_value; } - - constexpr bool has_value() const noexcept { return m_value != nullptr; } - - constexpr explicit operator bool() const noexcept { - return m_value != nullptr; - } - - /// Returns the contained value if there is one, otherwise throws bad_optional_access - TL_OPTIONAL_11_CONSTEXPR T &value() { - if (has_value()) - return *m_value; - throw bad_optional_access(); - } - TL_OPTIONAL_11_CONSTEXPR const T &value() const { - if (has_value()) - return *m_value; - throw bad_optional_access(); - } - - /// Returns the stored value if there is one, otherwise returns `u` - template constexpr T value_or(U &&u) const & noexcept { - static_assert(std::is_copy_constructible::value && - std::is_convertible::value, - "T must be copy constructible and convertible from U"); - return has_value() ? **this : static_cast(std::forward(u)); - } - - /// \group value_or - template TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && noexcept { - static_assert(std::is_move_constructible::value && - std::is_convertible::value, - "T must be move constructible and convertible from U"); - return has_value() ? **this : static_cast(std::forward(u)); - } - - /// Destroys the stored value if one exists, making the optional empty - void reset() noexcept { m_value = nullptr; } - -private: - T *m_value; -}; // namespace tl - - - -} // namespace tl + /// Takes the value out of the optional, leaving it empty + optional take() { + optional ret = std::move(*this); + reset(); + return ret; + } + + using value_type = T &; + + /// Constructs an optional that does not contain a value. + constexpr optional() noexcept : m_value(nullptr) {} + + constexpr optional(nullopt_t) noexcept : m_value(nullptr) {} + + /// Copy constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) noexcept = default; + + /// Move constructor + /// + /// If `rhs` contains a value, the stored value is direct-initialized with + /// it. Otherwise, the constructed optional is empty. + TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; + + /// Constructs the stored value with `u`. + template >::value> * = nullptr> + constexpr optional(U &&u) noexcept : m_value(std::addressof(u)) { + static_assert(std::is_lvalue_reference::value, + "U must be an lvalue"); + } + + template + constexpr explicit optional(const optional &rhs) noexcept + : optional(*rhs) {} + + /// No-op + ~optional() = default; + + /// Assignment to empty. + /// + /// Destroys the current value if there is one. + optional &operator=(nullopt_t) noexcept { + m_value = nullptr; + return *this; + } + + /// Copy assignment. + /// + /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise + /// resets the stored value in `*this`. + optional &operator=(const optional &rhs) = default; + + /// Rebinds this optional to `u`. + template >::value> * = nullptr> + optional &operator=(U &&u) { + static_assert(std::is_lvalue_reference::value, + "U must be an lvalue"); + m_value = std::addressof(u); + return *this; + } + + /// Converting copy assignment operator. + /// + /// Rebinds this optional to the referee of `rhs` if there is one. Otherwise + /// resets the stored value in `*this`. + template + optional &operator=(const optional &rhs) noexcept { + m_value = std::addressof(rhs.value()); + return *this; + } + + /// Rebinds this optional to `u`. + template >::value> * = nullptr> + optional &emplace(U &&u) noexcept { + return *this = std::forward(u); + } + + void swap(optional &rhs) noexcept { std::swap(m_value, rhs.m_value); } + + /// Returns a pointer to the stored value + constexpr const T *operator->() const noexcept { return m_value; } + + TL_OPTIONAL_11_CONSTEXPR T *operator->() noexcept { return m_value; } + + /// Returns the stored value + TL_OPTIONAL_11_CONSTEXPR T &operator*() noexcept { return *m_value; } + + constexpr const T &operator*() const noexcept { return *m_value; } + + constexpr bool has_value() const noexcept { return m_value != nullptr; } + + constexpr explicit operator bool() const noexcept { + return m_value != nullptr; + } + + /// Returns the contained value if there is one, otherwise throws + /// bad_optional_access + TL_OPTIONAL_11_CONSTEXPR T &value() { + if (has_value()) return *m_value; + throw bad_optional_access(); + } + TL_OPTIONAL_11_CONSTEXPR const T &value() const { + if (has_value()) return *m_value; + throw bad_optional_access(); + } + + /// Returns the stored value if there is one, otherwise returns `u` + template + constexpr T value_or(U &&u) const &noexcept { + static_assert(std::is_copy_constructible::value && + std::is_convertible::value, + "T must be copy constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + /// \group value_or + template + TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && noexcept { + static_assert(std::is_move_constructible::value && + std::is_convertible::value, + "T must be move constructible and convertible from U"); + return has_value() ? **this : static_cast(std::forward(u)); + } + + /// Destroys the stored value if one exists, making the optional empty + void reset() noexcept { m_value = nullptr; } + + private: + T *m_value; +}; // namespace tl + + +} // namespace tl namespace std { // TODO SFINAE -template struct hash> { - ::std::size_t operator()(const tl::optional &o) const { - if (!o.has_value()) - return 0; +template +struct hash> { + ::std::size_t operator()(const tl::optional &o) const { + if (!o.has_value()) return 0; - return std::hash>()(*o); - } + return std::hash>()(*o); + } }; -} // namespace std +} // namespace std #endif diff --git a/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common.h b/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common.h index 41850cbe0..ef4facea6 100644 --- a/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common.h +++ b/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common.h @@ -14,9 +14,9 @@ #pragma once -#include "feat/feature-window.h" -#include #include +#include +#include "feat/feature-window.h" namespace paddleaudio { @@ -27,18 +27,14 @@ class StreamingFeatureTpl { public: typedef typename F::Options Options; StreamingFeatureTpl(const Options& opts); - bool ComputeFeature(const kaldi::VectorBase& wav, + bool ComputeFeature(const kaldi::VectorBase& wav, kaldi::Vector* feats); - void Reset() { - remained_wav_.Resize(0); - } + void Reset() { remained_wav_.Resize(0); } - int Dim() { - return computer_.Dim(); - } + int Dim() { return computer_.Dim(); } private: - bool Compute(const kaldi::Vector& waves, + bool Compute(const kaldi::Vector& waves, kaldi::Vector* feats); Options opts_; kaldi::FeatureWindowFunction window_function_; @@ -49,4 +45,3 @@ class StreamingFeatureTpl { } // namespace ppspeech #include "feature_common_inl.h" - diff --git a/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common_inl.h b/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common_inl.h index db45b26a2..809cc60dd 100644 --- a/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common_inl.h +++ b/paddlespeech/audio/src/pybind/kaldi_frontend/feature_common_inl.h @@ -17,16 +17,15 @@ namespace paddleaudio { template -StreamingFeatureTpl::StreamingFeatureTpl( - const Options& opts) - : opts_(opts), computer_(opts), - window_function_(opts.frame_opts) { - //window_function_(computer_.GetFrameOptions()) { the opt set to zero +StreamingFeatureTpl::StreamingFeatureTpl(const Options& opts) + : opts_(opts), computer_(opts), window_function_(opts.frame_opts) { + // window_function_(computer_.GetFrameOptions()) { the opt set to zero } template -bool StreamingFeatureTpl::ComputeFeature(const kaldi::VectorBase& wav, - kaldi::Vector* feats) { +bool StreamingFeatureTpl::ComputeFeature( + const kaldi::VectorBase& wav, + kaldi::Vector* feats) { // append remaned waves kaldi::int32 wav_len = wav.Dim(); if (wav_len == 0) return false; @@ -61,7 +60,7 @@ bool StreamingFeatureTpl::Compute( kaldi::int32 frame_length = frame_opts.WindowSize(); kaldi::int32 sample_rate = frame_opts.samp_freq; if (num_samples < frame_length) { - return false; + return false; } kaldi::int32 num_frames = kaldi::NumFrames(num_samples, frame_opts); diff --git a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.cc b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.cc index 21e71d8d0..5a367b12e 100644 --- a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.cc +++ b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.cc @@ -1,46 +1,45 @@ -#include #include +#include #include "kaldi_feature_wrapper.h" -namespace py=pybind11; +namespace py = pybind11; -bool InitFbank( - float samp_freq, // frame opts - float frame_shift_ms, - float frame_length_ms, - float dither, - float preemph_coeff, - bool remove_dc_offset, - std::string window_type, // e.g. Hamming window - bool round_to_power_of_two, - float blackman_coeff, - bool snip_edges, - bool allow_downsample, - bool allow_upsample, - int max_feature_vectors, - int num_bins, // mel opts - float low_freq, - float high_freq, - float vtln_low, - float vtln_high, - bool debug_mel, - bool htk_mode, - bool use_energy, // fbank opts - float energy_floor, - bool raw_energy, - bool htk_compat, - bool use_log_fbank, - bool use_power) { +bool InitFbank(float samp_freq, // frame opts + float frame_shift_ms, + float frame_length_ms, + float dither, + float preemph_coeff, + bool remove_dc_offset, + std::string window_type, // e.g. Hamming window + bool round_to_power_of_two, + float blackman_coeff, + bool snip_edges, + bool allow_downsample, + bool allow_upsample, + int max_feature_vectors, + int num_bins, // mel opts + float low_freq, + float high_freq, + float vtln_low, + float vtln_high, + bool debug_mel, + bool htk_mode, + bool use_energy, // fbank opts + float energy_floor, + bool raw_energy, + bool htk_compat, + bool use_log_fbank, + bool use_power) { kaldi::FbankOptions opts; - opts.frame_opts.samp_freq = samp_freq; // frame opts + opts.frame_opts.samp_freq = samp_freq; // frame opts opts.frame_opts.frame_shift_ms = frame_shift_ms; opts.frame_opts.frame_length_ms = frame_length_ms; opts.frame_opts.dither = dither; opts.frame_opts.preemph_coeff = preemph_coeff; opts.frame_opts.remove_dc_offset = remove_dc_offset; - opts.frame_opts.window_type = window_type; + opts.frame_opts.window_type = window_type; opts.frame_opts.round_to_power_of_two = round_to_power_of_two; opts.frame_opts.blackman_coeff = blackman_coeff; opts.frame_opts.snip_edges = snip_edges; @@ -48,7 +47,7 @@ bool InitFbank( opts.frame_opts.allow_upsample = allow_upsample; opts.frame_opts.max_feature_vectors = max_feature_vectors; - opts.mel_opts.num_bins = num_bins; // mel opts + opts.mel_opts.num_bins = num_bins; // mel opts opts.mel_opts.low_freq = low_freq; opts.mel_opts.high_freq = high_freq; opts.mel_opts.vtln_low = vtln_low; @@ -56,7 +55,7 @@ bool InitFbank( opts.mel_opts.debug_mel = debug_mel; opts.mel_opts.htk_mode = htk_mode; - opts.use_energy = use_energy; // fbank opts + opts.use_energy = use_energy; // fbank opts opts.energy_floor = energy_floor; opts.raw_energy = raw_energy; opts.htk_compat = htk_compat; @@ -67,71 +66,71 @@ bool InitFbank( } py::array_t ComputeFbankStreaming(const py::array_t& wav) { - return paddleaudio::KaldiFeatureWrapper::GetInstance()->ComputeFbank(wav); + return paddleaudio::KaldiFeatureWrapper::GetInstance()->ComputeFbank(wav); } py::array_t ComputeFbank( - float samp_freq, // frame opts + float samp_freq, // frame opts float frame_shift_ms, float frame_length_ms, float dither, float preemph_coeff, bool remove_dc_offset, - std::string window_type, // e.g. Hamming window + std::string window_type, // e.g. Hamming window bool round_to_power_of_two, float blackman_coeff, bool snip_edges, bool allow_downsample, bool allow_upsample, int max_feature_vectors, - int num_bins, // mel opts + int num_bins, // mel opts float low_freq, float high_freq, float vtln_low, float vtln_high, bool debug_mel, bool htk_mode, - bool use_energy, // fbank opts + bool use_energy, // fbank opts float energy_floor, bool raw_energy, bool htk_compat, bool use_log_fbank, - bool use_power, + bool use_power, const py::array_t& wav) { - InitFbank(samp_freq, // frame opts - frame_shift_ms, - frame_length_ms, - dither, - preemph_coeff, - remove_dc_offset, - window_type, // e.g. Hamming window - round_to_power_of_two, - blackman_coeff, - snip_edges, - allow_downsample, - allow_upsample, - max_feature_vectors, - num_bins, // mel opts - low_freq, - high_freq, - vtln_low, - vtln_high, - debug_mel, - htk_mode, - use_energy, // fbank opts - energy_floor, - raw_energy, - htk_compat, - use_log_fbank, - use_power); - py::array_t result = ComputeFbankStreaming(wav); - paddleaudio::KaldiFeatureWrapper::GetInstance()->ResetFbank(); - return result; + InitFbank(samp_freq, // frame opts + frame_shift_ms, + frame_length_ms, + dither, + preemph_coeff, + remove_dc_offset, + window_type, // e.g. Hamming window + round_to_power_of_two, + blackman_coeff, + snip_edges, + allow_downsample, + allow_upsample, + max_feature_vectors, + num_bins, // mel opts + low_freq, + high_freq, + vtln_low, + vtln_high, + debug_mel, + htk_mode, + use_energy, // fbank opts + energy_floor, + raw_energy, + htk_compat, + use_log_fbank, + use_power); + py::array_t result = ComputeFbankStreaming(wav); + paddleaudio::KaldiFeatureWrapper::GetInstance()->ResetFbank(); + return result; } void ResetFbank() { - paddleaudio::KaldiFeatureWrapper::GetInstance()->ResetFbank(); + paddleaudio::KaldiFeatureWrapper::GetInstance()->ResetFbank(); } PYBIND11_MODULE(kaldi_featurepy, m) { @@ -139,5 +138,7 @@ PYBIND11_MODULE(kaldi_featurepy, m) { m.def("InitFbank", &InitFbank, "init fbank"); m.def("ResetFbank", &ResetFbank, "reset fbank"); m.def("ComputeFbank", &ComputeFbank, "compute fbank"); - m.def("ComputeFbankStreaming", &ComputeFbankStreaming, "compute fbank streaming"); + m.def("ComputeFbankStreaming", + &ComputeFbankStreaming, + "compute fbank streaming"); } diff --git a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.h b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.h index b24416b18..adb49222b 100644 --- a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.h +++ b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature.h @@ -1,60 +1,59 @@ -#include #include +#include #include "kaldi_feature_wrapper.h" -namespace py=pybind11; +namespace py = pybind11; -bool InitFbank( - float samp_freq, // frame opts - float frame_shift_ms, - float frame_length_ms, - float dither, - float preemph_coeff, - bool remove_dc_offset, - std::string window_type, // e.g. Hamming window - bool round_to_power_of_two, - float blackman_coeff, - bool snip_edges, - bool allow_downsample, - bool allow_upsample, - int max_feature_vectors, - int num_bins, // mel opts - float low_freq, - float high_freq, - float vtln_low, - float vtln_high, - bool debug_mel, - bool htk_mode, - bool use_energy, // fbank opts - float energy_floor, - bool raw_energy, - bool htk_compat, - bool use_log_fbank, - bool use_power); +bool InitFbank(float samp_freq, // frame opts + float frame_shift_ms, + float frame_length_ms, + float dither, + float preemph_coeff, + bool remove_dc_offset, + std::string window_type, // e.g. Hamming window + bool round_to_power_of_two, + float blackman_coeff, + bool snip_edges, + bool allow_downsample, + bool allow_upsample, + int max_feature_vectors, + int num_bins, // mel opts + float low_freq, + float high_freq, + float vtln_low, + float vtln_high, + bool debug_mel, + bool htk_mode, + bool use_energy, // fbank opts + float energy_floor, + bool raw_energy, + bool htk_compat, + bool use_log_fbank, + bool use_power); py::array_t ComputeFbank( - float samp_freq, // frame opts + float samp_freq, // frame opts float frame_shift_ms, float frame_length_ms, float dither, float preemph_coeff, bool remove_dc_offset, - std::string window_type, // e.g. Hamming window + std::string window_type, // e.g. Hamming window bool round_to_power_of_two, kaldi::BaseFloat blackman_coeff, bool snip_edges, bool allow_downsample, bool allow_upsample, int max_feature_vectors, - int num_bins, // mel opts + int num_bins, // mel opts float low_freq, float high_freq, float vtln_low, float vtln_high, bool debug_mel, bool htk_mode, - bool use_energy, // fbank opts + bool use_energy, // fbank opts float energy_floor, bool raw_energy, bool htk_compat, diff --git a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.cc b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.cc index a965fd6a2..ce5ae412c 100644 --- a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.cc +++ b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.cc @@ -8,17 +8,18 @@ KaldiFeatureWrapper* KaldiFeatureWrapper::GetInstance() { } bool KaldiFeatureWrapper::InitFbank(kaldi::FbankOptions opts) { - fbank_.reset(new Fbank(opts)); - return true; + fbank_.reset(new Fbank(opts)); + return true; } -py::array_t KaldiFeatureWrapper::ComputeFbank(const py::array_t wav) { +py::array_t KaldiFeatureWrapper::ComputeFbank( + const py::array_t wav) { py::buffer_info info = wav.request(); kaldi::Vector input_wav(info.size); double* wav_ptr = (double*)info.ptr; for (int idx = 0; idx < info.size; ++idx) { - input_wav(idx) = *wav_ptr; - wav_ptr++; + input_wav(idx) = *wav_ptr; + wav_ptr++; } @@ -28,8 +29,8 @@ py::array_t KaldiFeatureWrapper::ComputeFbank(const py::array_t auto result = py::array_t(feats.Dim()); py::buffer_info xs = result.request(); for (int idx = 0; idx < 10; ++idx) { - float val = feats(idx); - std::cout << val << " "; + float val = feats(idx); + std::cout << val << " "; } std::cout << std::endl; double* res_ptr = (double*)xs.ptr; @@ -38,20 +39,21 @@ py::array_t KaldiFeatureWrapper::ComputeFbank(const py::array_t res_ptr++; } - return result.reshape({ feats.Dim() / Dim(), Dim()}); -/* - py::buffer_info info = wav.request(); - std::cout << info.size << std::endl; - auto result = py::array_t(info.size); - //kaldi::Vector input_wav(info.size); - kaldi::Vector input_wav(info.size); - py::buffer_info info_re = result.request(); - - memcpy(input_wav.Data(), (double*)info.ptr, wav.nbytes()); - memcpy((double*)info_re.ptr, input_wav.Data(), input_wav.Dim()* sizeof(double)); - return result; -*/ + return result.reshape({feats.Dim() / Dim(), Dim()}); + /* + py::buffer_info info = wav.request(); + std::cout << info.size << std::endl; + auto result = py::array_t(info.size); + //kaldi::Vector input_wav(info.size); + kaldi::Vector input_wav(info.size); + py::buffer_info info_re = result.request(); + + memcpy(input_wav.Data(), (double*)info.ptr, wav.nbytes()); + memcpy((double*)info_re.ptr, input_wav.Data(), input_wav.Dim()* + sizeof(double)); + return result; + */ } - -} // namespace paddleaudio + +} // namespace paddleaudio diff --git a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.h b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.h index 06e49093d..d025bf398 100644 --- a/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.h +++ b/paddlespeech/audio/src/pybind/kaldi_frontend/kaldi_feature_wrapper.h @@ -1,7 +1,7 @@ #include "base/kaldi-common.h" -#include "feature_common.h" #include "feat/feature-fbank.h" +#include "feature_common.h" #pragma once @@ -14,12 +14,8 @@ class KaldiFeatureWrapper { static KaldiFeatureWrapper* GetInstance(); bool InitFbank(kaldi::FbankOptions opts); py::array_t ComputeFbank(const py::array_t wav); - int Dim() { - return fbank_->Dim(); - } - void ResetFbank() { - fbank_->Reset(); - } + int Dim() { return fbank_->Dim(); } + void ResetFbank() { fbank_->Reset(); } private: std::unique_ptr fbank_; diff --git a/paddlespeech/audio/src/pybind/pybind.cpp b/paddlespeech/audio/src/pybind/pybind.cpp index f82e48c43..389601365 100644 --- a/paddlespeech/audio/src/pybind/pybind.cpp +++ b/paddlespeech/audio/src/pybind/pybind.cpp @@ -1,11 +1,13 @@ -//Copyright (c) 2017 Facebook Inc. (Soumith Chintala), -//All rights reserved. +// Copyright (c) 2017 Facebook Inc. (Soumith Chintala), +// All rights reserved. #include "paddlespeech/audio/src/pybind/sox/io.h" PYBIND11_MODULE(_paddleaudio, m) { - m.def("get_info_file", &paddleaudio::sox_io::get_info_file, - "Get metadata of audio file."); - m.def("get_info_fileobj", &paddleaudio::sox_io::get_info_fileobj, - "Get metadata of audio in file object."); + m.def("get_info_file", + &paddleaudio::sox_io::get_info_file, + "Get metadata of audio file."); + m.def("get_info_fileobj", + &paddleaudio::sox_io::get_info_fileobj, + "Get metadata of audio in file object."); } \ No newline at end of file diff --git a/paddlespeech/audio/src/pybind/sox/io.cpp b/paddlespeech/audio/src/pybind/sox/io.cpp index 687c617ca..d5bd8fd65 100644 --- a/paddlespeech/audio/src/pybind/sox/io.cpp +++ b/paddlespeech/audio/src/pybind/sox/io.cpp @@ -1,5 +1,5 @@ -//Copyright (c) 2017 Facebook Inc. (Soumith Chintala), -//All rights reserved. +// Copyright (c) 2017 Facebook Inc. (Soumith Chintala), +// All rights reserved. #include "paddlespeech/audio/src/pybind/sox/io.h" #include "paddlespeech/audio/src/pybind/sox/utils.h" @@ -11,51 +11,54 @@ namespace sox_io { auto get_info_file(const std::string &path, const std::string &format) -> std::tuple { - SoxFormat sf(sox_open_read(path.data(), - /*signal=*/nullptr, - /*encoding=*/nullptr, - /*filetype=*/format.empty() ? nullptr : format.data())); - - - validate_input_file(sf, path); - - return std::make_tuple( - static_cast(sf->signal.rate), - static_cast(sf->signal.length / sf->signal.channels), - static_cast(sf->signal.channels), - static_cast(sf->encoding.bits_per_sample), - get_encoding(sf->encoding.encoding)); + SoxFormat sf( + sox_open_read(path.data(), + /*signal=*/nullptr, + /*encoding=*/nullptr, + /*filetype=*/format.empty() ? nullptr : format.data())); + + + validate_input_file(sf, path); + + return std::make_tuple( + static_cast(sf->signal.rate), + static_cast(sf->signal.length / sf->signal.channels), + static_cast(sf->signal.channels), + static_cast(sf->encoding.bits_per_sample), + get_encoding(sf->encoding.encoding)); } auto get_info_fileobj(py::object fileobj, const std::string &format) -> std::tuple { - const auto capacity = [&]() { - const auto bufsiz = get_buffer_size(); - const int64_t kDefaultCapacityInBytes = 4096; - return (bufsiz > kDefaultCapacityInBytes) ? bufsiz - : kDefaultCapacityInBytes; - }(); - std::string buffer(capacity, '\0'); - auto *buf = const_cast(buffer.data()); - auto num_read = read_fileobj(&fileobj, capacity, buf); - // If the file is shorter than 256, then libsox cannot read the header. - auto buf_size = (num_read > 256) ? num_read : 256; - - SoxFormat sf(sox_open_mem_read(buf, buf_size, - /*signal=*/nullptr, - /*encoding=*/nullptr, - /*filetype=*/format.empty() ? nullptr : format.data())); - - // In case of streamed data, length can be 0 - validate_input_memfile(sf); - - return std::make_tuple( - static_cast(sf->signal.rate), - static_cast(sf->signal.length / sf->signal.channels), - static_cast(sf->signal.channels), - static_cast(sf->encoding.bits_per_sample), - get_encoding(sf->encoding.encoding)); + const auto capacity = [&]() { + const auto bufsiz = get_buffer_size(); + const int64_t kDefaultCapacityInBytes = 4096; + return (bufsiz > kDefaultCapacityInBytes) ? bufsiz + : kDefaultCapacityInBytes; + }(); + std::string buffer(capacity, '\0'); + auto *buf = const_cast(buffer.data()); + auto num_read = read_fileobj(&fileobj, capacity, buf); + // If the file is shorter than 256, then libsox cannot read the header. + auto buf_size = (num_read > 256) ? num_read : 256; + + SoxFormat sf(sox_open_mem_read( + buf, + buf_size, + /*signal=*/nullptr, + /*encoding=*/nullptr, + /*filetype=*/format.empty() ? nullptr : format.data())); + + // In case of streamed data, length can be 0 + validate_input_memfile(sf); + + return std::make_tuple( + static_cast(sf->signal.rate), + static_cast(sf->signal.length / sf->signal.channels), + static_cast(sf->signal.channels), + static_cast(sf->encoding.bits_per_sample), + get_encoding(sf->encoding.encoding)); } -} // namespace paddleaudio -} // namespace sox_io +} // namespace paddleaudio +} // namespace sox_io diff --git a/paddlespeech/audio/src/pybind/sox/io.h b/paddlespeech/audio/src/pybind/sox/io.h index 9a7cc85cf..13381c68d 100644 --- a/paddlespeech/audio/src/pybind/sox/io.h +++ b/paddlespeech/audio/src/pybind/sox/io.h @@ -1,5 +1,5 @@ -//Copyright (c) 2017 Facebook Inc. (Soumith Chintala), -//All rights reserved. +// Copyright (c) 2017 Facebook Inc. (Soumith Chintala), +// All rights reserved. #ifndef PADDLEAUDIO_PYBIND_SOX_IO_H #define PADDLEAUDIO_PYBIND_SOX_IO_H @@ -15,7 +15,7 @@ auto get_info_file(const std::string &path, const std::string &format) auto get_info_fileobj(py::object fileobj, const std::string &format) -> std::tuple; -} // namespace paddleaudio -} // namespace sox_io +} // namespace paddleaudio +} // namespace sox_io #endif diff --git a/paddlespeech/audio/src/pybind/sox/utils.cpp b/paddlespeech/audio/src/pybind/sox/utils.cpp index 34e575a50..53a3cbe41 100644 --- a/paddlespeech/audio/src/pybind/sox/utils.cpp +++ b/paddlespeech/audio/src/pybind/sox/utils.cpp @@ -1,5 +1,5 @@ -//Copyright (c) 2017 Facebook Inc. (Soumith Chintala), -//All rights reserved. +// Copyright (c) 2017 Facebook Inc. (Soumith Chintala), +// All rights reserved. #include "paddlespeech/audio/src/pybind/sox/utils.h" @@ -15,86 +15,87 @@ sox_format_t *SoxFormat::operator->() const noexcept { return fd_; } SoxFormat::operator sox_format_t *() const noexcept { return fd_; } void SoxFormat::close() { - if (fd_ != nullptr) { - sox_close(fd_); - fd_ = nullptr; - } + if (fd_ != nullptr) { + sox_close(fd_); + fd_ = nullptr; + } } auto read_fileobj(py::object *fileobj, const uint64_t size, char *buffer) -> uint64_t { - uint64_t num_read = 0; - while (num_read < size) { - auto request = size - num_read; - auto chunk = static_cast( - static_cast(fileobj->attr("read")(request))); - auto chunk_len = chunk.length(); - if (chunk_len == 0) { - break; - } - if (chunk_len > request) { - std::ostringstream message; - message << "Requested up to " << request << " bytes but, " - << "received " << chunk_len << " bytes. " - << "The given object does not confirm to read protocol of file " - "object."; - throw std::runtime_error(message.str()); + uint64_t num_read = 0; + while (num_read < size) { + auto request = size - num_read; + auto chunk = static_cast( + static_cast(fileobj->attr("read")(request))); + auto chunk_len = chunk.length(); + if (chunk_len == 0) { + break; + } + if (chunk_len > request) { + std::ostringstream message; + message + << "Requested up to " << request << " bytes but, " + << "received " << chunk_len << " bytes. " + << "The given object does not confirm to read protocol of file " + "object."; + throw std::runtime_error(message.str()); + } + memcpy(buffer, chunk.data(), chunk_len); + buffer += chunk_len; + num_read += chunk_len; } - memcpy(buffer, chunk.data(), chunk_len); - buffer += chunk_len; - num_read += chunk_len; - } - return num_read; + return num_read; } int64_t get_buffer_size() { return sox_get_globals()->bufsiz; } void validate_input_file(const SoxFormat &sf, const std::string &path) { - if (static_cast(sf) == nullptr) { - throw std::runtime_error("Error loading audio file: failed to open file " + - path); - } - if (sf->encoding.encoding == SOX_ENCODING_UNKNOWN) { - throw std::runtime_error("Error loading audio file: unknown encoding."); - } + if (static_cast(sf) == nullptr) { + throw std::runtime_error( + "Error loading audio file: failed to open file " + path); + } + if (sf->encoding.encoding == SOX_ENCODING_UNKNOWN) { + throw std::runtime_error("Error loading audio file: unknown encoding."); + } } void validate_input_memfile(const SoxFormat &sf) { - return validate_input_file(sf, ""); + return validate_input_file(sf, ""); } std::string get_encoding(sox_encoding_t encoding) { - switch (encoding) { - case SOX_ENCODING_UNKNOWN: - return "UNKNOWN"; - case SOX_ENCODING_SIGN2: - return "PCM_S"; - case SOX_ENCODING_UNSIGNED: - return "PCM_U"; - case SOX_ENCODING_FLOAT: - return "PCM_F"; - case SOX_ENCODING_FLAC: - return "FLAC"; - case SOX_ENCODING_ULAW: - return "ULAW"; - case SOX_ENCODING_ALAW: - return "ALAW"; - case SOX_ENCODING_MP3: - return "MP3"; - case SOX_ENCODING_VORBIS: - return "VORBIS"; - case SOX_ENCODING_AMR_WB: - return "AMR_WB"; - case SOX_ENCODING_AMR_NB: - return "AMR_NB"; - case SOX_ENCODING_OPUS: - return "OPUS"; - case SOX_ENCODING_GSM: - return "GSM"; - default: - return "UNKNOWN"; - } + switch (encoding) { + case SOX_ENCODING_UNKNOWN: + return "UNKNOWN"; + case SOX_ENCODING_SIGN2: + return "PCM_S"; + case SOX_ENCODING_UNSIGNED: + return "PCM_U"; + case SOX_ENCODING_FLOAT: + return "PCM_F"; + case SOX_ENCODING_FLAC: + return "FLAC"; + case SOX_ENCODING_ULAW: + return "ULAW"; + case SOX_ENCODING_ALAW: + return "ALAW"; + case SOX_ENCODING_MP3: + return "MP3"; + case SOX_ENCODING_VORBIS: + return "VORBIS"; + case SOX_ENCODING_AMR_WB: + return "AMR_WB"; + case SOX_ENCODING_AMR_NB: + return "AMR_NB"; + case SOX_ENCODING_OPUS: + return "OPUS"; + case SOX_ENCODING_GSM: + return "GSM"; + default: + return "UNKNOWN"; + } } -} // namespace paddleaudio -} // namespace sox_utils +} // namespace paddleaudio +} // namespace sox_utils diff --git a/paddlespeech/audio/src/pybind/sox/utils.h b/paddlespeech/audio/src/pybind/sox/utils.h index 4c2fabc19..9445fe31f 100644 --- a/paddlespeech/audio/src/pybind/sox/utils.h +++ b/paddlespeech/audio/src/pybind/sox/utils.h @@ -1,5 +1,5 @@ -//Copyright (c) 2017 Facebook Inc. (Soumith Chintala), -//All rights reserved. +// Copyright (c) 2017 Facebook Inc. (Soumith Chintala), +// All rights reserved. #ifndef PADDLEAUDIO_PYBIND_SOX_UTILS_H #define PADDLEAUDIO_PYBIND_SOX_UTILS_H @@ -14,19 +14,19 @@ namespace sox_utils { /// helper class to automatically close sox_format_t* struct SoxFormat { - explicit SoxFormat(sox_format_t *fd) noexcept; - SoxFormat(const SoxFormat &other) = delete; - SoxFormat(SoxFormat &&other) = delete; - SoxFormat &operator=(const SoxFormat &other) = delete; - SoxFormat &operator=(SoxFormat &&other) = delete; - ~SoxFormat(); - sox_format_t *operator->() const noexcept; - operator sox_format_t *() const noexcept; - - void close(); - -private: - sox_format_t *fd_; + explicit SoxFormat(sox_format_t *fd) noexcept; + SoxFormat(const SoxFormat &other) = delete; + SoxFormat(SoxFormat &&other) = delete; + SoxFormat &operator=(const SoxFormat &other) = delete; + SoxFormat &operator=(SoxFormat &&other) = delete; + ~SoxFormat(); + sox_format_t *operator->() const noexcept; + operator sox_format_t *() const noexcept; + + void close(); + + private: + sox_format_t *fd_; }; auto read_fileobj(py::object *fileobj, uint64_t size, char *buffer) -> uint64_t; @@ -39,7 +39,7 @@ void validate_input_memfile(const SoxFormat &sf); std::string get_encoding(sox_encoding_t encoding); -} // namespace paddleaudio -} // namespace sox_utils +} // namespace paddleaudio +} // namespace sox_utils #endif diff --git a/paddlespeech/audio/src/sox/io.cpp b/paddlespeech/audio/src/sox/io.cpp index 3f7d0f4ff..f4dcce475 100644 --- a/paddlespeech/audio/src/sox/io.cpp +++ b/paddlespeech/audio/src/sox/io.cpp @@ -11,54 +11,53 @@ namespace paddleaudio { namespace sox_io { tl::optional get_info_file( - const std::string& path, - const tl::optional& format) { - SoxFormat sf(sox_open_read( - path.c_str(), - /*signal=*/nullptr, - /*encoding=*/nullptr, - /*filetype=*/format.has_value() ? format.value().c_str() : nullptr)); - - if (static_cast(sf) == nullptr || - sf->encoding.encoding == SOX_ENCODING_UNKNOWN) { - return {}; - } - - return std::forward_as_tuple( - static_cast(sf->signal.rate), - static_cast(sf->signal.length / sf->signal.channels), - static_cast(sf->signal.channels), - static_cast(sf->encoding.bits_per_sample), - get_encoding(sf->encoding.encoding)); + const std::string& path, const tl::optional& format) { + SoxFormat sf(sox_open_read( + path.c_str(), + /*signal=*/nullptr, + /*encoding=*/nullptr, + /*filetype=*/format.has_value() ? format.value().c_str() : nullptr)); + + if (static_cast(sf) == nullptr || + sf->encoding.encoding == SOX_ENCODING_UNKNOWN) { + return {}; + } + + return std::forward_as_tuple( + static_cast(sf->signal.rate), + static_cast(sf->signal.length / sf->signal.channels), + static_cast(sf->signal.channels), + static_cast(sf->encoding.bits_per_sample), + get_encoding(sf->encoding.encoding)); } std::vector> get_effects( const tl::optional& frame_offset, const tl::optional& num_frames) { - const auto offset = frame_offset.value_or(0); - if (offset < 0) { - throw std::runtime_error( - "Invalid argument: frame_offset must be non-negative."); - } - const auto frames = num_frames.value_or(-1); - if (frames == 0 || frames < -1) { - throw std::runtime_error( - "Invalid argument: num_frames must be -1 or greater than 0."); - } - - std::vector> effects; - if (frames != -1) { - std::ostringstream os_offset, os_frames; - os_offset << offset << "s"; - os_frames << "+" << frames << "s"; - effects.emplace_back( - std::vector{"trim", os_offset.str(), os_frames.str()}); - } else if (offset != 0) { - std::ostringstream os_offset; - os_offset << offset << "s"; - effects.emplace_back(std::vector{"trim", os_offset.str()}); - } - return effects; + const auto offset = frame_offset.value_or(0); + if (offset < 0) { + throw std::runtime_error( + "Invalid argument: frame_offset must be non-negative."); + } + const auto frames = num_frames.value_or(-1); + if (frames == 0 || frames < -1) { + throw std::runtime_error( + "Invalid argument: num_frames must be -1 or greater than 0."); + } + + std::vector> effects; + if (frames != -1) { + std::ostringstream os_offset, os_frames; + os_offset << offset << "s"; + os_frames << "+" << frames << "s"; + effects.emplace_back( + std::vector{"trim", os_offset.str(), os_frames.str()}); + } else if (offset != 0) { + std::ostringstream os_offset; + os_offset << offset << "s"; + effects.emplace_back(std::vector{"trim", os_offset.str()}); + } + return effects; } tl::optional> load_audio_file( @@ -68,79 +67,73 @@ tl::optional> load_audio_file( tl::optional normalize, tl::optional channels_first, const tl::optional& format) { - auto effects = get_effects(frame_offset, num_frames); - return paddleaudio::sox_effects::apply_effects_file( - path, effects, normalize, channels_first, format); + auto effects = get_effects(frame_offset, num_frames); + return paddleaudio::sox_effects::apply_effects_file( + path, effects, normalize, channels_first, format); } -void save_audio_file( - const std::string& path, - torch::Tensor tensor, - int64_t sample_rate, - bool channels_first, - tl::optional compression, - tl::optional format, - tl::optional encoding, - tl::optional bits_per_sample) { - validate_input_tensor(tensor); - - const auto filetype = [&]() { - if (format.has_value()) - return format.value(); - return get_filetype(path); - }(); - - if (filetype == "amr-nb") { - const auto num_channels = tensor.size(channels_first ? 0 : 1); - TORCH_CHECK( - num_channels == 1, "amr-nb format only supports single channel audio."); - } else if (filetype == "htk") { - const auto num_channels = tensor.size(channels_first ? 0 : 1); - TORCH_CHECK( - num_channels == 1, "htk format only supports single channel audio."); - } else if (filetype == "gsm") { - const auto num_channels = tensor.size(channels_first ? 0 : 1); - TORCH_CHECK( - num_channels == 1, "gsm format only supports single channel audio."); - TORCH_CHECK( - sample_rate == 8000, - "gsm format only supports a sampling rate of 8kHz."); - } - const auto signal_info = - get_signalinfo(&tensor, sample_rate, filetype, channels_first); - const auto encoding_info = get_encodinginfo_for_save( - filetype, tensor.dtype(), compression, encoding, bits_per_sample); - - SoxFormat sf(sox_open_write( - path.c_str(), - &signal_info, - &encoding_info, - /*filetype=*/filetype.c_str(), - /*oob=*/nullptr, - /*overwrite_permitted=*/nullptr)); - - if (static_cast(sf) == nullptr) { - throw std::runtime_error( - "Error saving audio file: failed to open file " + path); - } - - paddleaudio::sox_effects_chain::SoxEffectsChain chain( - /*input_encoding=*/get_tensor_encodinginfo(tensor.dtype()), - /*output_encoding=*/sf->encoding); - chain.addInputTensor(&tensor, sample_rate, channels_first); - chain.addOutputFile(sf); - chain.run(); +void save_audio_file(const std::string& path, + torch::Tensor tensor, + int64_t sample_rate, + bool channels_first, + tl::optional compression, + tl::optional format, + tl::optional encoding, + tl::optional bits_per_sample) { + validate_input_tensor(tensor); + + const auto filetype = [&]() { + if (format.has_value()) return format.value(); + return get_filetype(path); + }(); + + if (filetype == "amr-nb") { + const auto num_channels = tensor.size(channels_first ? 0 : 1); + TORCH_CHECK(num_channels == 1, + "amr-nb format only supports single channel audio."); + } else if (filetype == "htk") { + const auto num_channels = tensor.size(channels_first ? 0 : 1); + TORCH_CHECK(num_channels == 1, + "htk format only supports single channel audio."); + } else if (filetype == "gsm") { + const auto num_channels = tensor.size(channels_first ? 0 : 1); + TORCH_CHECK(num_channels == 1, + "gsm format only supports single channel audio."); + TORCH_CHECK(sample_rate == 8000, + "gsm format only supports a sampling rate of 8kHz."); + } + const auto signal_info = + get_signalinfo(&tensor, sample_rate, filetype, channels_first); + const auto encoding_info = get_encodinginfo_for_save( + filetype, tensor.dtype(), compression, encoding, bits_per_sample); + + SoxFormat sf(sox_open_write(path.c_str(), + &signal_info, + &encoding_info, + /*filetype=*/filetype.c_str(), + /*oob=*/nullptr, + /*overwrite_permitted=*/nullptr)); + + if (static_cast(sf) == nullptr) { + throw std::runtime_error( + "Error saving audio file: failed to open file " + path); + } + + paddleaudio::sox_effects_chain::SoxEffectsChain chain( + /*input_encoding=*/get_tensor_encodinginfo(tensor.dtype()), + /*output_encoding=*/sf->encoding); + chain.addInputTensor(&tensor, sample_rate, channels_first); + chain.addOutputFile(sf); + chain.run(); } TORCH_LIBRARY_FRAGMENT(paddleaudio, m) { - m.def("paddleaudio::sox_io_get_info", &paddleaudio::sox_io::get_info_file); - m.def( - "paddleaudio::sox_io_load_audio_file", - &paddleaudio::sox_io::load_audio_file); - m.def( - "paddleaudio::sox_io_save_audio_file", - &paddleaudio::sox_io::save_audio_file); + m.def("paddleaudio::sox_io_get_info", &paddleaudio::sox_io::get_info_file); + m.def("paddleaudio::sox_io_load_audio_file", + &paddleaudio::sox_io::load_audio_file); + m.def("paddleaudio::sox_io_save_audio_file", + &paddleaudio::sox_io::save_audio_file); } -} // namespace sox_io -} // namespace paddleaudio \ No newline at end of file +} // namespace sox_io +} // namespace paddleaudio \ No newline at end of file diff --git a/paddlespeech/audio/src/sox/io.h b/paddlespeech/audio/src/sox/io.h index 405f4637c..4464e6655 100644 --- a/paddlespeech/audio/src/sox/io.h +++ b/paddlespeech/audio/src/sox/io.h @@ -1,6 +1,6 @@ -//Copyright (c) 2017 Facebook Inc. (Soumith Chintala), -//All rights reserved. +// Copyright (c) 2017 Facebook Inc. (Soumith Chintala), +// All rights reserved. #ifndef PADDLEAUDIO_SOX_IO_H #define PADDLEAUDIO_SOX_IO_H @@ -11,17 +11,15 @@ namespace paddleaudio { namespace sox_io { -auto get_effects( - const tl::optional& frame_offset, - const tl::optional& num_frames) +auto get_effects(const tl::optional& frame_offset, + const tl::optional& num_frames) -> std::vector>; using MetaDataTuple = std::tuple; tl::optional get_info_file( - const std::string& path, - const tl::optional& format); + const std::string& path, const tl::optional& format); tl::optional> load_audio_file( const std::string& path, @@ -31,17 +29,16 @@ tl::optional> load_audio_file( tl::optional channels_first, const tl::optional& format); -void save_audio_file( - const std::string& path, - torch::Tensor tensor, - int64_t sample_rate, - bool channels_first, - tl::optional compression, - tl::optional format, - tl::optional encoding, - tl::optional bits_per_sample); - -} // namespace sox_io -} // namespace paddleaudio +void save_audio_file(const std::string& path, + torch::Tensor tensor, + int64_t sample_rate, + bool channels_first, + tl::optional compression, + tl::optional format, + tl::optional encoding, + tl::optional bits_per_sample); + +} // namespace sox_io +} // namespace paddleaudio #endif \ No newline at end of file diff --git a/paddlespeech/audio/src/utils.cpp b/paddlespeech/audio/src/utils.cpp index e4c958b2d..d9d3575f2 100644 --- a/paddlespeech/audio/src/utils.cpp +++ b/paddlespeech/audio/src/utils.cpp @@ -4,17 +4,17 @@ namespace { bool is_sox_available() { #ifdef INCLUDE_SOX - return true; + return true; #else - return false; + return false; #endif } bool is_kaldi_available() { #ifdef INCLUDE_KALDI - return true; + return true; #else - return false; + return false; #endif } @@ -22,12 +22,12 @@ bool is_kaldi_available() { // not the runtime availability. bool is_ffmpeg_available() { #ifdef USE_FFMPEG - return true; + return true; #else - return false; + return false; #endif } -} // namespace +} // namespace -} // namespace paddleaudio \ No newline at end of file +} // namespace paddleaudio \ No newline at end of file diff --git a/setup.py b/setup.py index 4d9b4791e..70456e3a4 100644 --- a/setup.py +++ b/setup.py @@ -18,9 +18,9 @@ import os import subprocess as sp import sys from pathlib import Path -from typing import Union -from typing import Tuple from typing import List +from typing import Tuple +from typing import Union import distutils.command.clean from setuptools import Command @@ -38,43 +38,13 @@ VERSION = '0.0.0' COMMITID = 'none' base = [ - "editdistance", - "g2p_en", - "g2pM", - "h5py", - "inflect", - "jieba", - "jsonlines", - "kaldiio", - "librosa==0.8.1", - "loguru", - "matplotlib", - "nara_wpe", - "onnxruntime", - "pandas", - "paddlenlp", - "paddlespeech_feat", - "praatio==5.0.0", - "pypinyin", - "pypinyin-dict", - "python-dateutil", - "pyworld", - "resampy==0.2.2", - "sacrebleu", - "scipy", - "sentencepiece~=0.1.96", - "soundfile~=0.10", - "textgrid", - "timer", - "tqdm", - "typeguard", - "visualdl", - "webrtcvad", - "yacs~=0.1.8", - "prettytable", - "zhon", - 'colorlog', - 'pathos == 0.2.8' + "editdistance", "g2p_en", "g2pM", "h5py", "inflect", "jieba", "jsonlines", + "kaldiio", "librosa==0.8.1", "loguru", "matplotlib", "nara_wpe", + "onnxruntime", "pandas", "paddlenlp", "paddlespeech_feat", "praatio==5.0.0", + "pypinyin", "pypinyin-dict", "python-dateutil", "pyworld", "resampy==0.2.2", + "sacrebleu", "scipy", "sentencepiece~=0.1.96", "soundfile~=0.10", + "textgrid", "timer", "tqdm", "typeguard", "visualdl", "webrtcvad", + "yacs~=0.1.8", "prettytable", "zhon", 'colorlog', 'pathos == 0.2.8' ] server = [ @@ -264,8 +234,9 @@ class clean(distutils.command.clean.clean): print(f"removing '{path}' (and everything under it)") shutil.rmtree(str(path), ignore_errors=True) + def main(): - sha = check_output(["git", "rev-parse", "HEAD"]) # commit id + sha = check_output(["git", "rev-parse", "HEAD"]) # commit id branch = check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]) tag = check_output(["git", "describe", "--tags", "--exact-match", "@"]) print("-- Git branch:", branch) @@ -319,7 +290,8 @@ def main(): requirements["develop"], 'doc': [ "sphinx", "sphinx-rtd-theme", "numpydoc", "myst_parser", - "recommonmark>=0.5.0", "sphinx-markdown-tables", "sphinx-autobuild" + "recommonmark>=0.5.0", "sphinx-markdown-tables", + "sphinx-autobuild" ], 'test': ['nose', 'torchaudio==0.10.2'], }, @@ -358,5 +330,6 @@ def main(): setup(**setup_info) + if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/speechx/examples/ds2_ol/onnx/local/onnx_infer_shape.py b/speechx/examples/ds2_ol/onnx/local/onnx_infer_shape.py index 2d364c252..088d3ef5c 100755 --- a/speechx/examples/ds2_ol/onnx/local/onnx_infer_shape.py +++ b/speechx/examples/ds2_ol/onnx/local/onnx_infer_shape.py @@ -494,6 +494,11 @@ class SymbolicShapeInference: # contrib ops + + + + + 'Attention', 'BiasGelu', \ 'EmbedLayerNormalization', \ 'FastGelu', 'Gelu', 'LayerNormalization', \ diff --git a/tools/setup_helpers/__init__.py b/tools/setup_helpers/__init__.py index 4cb5ecee0..5d375081f 100644 --- a/tools/setup_helpers/__init__.py +++ b/tools/setup_helpers/__init__.py @@ -1 +1 @@ -from .extension import * \ No newline at end of file +from .extension import * diff --git a/tools/setup_helpers/extension.py b/tools/setup_helpers/extension.py index e85ad75f9..ae84f09f4 100644 --- a/tools/setup_helpers/extension.py +++ b/tools/setup_helpers/extension.py @@ -1,9 +1,9 @@ -import distutils.sysconfig import os import platform import subprocess from pathlib import Path +import distutils.sysconfig from setuptools import Extension from setuptools.command.build_ext import build_ext @@ -27,11 +27,13 @@ def _get_build(var, default=False): if val in trues: return True if val not in falses: - print(f"WARNING: Unexpected environment variable value `{var}={val}`. " f"Expected one of {trues + falses}") + print(f"WARNING: Unexpected environment variable value `{var}={val}`. " + f"Expected one of {trues + falses}") return False -_BUILD_SOX = False if platform.system() == "Windows" else _get_build("BUILD_SOX", True) +_BUILD_SOX = False if platform.system() == "Windows" else _get_build( + "BUILD_SOX", True) _BUILD_MAD = _get_build("BUILD_MAD", False) # _BUILD_KALDI = False if platform.system() == "Windows" else _get_build("BUILD_KALDI", True) # _BUILD_RNNT = _get_build("BUILD_RNNT", True) @@ -40,7 +42,8 @@ _BUILD_MAD = _get_build("BUILD_MAD", False) # _USE_ROCM = _get_build("USE_ROCM", torch.cuda.is_available() and torch.version.hip is not None) # _USE_CUDA = _get_build("USE_CUDA", torch.cuda.is_available() and torch.version.hip is None) # _USE_OPENMP = _get_build("USE_OPENMP", True) and "ATen parallel backend: OpenMP" in torch.__config__.parallel_info() -_PADDLESPEECH_CUDA_ARCH_LIST = os.environ.get("PADDLESPEECH_CUDA_ARCH_LIST", None) +_PADDLESPEECH_CUDA_ARCH_LIST = os.environ.get("PADDLESPEECH_CUDA_ARCH_LIST", + None) def get_ext_modules(): @@ -71,7 +74,8 @@ class CMakeBuild(build_ext): if ext.name != "paddlespeech.audio._paddleaudio": return - extdir = os.path.abspath(os.path.dirname(self.get_ext_filename(ext.name))) + extdir = os.path.abspath( + os.path.dirname(self.get_ext_filename(ext.name))) # required for auto-detection of auxiliary "native" libs if not extdir.endswith(os.path.sep): @@ -101,8 +105,12 @@ class CMakeBuild(build_ext): if _PADDLESPEECH_CUDA_ARCH_LIST is not None: # Convert MAJOR.MINOR[+PTX] list to new style one # defined at https://cmake.org/cmake/help/latest/prop_tgt/CUDA_ARCHITECTURES.html - _arches = _PADDLESPEECH_CUDA_ARCH_LIST.replace(".", "").replace(" ", ";").split(";") - _arches = [arch[:-4] if arch.endswith("+PTX") else f"{arch}-real" for arch in _arches] + _arches = _PADDLESPEECH_CUDA_ARCH_LIST.replace(".", "").replace( + " ", ";").split(";") + _arches = [ + arch[:-4] if arch.endswith("+PTX") else f"{arch}-real" + for arch in _arches + ] cmake_args += [f"-DCMAKE_CUDA_ARCHITECTURES={';'.join(_arches)}"] # Default to Ninja @@ -131,10 +139,13 @@ class CMakeBuild(build_ext): if not os.path.exists(self.build_temp): os.makedirs(self.build_temp) - print(f"cmake {_ROOT_DIR} {' '.join(cmake_args)}, cwd={self.build_temp}") - subprocess.check_call(["cmake", str(_ROOT_DIR)] + cmake_args, cwd=self.build_temp) + print( + f"cmake {_ROOT_DIR} {' '.join(cmake_args)}, cwd={self.build_temp}") + subprocess.check_call( + ["cmake", str(_ROOT_DIR)] + cmake_args, cwd=self.build_temp) print(f"cmake --build . {' '.join(build_args)}, cwd={self.build_temp}") - subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=self.build_temp) + subprocess.check_call( + ["cmake", "--build", "."] + build_args, cwd=self.build_temp) def get_ext_filename(self, fullname): ext_filename = super().get_ext_filename(fullname)