-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_value_inplace API to basic_node (#443)
* added get_value_inplace() to basic_node * updated API documentation contents * fixed some typos in doc * added link to the documentation page for get_value_inplace() * fixed error in building test suite with older compilers * added test case for get_value_inplace * fixed issue reported from codacy
- Loading branch information
Showing
10 changed files
with
1,110 additions
and
66 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,54 @@ | ||
// _______ __ __ __ _____ __ __ __ | ||
// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library (supporting code) | ||
// | __| _ < \_ _/| ___ | _ | |___ version 0.4.0 | ||
// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML | ||
// | ||
// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani <fktn.dev@gmail.com> | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <iostream> | ||
#include <fkYAML/node.hpp> | ||
|
||
struct not_default_constructible { | ||
not_default_constructible() = delete; | ||
explicit not_default_constructible(int v) noexcept | ||
: value(v) { | ||
} | ||
not_default_constructible(const not_default_constructible&) = default; | ||
|
||
int value {0}; | ||
}; | ||
// Custom implementation of from_node() for not_default_constructible objects. | ||
// This function is called inside get_value_inplace(). | ||
// See https://fktn-k.github.io/fkYAML/api/node_value_converter/from_node/ for details. | ||
inline void from_node(const fkyaml::node& n, not_default_constructible& ndc) { | ||
ndc.value = n.get_value<int>(); | ||
} | ||
|
||
int main() { | ||
// create a sequence node. | ||
fkyaml::node seq = {true, false}; | ||
|
||
// fill the node values into a 1D C-style array | ||
bool bools[2] {}; | ||
seq.get_value_inplace(bools); | ||
for (auto b : bools) { | ||
std::cout << std::boolalpha << b << " "; | ||
} | ||
std::cout << "\n\n"; | ||
|
||
// create an integer scalar node | ||
fkyaml::node integer = 123; | ||
|
||
// get_value_inplace() accepts a type which is not default constructible. | ||
not_default_constructible ndc {0}; | ||
integer.get_value_inplace(ndc); | ||
std::cout << ndc.value << std::endl; | ||
|
||
// of course, you can pass a value of default constructible type as well. | ||
integer = -123; | ||
integer.get_value_inplace(ndc.value); | ||
std::cout << ndc.value << std::endl; | ||
|
||
return 0; | ||
} |
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,4 @@ | ||
true false | ||
|
||
123 | ||
-123 |
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
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,64 @@ | ||
<small>Defined in header [`<fkYAML/node.hpp>`](https://github.com/fktn-k/fkYAML/blob/develop/include/fkYAML/node.hpp)</small> | ||
|
||
# <small>fkyaml::basic_node::</small>get_value | ||
|
||
```cpp | ||
template <typename T> | ||
void get_value_inplace(T& value_ref) const noexcept( | ||
noexcept(ConverterType<T>::from_node(std::declval<const basic_node&>(), std::declval<T&>()))); // (1) | ||
|
||
template <typename BasicNodeType> | ||
void get_value_inplace(BasicNodeType& value_ref) const; // (2) | ||
``` | ||
This function converts a [`fkyaml::basic_node`](./index.md) to either of the followings and fills the conversion result into the given `value_ref` parameter. | ||
1. a compatible value ([copy-constructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)) | ||
The function is equivalent to executing | ||
```cpp | ||
ConverterType<T>::from_node(*this, value_ref); | ||
``` | ||
Unlike the [`get_value`](./get_value.md) function, this function does not require the template parameter type `T` to be [default-constructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible) since no instantiation of `T` is necessary inside the function to return the conversion result. | ||
2. a [fkyaml::basic_node](./index.md) object | ||
The function is equivalent to executing | ||
```cpp | ||
value_ref = *this; // Copy-assigns a current basic_node object. | ||
``` | ||
|
||
This function shares internal implementation with the [`get_value`](./get_value.md). | ||
Thus, all the STL container & scalar types which are supported by that function, is also supported by this function as well. | ||
See the notes there for details. | ||
|
||
???+ Note "Difference from `get_value()`" | ||
|
||
One crutial difference from the [`get_value`](./get_value.md) function is, this function does not require the template parameter type `T` to be [default-constructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible) since no instantiation of `T` is necessary inside the function to return the conversion result anymore. | ||
So, you might prefer this function, for example, if you already created a `T` object as a member variable and want to assign a node value to it. | ||
|
||
Another is C-style array support. | ||
While [`get_value`](./get_value.md) cannot accept any kind of C-style array types since returning such array objects is impossible due to its implementation, this function accepts 1D, 2D and 3D arrays. Note that, if `T` is one of them, the target basic_node object must be a sequence. A [`type_error`](../exception/type_error.md) is thrown otherwise. | ||
|
||
## **Template Parameters** | ||
|
||
***T*** | ||
: A compatible value type. | ||
|
||
***BasicNodeType*** | ||
: A basic_node template instance type. | ||
|
||
???+ Example | ||
|
||
```cpp | ||
--8<-- "examples/ex_basic_node_get_value_inplace.cpp:9" | ||
``` | ||
|
||
output: | ||
```bash | ||
--8<-- "examples/ex_basic_node_get_value_inplace.output" | ||
``` | ||
|
||
## **See Also** | ||
|
||
* [basic_node](index.md) | ||
* [get_value](get_value.md) | ||
* [get_value_ref](get_value_ref.md) | ||
* [node_value_converter::from_node](../node_value_converter/from_node.md) |
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
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
Oops, something went wrong.