Skip to content

Commit

Permalink
Merge pull request #183 from fktn-k/feature/180_clean_up_public_apis
Browse files Browse the repository at this point in the history
#180 clean up public apis
  • Loading branch information
fktn-k authored Oct 29, 2023
2 parents b116abb + aff3715 commit 26dd1c6
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 397 deletions.
37 changes: 14 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,11 @@ The `Serializer` class provides an API for serializing YAML node values into a s
// ...
fkyaml::node root = fkyaml::node::Mapping({
{ "foo", fkyaml::node::string_scalar("test") },
{ "bar", fkyaml::node::sequence({
fkyaml::node(3.14),
fkyaml::node(std::nan(""))
}) },
{ "baz", fkyaml::node(true) }
});
fkyaml::node root = {
{ std::string("foo"), std::string("test") },
{ std::string("bar"), { 3.14, std::nan("") } },
{ std::string("baz"), true }
};
std::string str = fkyaml::node::serialize(root);
// foo: test
Expand Down Expand Up @@ -236,33 +233,27 @@ The `node` class provides APIs for building YAML nodes programatically.
fkyaml::node root = fkyaml::node::mapping();

// Add a string scalar node.
root["foo"] = fkyaml::node(std::string("test"));
root["foo"] = std::string("test");

// Add a sequence node containing floating number scalar nodes.
root["bar"] = fkyaml::node::sequence({
fkyaml::node(3.14),
fkyaml::node(std::nan(""))
});
root["bar"] = { 3.14, std::nan("") };

// Add a boolean node.
root["baz"] = fkyaml::node(true);
root["baz"] = true;

// Instead, you can build YAML nodes all at once.
fkyaml::node another_root = fkyaml::node::mapping({
{ "foo", fkyaml::node(std::string("test")) },
{ "bar", fkyaml::node::sequence({
fkyaml::node(3.14),
fkyaml::node(std::nan(""))
}) },
{ "baz", fkyaml::node(true) }
});
fkyaml::node another_root = {
{ std::string("foo"), std::string("test") },
{ std::string("bar"), { 3.14, std::nan("") } },
{ std::string("baz"), true }
};
```

</div></details>

### Customize serialization/deserialization

To make your own custom types convertible from/to ``fkyaml::node`` type, you can implement your own `to_node()` & `from_node()` **outside** of the fkyaml namespace.
To make your own custom types convertible from/to `fkyaml::node` type, you can implement your own `to_node()` & `from_node()` **outside** of the fkyaml namespace.
(Those functions will be called when you use `fkyaml::node::get_value\<CustomType\>` to get a CustomType object out of a `fkyaml::node` object.)

<details>
Expand Down
8 changes: 4 additions & 4 deletions include/fkYAML/detail/input/deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,31 +202,31 @@ class basic_deserializer
add_new_key(lexer.get_string(), cur_indent);
break;
}
assign_node_value(BasicNodeType::boolean_scalar(lexer.get_boolean()));
assign_node_value(BasicNodeType(lexer.get_boolean()));
break;
case lexical_token_t::INTEGER_VALUE:
if (m_current_node->is_mapping())
{
add_new_key(lexer.get_string(), cur_indent);
break;
}
assign_node_value(BasicNodeType::integer_scalar(lexer.get_integer()));
assign_node_value(BasicNodeType(lexer.get_integer()));
break;
case lexical_token_t::FLOAT_NUMBER_VALUE:
if (m_current_node->is_mapping())
{
add_new_key(lexer.get_string(), cur_indent);
break;
}
assign_node_value(BasicNodeType::float_number_scalar(lexer.get_float_number()));
assign_node_value(BasicNodeType(lexer.get_float_number()));
break;
case lexical_token_t::STRING_VALUE:
if (m_current_node->is_mapping())
{
add_new_key(lexer.get_string(), cur_indent);
break;
}
assign_node_value(BasicNodeType::string_scalar(lexer.get_string()));
assign_node_value(BasicNodeType(lexer.get_string()));
break;
default: // LCOV_EXCL_LINE
throw fkyaml::exception("Unsupported lexical token found."); // LCOV_EXCL_LINE
Expand Down
26 changes: 0 additions & 26 deletions include/fkYAML/detail/input/input_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,6 @@ inline iterator_input_adapter<ItrType> input_adapter(ItrType begin, ItrType end)
return iterator_input_adapter<ItrType>(begin, end);
}

/**
* @brief A factory method for iterator_input_adapter objects with char* objects.
* @note This function assumes a null-terminated string as an argument.
*
* @tparam CharPtrType A pointer type for char.
* @param ptr A pointer to the beginning of a target null-terminated string.
* @return decltype(input_adapter(ptr, ptr + std::strlen(ptr)))
*/
template <
typename CharPtrType, enable_if_t<
conjunction<
std::is_pointer<CharPtrType>, negation<std::is_array<CharPtrType>>,
std::is_integral<remove_pointer_t<CharPtrType>>,
bool_constant<sizeof(remove_pointer_t<CharPtrType>) == sizeof(char)>>::value,
int> = 0>
inline auto input_adapter(CharPtrType ptr, std::size_t size) -> decltype(input_adapter(ptr, ptr + size))
{
// get the actual buffer size.
std::size_t i = 0;
for (; i < size && *(ptr + i) != '\0'; i++)
{
}
size = (i < size) ? i : size;
return input_adapter(ptr, ptr + size);
}

/**
* @brief A factory method for iterator_input_adapter objects with C-style arrays.
*
Expand Down
73 changes: 1 addition & 72 deletions include/fkYAML/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class basic_node
float_val = static_cast<float_number_type>(0.0);
break;
case node_t::STRING:
p_string = create_object<string_type>("");
p_string = create_object<string_type>();
break;
default: // LCOV_EXCL_LINE
throw fkyaml::exception("Unsupported node value type."); // LCOV_EXCL_LINE
Expand Down Expand Up @@ -459,14 +459,6 @@ class basic_node
detail::input_adapter(std::forward<ItrType>(begin), std::forward<ItrType>(end)));
}

/// @brief Deserialize an input string specified with a pointer and a size.
/// @todo Delete this API b/c this can be achieved with iterators and possibly yield undefined behavior.
template <typename PtrType, detail::enable_if_t<std::is_pointer<PtrType>::value, int> = 0>
static basic_node deserialize(PtrType&& ptr, std::size_t size)
{
return deserializer_type().deserialize(detail::input_adapter(std::forward<PtrType>(ptr), size));
}

/// @brief Serialize a basic_node object into a string.
/// @sa https://fktn-k.github.io/fkYAML/api/basic_node/serialize/
static std::string serialize(const basic_node& node)
Expand Down Expand Up @@ -540,69 +532,6 @@ class basic_node
return node;
} // LCOV_EXCL_LINE

/// @brief A factory method for boolean scalar basic_node objects.
/// @todo delete this API b/c this can be achieved with constructor with std::initializer_list.
static basic_node boolean_scalar(const boolean_type boolean) noexcept
{
basic_node node;
node.m_node_type = node_t::BOOLEAN;
node.m_node_value.boolean = boolean;
return node;
}

/// @brief A factory method for integer scalar basic_node objects.
/// @todo delete this API b/c this can be achieved with constructor with std::initializer_list.
static basic_node integer_scalar(const integer_type integer) noexcept
{
basic_node node;
node.m_node_type = node_t::INTEGER;
node.m_node_value.integer = integer;
return node;
}

/// @brief A factory method for float number scalar basic_node objects.
/// @todo delete this API b/c this can be achieved with constructor with std::initializer_list.
static basic_node float_number_scalar(const float_number_type float_val) noexcept
{
basic_node node;
node.m_node_type = node_t::FLOAT_NUMBER;
node.m_node_value.float_val = float_val;
return node;
}

/// @brief A factory method for string basic_node objects without string_type objects.
/// @todo delete this API b/c this can be achieved with constructor with std::initializer_list.
static basic_node string_scalar()
{
basic_node node;
node.m_node_type = node_t::STRING;
node.m_node_value.p_string = create_object<string_type>();
FK_YAML_ASSERT(node.m_node_value.p_string != nullptr);
return node;
} // LCOV_EXCL_LINE

/// @brief A factory method for string basic_node objects with lvalue string_type objects.
/// @todo delete this API b/c this can be achieved with constructor with std::initializer_list.
static basic_node string_scalar(const string_type& str)
{
basic_node node;
node.m_node_type = node_t::STRING;
node.m_node_value.p_string = create_object<string_type>(str);
FK_YAML_ASSERT(node.m_node_value.p_string != nullptr);
return node;
} // LCOV_EXCL_LINE

/// @brief A factory method for string basic_node objects with rvalue string_type objects.
/// @todo delete this API b/c this can be achieved with constructor with std::initializer_list.
static basic_node string_scalar(string_type&& str)
{
basic_node node;
node.m_node_type = node_t::STRING;
node.m_node_value.p_string = create_object<string_type>(std::move(str));
FK_YAML_ASSERT(node.m_node_value.p_string != nullptr);
return node;
} // LCOV_EXCL_LINE

/// @brief A factory method for alias basic_node objects referencing the given anchor basic_node object.
/// @note The given anchor basic_node must have a non-empty anchor name.
/// @sa https://fktn-k.github.io/fkYAML/api/basic_node/alias_of/
Expand Down
6 changes: 0 additions & 6 deletions test/unit_test/test_input_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ TEST_CASE("InputAdapterTest_IteratorInputAdapterProviderTest", "[InputAdapterTes
REQUIRE(std::is_same<decltype(input_adapter), fkyaml::detail::iterator_input_adapter<char*>>::value);
}

SECTION("char pointer and size")
{
auto input_adapter = fkyaml::detail::input_adapter(&input[0], sizeof(input));
REQUIRE(std::is_same<decltype(input_adapter), fkyaml::detail::iterator_input_adapter<char*>>::value);
}

SECTION("char pointers for beginning/end")
{
auto input_adapter = fkyaml::detail::input_adapter(&input[0], &input[sizeof(input) - 1]);
Expand Down
Loading

0 comments on commit 26dd1c6

Please sign in to comment.