Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#181 support tag for scalars #305

Merged
merged 12 commits into from
Apr 6, 2024
17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: update-version-macros CHANGELOG.md update-version fkYAML.natvis
.PHONY: update-version-macros CHANGELOG.md update-version examples fkYAML.natvis

#################
# variables #
Expand Down Expand Up @@ -96,6 +96,20 @@ update-params-for-natvis:
fkYAML.natvis: update-params-for-natvis
@$(MAKE) -C ./tool/natvis_generator generate

#####################
# Documentation #
#####################

examples:
cmake -B build_examples -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_EXAMPLES=ON
cmake --build build_examples --config Debug

build-docs: examples
@$(MAKE) -C ./docs/mkdocs build

serve-docs: examples
@$(MAKE) -C serve

###############
# Version #
###############
Expand Down Expand Up @@ -169,5 +183,6 @@ clean:
build_clang_sanitizers \
build_clang_tidy \
build_coverage \
build_examples \
build_iwyu \
build_valgrind
18 changes: 18 additions & 0 deletions docs/examples/ex_basic_node_add_tag_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <fkYAML/node.hpp>

int main()
{
// create a YAML node.
fkyaml::node n = 123;

// set a tag name to the node.
n.add_tag_name("!!int");
std::cout << n.get_tag_name() << std::endl;

// overwrite it with a new one.
n.add_tag_name("!!foo");
std::cout << n.get_tag_name() << std::endl;

return 0;
}
2 changes: 2 additions & 0 deletions docs/examples/ex_basic_node_add_tag_name.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!!int
!!foo
2 changes: 1 addition & 1 deletion docs/examples/ex_basic_node_get_anchor_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main()
std::cout << e.what() << std::endl;
}

// add an anchor name to the node.
// set an anchor name to the node.
n.add_anchor_name("anchor");
std::cout << n.get_anchor_name() << std::endl;

Expand Down
24 changes: 24 additions & 0 deletions docs/examples/ex_basic_node_get_tag_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
#include <fkYAML/node.hpp>

int main()
{
// create a YAML node.
fkyaml::node n = 123;

// try to get a tag name before any tag name has been set.
try
{
std::cout << n.get_tag_name() << std::endl;
}
catch (const fkyaml::exception& e)
{
std::cout << e.what() << std::endl;
}

// set a tag name to the node.
n.add_tag_name("!!int");
std::cout << n.get_tag_name() << std::endl;

return 0;
}
2 changes: 2 additions & 0 deletions docs/examples/ex_basic_node_get_tag_name.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
No tag name has been set.
!!int
19 changes: 19 additions & 0 deletions docs/examples/ex_basic_node_has_tag_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <fkYAML/node.hpp>

int main()
{
// create a YAML node.
fkyaml::node n = {{"foo", true}};

// check if the node has a tag name.
std::cout << std::boolalpha << n.has_tag_name() << std::endl;

// set a tag name.
n.add_tag_name("!!map");

// check if the node has a tag name again.
std::cout << std::boolalpha << n.has_tag_name() << std::endl;

return 0;
}
2 changes: 2 additions & 0 deletions docs/examples/ex_basic_node_has_tag_name.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
false
true
32 changes: 32 additions & 0 deletions docs/mkdocs/docs/api/basic_node/add_tag_name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<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>add_tag_name

```cpp
void add_tag_name(const std::string& tag_name);
void add_tag_name(std::string&& tag_name);
```

Adds a tag name to the YAML node.
If the basic_node has already had any tag name, the new tag name overwrites the old one.

## **Parameters**

***`tag_name`*** [in]
: A tag name. This should not be empty.

???+ Example

```cpp
--8<-- "examples/ex_basic_node_add_tag_name.cpp"
```

output:
```bash
--8<-- "examples/ex_basic_node_add_tag_name.output"
```

## **See Also**

* [basic_node](index.md)
* [get_tag_name](get_tag_name.md)
4 changes: 2 additions & 2 deletions docs/mkdocs/docs/api/basic_node/get_anchor_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const std::string& get_anchor_name() const;
```

Gets the anchor name associated to the YAML node.
Gets the anchor name associated with the YAML node.
Some anchor name must be set before calling this API.
Calling [`has_anchor_name`](has_anchor_name.md) to see if the node has any anchor name beforehand.

Expand All @@ -29,4 +29,4 @@ If no anchor name has been set, an [`fkyaml::exception`](../exception/index.md)
## **See Also**

* [basic_node](index.md)
* [set_anchor_name](get_anchor_name.md)
* [has_anchor_name](has_anchor_name.md)
32 changes: 32 additions & 0 deletions docs/mkdocs/docs/api/basic_node/get_tag_name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<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_tag_name

```cpp
const std::string& get_tag_name() const;
```

Gets the tag name associated to the YAML node.
Some tag name must be set before calling this API.
Calling [`has_tag_name`](has_tag_name.md) to see if the node has any tag name beforehand.

## **Return Value**

The tag name associated to the node.
If no tag name has been set, an [`fkyaml::exception`](../exception/index.md) will be thrown.

???+ Example

```cpp
--8<-- "examples/ex_basic_node_get_tag_name.cpp"
```

output:
```bash
--8<-- "examples/ex_basic_node_get_tag_name.output"
```

## **See Also**

* [basic_node](index.md)
* [has_tag_name](has_tag_name.md)
29 changes: 29 additions & 0 deletions docs/mkdocs/docs/api/basic_node/has_tag_name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<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>has_tag_name

```cpp
bool has_tag_name() const noexcept;
```

Check if the YAML node has a tag name.

## **Return Value**

`true` if the YAML node has a tag name, `false` otherwise.

???+ Example

```cpp
--8<-- "examples/ex_basic_node_has_tag_name.cpp"
```

output:
```bash
--8<-- "examples/ex_basic_node_has_tag_name.output"
```

## **See Also**

* [basic_node](index.md)
* [add_tag_name](add_tag_name.md)
3 changes: 3 additions & 0 deletions docs/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ nav:
- (destructor): api/basic_node/destructor.md
- operator=: api/basic_node/operator=.md
- add_anchor_name: api/basic_node/add_anchor_name.md
- add_tag_name: api/basic_node/add_tag_name.md
- alias_of: api/basic_node/alias_of.md
- at: api/basic_node/at.md
- begin: api/basic_node/begin.md
Expand All @@ -117,10 +118,12 @@ nav:
- end: api/basic_node/end.md
- float_number_type: api/basic_node/float_number_type.md
- get_anchor_name: api/basic_node/get_anchor_name.md
- get_tag_name: api/basic_node/get_tag_name.md
- get_value: api/basic_node/get_value.md
- get_value_ref: api/basic_node/get_value_ref.md
- get_yaml_version: api/basic_node/get_yaml_version.md
- has_anchor_name: api/basic_node/has_anchor_name.md
- has_tag_name: api/basic_node/has_tag_name.md
- integer_type: api/basic_node/integer_type.md
- is_alias: api/basic_node/is_alias.md
- is_anchor: api/basic_node/is_anchor.md
Expand Down
48 changes: 48 additions & 0 deletions include/fkYAML/detail/directive_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// _______ __ __ __ _____ __ __ __
/// | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library
/// | __| _ < \_ _/| ___ | _ | |___ version 0.3.3
/// |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML
///
/// SPDX-FileCopyrightText: 2023-2024 Kensuke Fukutani <fktn.dev@gmail.com>
/// SPDX-License-Identifier: MIT
///
/// @file

#ifndef FK_YAML_DETAIL_DIRECTIVE_SET_HPP_
#define FK_YAML_DETAIL_DIRECTIVE_SET_HPP_

#include <string>
#include <map>

#include <fkYAML/detail/macros/version_macros.hpp>
#include <fkYAML/detail/types/yaml_version_t.hpp>

/// @namespace fkyaml
/// @brief namespace for fkYAML library.
FK_YAML_NAMESPACE_BEGIN

/// @namespace detail
/// @brief namespace for internal implementaions of fkYAML library.
namespace detail
{

/// @brief The set of directives for a YAML document.
struct directive_set
{
/// The YAML version used for the YAML document.
yaml_version_t version {yaml_version_t::VER_1_2};
/// Whether or not the YAML version has been specified.
bool is_version_specified {false};
/// The prefix of the primary handle.
std::string primary_handle_prefix {};
/// The prefix of the secondary handle.
std::string secondary_handle_prefix {};
/// The map of handle-prefix pairs.
std::map<std::string /*handle*/, std::string /*prefix*/> named_handle_map {};
};

} // namespace detail

FK_YAML_NAMESPACE_END

#endif /* FK_YAML_DETAIL_DIRECTIVE_SET_HPP_ */