<commit_before>// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
#define SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H

#include <string>

#include "SurgSim/Framework/Log.h"

namespace
{
const std::string serializeLogger = "Serialization";
const std::string HasValueName = "HasValue";
const std::string ValueName = "Value";
};

template <class T>
YAML::Node YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::encode(
	const SurgSim::DataStructures::OptionalValue<T>& rhs)
{
	Node node;
	node[HasValueName] = rhs.hasValue();
	if (rhs.hasValue())
	{
		node[ValueName] = rhs.getValue();
	}
	else
	{
		node[ValueName] = "Not set";
	}
	return node;
}

template <class T>
bool YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::decode(
	const Node& node, SurgSim::DataStructures::OptionalValue<T>& rhs)
{
	bool result = true;
	if (node[HasValueName].as<bool>())
	{
		try
		{
			rhs.setValue(node[ValueName].as<T>());
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(serializeLogger);
			SURGSIM_LOG(logger, WARNING) << "Bad conversion";
		}
	}
	else
	{
		rhs.invalidate();
	}
	return result;
}

template <class T, size_t N>
YAML::Node YAML::convert<std::array<T, N>>::encode(const std::array<T, N>& rhs)
{
	Node node(NodeType::Sequence);
	for (auto it = rhs.cbegin(); it != rhs.cend(); ++it)
	{
		node.push_back(*it);
	}
	return node;
}

template <class T, size_t N>
bool YAML::convert<std::array<T, N>>::decode(const Node& node, std::array<T, N>& rhs)
{
	if (!node.IsSequence() || node.size() != N)
	{
		return false;
	}

	bool result = true;
	auto rhsit = rhs.begin();
	for (YAML::const_iterator it = node.begin(); it != node.end(); ++it, ++rhsit)
	{
		try
		{
			(*rhsit) = it->as<T>();
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(serializeLogger);
			SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
		}
	}
	return result;
}

#endif // SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H<commit_msg>Put YAML ID strings in specialized namespaces to avoid symbol conflict.<commit_after>// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
#define SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H

#include <string>

#include "SurgSim/Framework/Log.h"

namespace SurgSim
{
namespace DataStructures
{
namespace Convert
{
const std::string serializeLogger = "Serialization";
const std::string hasValueName = "HasValue";
const std::string valueName = "Value";
};
};
};

template <class T>
YAML::Node YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::encode(
	const SurgSim::DataStructures::OptionalValue<T>& rhs)
{
	Node node;
	node[SurgSim::DataStructures::Convert::hasValueName] = rhs.hasValue();
	if (rhs.hasValue())
	{
		node[SurgSim::DataStructures::Convert::valueName] = rhs.getValue();
	}
	else
	{
		node[SurgSim::DataStructures::Convert::valueName] = "Not set";
	}
	return node;
}

template <class T>
bool YAML::convert<SurgSim::DataStructures::OptionalValue<T>>::decode(
	const Node& node, SurgSim::DataStructures::OptionalValue<T>& rhs)
{
	bool result = true;
	if (node[SurgSim::DataStructures::Convert::hasValueName].as<bool>())
	{
		try
		{
			rhs.setValue(node[SurgSim::DataStructures::Convert::valueName].as<T>());
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(SurgSim::DataStructures::Convert::serializeLogger);
			SURGSIM_LOG(logger, WARNING) << "Bad conversion";
		}
	}
	else
	{
		rhs.invalidate();
	}
	return result;
}

template <class T, size_t N>
YAML::Node YAML::convert<std::array<T, N>>::encode(const std::array<T, N>& rhs)
{
	Node node(NodeType::Sequence);
	for (auto it = rhs.cbegin(); it != rhs.cend(); ++it)
	{
		node.push_back(*it);
	}
	return node;
}

template <class T, size_t N>
bool YAML::convert<std::array<T, N>>::decode(const Node& node, std::array<T, N>& rhs)
{
	if (!node.IsSequence() || node.size() != N)
	{
		return false;
	}

	bool result = true;
	auto rhsit = rhs.begin();
	for (YAML::const_iterator it = node.begin(); it != node.end(); ++it, ++rhsit)
	{
		try
		{
			(*rhsit) = it->as<T>();
		}
		catch (YAML::RepresentationException)
		{
			result = false;
			auto logger = SurgSim::Framework::Logger::getLogger(SurgSim::DataStructures::Convert::serializeLogger);
			SURGSIM_LOG(logger, WARNING) << __FUNCTION__ << ": Bad conversion";
		}
	}
	return result;
}

#endif // SURGSIM_DATASTRUCTURES_DATASTRUCTURESCONVERT_INL_H
{'fraction_non_alphanumeric': 0.1468125106819347, 'fraction_numerical': 0.00273457528627585, 'mean_word_length': 4.965341488277268, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '523519', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1991, 'n_words': 500}