-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from fktn-k/feature/158_add_to_string_for_ser…
…ialization #158 Added to_string() for serialization of YAML nodes.
- Loading branch information
Showing
2 changed files
with
104 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* _______ __ __ __ _____ __ __ __ | ||
* | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library | ||
* | __| _ < \_ _/| ___ | _ | |___ version 0.1.2 | ||
* |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML | ||
* | ||
* SPDX-FileCopyrightText: 2023 Kensuke Fukutani <[email protected]> | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* @file | ||
*/ | ||
|
||
#ifndef TO__string_HPP_ | ||
#define TO__string_HPP_ | ||
|
||
#include <cmath> | ||
#include <limits> | ||
#include <string> | ||
#include <sstream> | ||
#include <type_traits> | ||
|
||
#include <fkYAML/detail/macros/version_macros.hpp> | ||
#include <fkYAML/detail/meta/stl_supplement.hpp> | ||
#include <fkYAML/detail/meta/type_traits.hpp> | ||
|
||
/** | ||
* @namespace fkyaml | ||
* @brief namespace for fkYAML library. | ||
*/ | ||
FK_YAML_NAMESPACE_BEGIN | ||
|
||
/** | ||
* @namespace detail | ||
* @brief namespace for internal implementations of fkYAML library. | ||
*/ | ||
namespace detail | ||
{ | ||
|
||
template <typename ValueType, typename CharType> | ||
inline void to_string(std::basic_string<CharType>& s, ValueType); | ||
|
||
template <> | ||
inline void to_string(std::string& s, std::nullptr_t /*unused*/) | ||
{ | ||
s = "null"; | ||
} | ||
|
||
template <> | ||
inline void to_string(std::string& s, bool b) | ||
{ | ||
s = b ? "true" : "false"; | ||
} | ||
|
||
template <typename IntegerType> | ||
inline enable_if_t<is_non_bool_integral<IntegerType>::value> to_string(std::string& s, IntegerType i) | ||
{ | ||
s = std::to_string(i); | ||
} | ||
|
||
template <typename FloatType> | ||
inline enable_if_t<std::is_floating_point<FloatType>::value> to_string(std::string& s, FloatType f) | ||
{ | ||
if (std::isnan(f)) | ||
{ | ||
s = ".nan"; | ||
return; | ||
} | ||
|
||
if (std::isinf(f)) | ||
{ | ||
if (f == std::numeric_limits<FloatType>::infinity()) | ||
{ | ||
s = ".inf"; | ||
} | ||
else | ||
{ | ||
s = "-.inf"; | ||
} | ||
return; | ||
} | ||
|
||
std::ostringstream oss; | ||
oss << f; | ||
s = oss.str(); | ||
} | ||
|
||
} // namespace detail | ||
|
||
FK_YAML_NAMESPACE_END | ||
|
||
#endif /* TO__string_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters