<commit_before>/** @file
    @brief Header

    @date 2015

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

// Copyright 2015 Sensics, 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 INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43
#define INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43

// Internal Includes
#include <osvr/Common/AlignmentPadding.h>
#include <osvr/Common/Endianness.h>
#include <osvr/Common/SerializationTags.h>
#include <osvr/Util/BoolC.h>

// Library/third-party includes
#include <boost/call_traits.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include <boost/noncopyable.hpp>

// Standard includes
#include <string>

namespace osvr {
namespace common {

    namespace serialization {

        /// @brief Traits class indicating how to serialize a type with a given
        /// tag. The default tag for a type `T` is `DefaultSerializationTag<T>`
        ///
        /// A traits class must implement:
        ///
        /// - `template <typename BufferType> static void
        /// serialize(BufferType &buf, typename Base::param_type val, Tag
        /// const&)`
        /// - `template <typename BufferReaderType> static void
        /// deserialize(BufferReaderType &buf, typename Base::reference_type
        /// val, Tag const&)`
        /// - `static size_t spaceRequired(size_t existingBytes, typename
        /// Base::param_type val, Tag const&)`
        ///
        /// The tag parameter may be used to pass runtime arguments in (like a
        /// length), if required.
        ///
        /// A traits class should inherit from `BaseSerializationTraits<T>` to
        /// get useful typedefs.
        ///
        /// See the serializers below for std::string for useful
        /// examples/starting points.
        ///
        /// The dummy template parameter exists for usage of `enable_if`.
        template <typename Tag, typename Dummy = void>
        struct SerializationTraits;

        /// @brief Serialize a value to a buffer, with optional tag to specify
        /// non-default traits.
        template <typename T, typename BufferType,
                  typename Tag = DefaultSerializationTag<T> >
        inline void serializeRaw(BufferType &buf, T const &v,
                                 Tag const &tag = Tag()) {
            SerializationTraits<Tag>::serialize(buf, v, tag);
        }
<commit_msg>Add earlier, clearer error for missing serialization traits.<commit_after>/** @file
    @brief Header

    @date 2015

    @author
    Sensics, Inc.
    <http://sensics.com/osvr>
*/

// Copyright 2015 Sensics, 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 INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43
#define INCLUDED_SerializationTraits_h_GUID_DF0CDFE0_097F_41B2_2DAE_7D4033D28D43

// Internal Includes
#include <osvr/Common/AlignmentPadding.h>
#include <osvr/Common/Endianness.h>
#include <osvr/Common/SerializationTags.h>
#include <osvr/Util/BoolC.h>

// Library/third-party includes
#include <boost/call_traits.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_floating_point.hpp>
#include <boost/type_traits/alignment_of.hpp>
#include <boost/noncopyable.hpp>

// Standard includes
#include <string>

namespace osvr {
namespace common {

    namespace serialization {

        /// Dummy template for better error messages - inspired by
        /// http://stackoverflow.com/a/17917624/265522
        template <typename Tag>
        using MissingSerializationTraitsForTagOrType =
            std::integral_constant<bool, std::is_same<Tag, Tag>::value>;

        /// @brief Traits class indicating how to serialize a type with a given
        /// tag. The default tag for a type `T` is `DefaultSerializationTag<T>`
        ///
        /// A traits class must implement:
        ///
        /// - `template <typename BufferType> static void
        /// serialize(BufferType &buf, typename Base::param_type val, Tag
        /// const&)`
        /// - `template <typename BufferReaderType> static void
        /// deserialize(BufferReaderType &buf, typename Base::reference_type
        /// val, Tag const&)`
        /// - `static size_t spaceRequired(size_t existingBytes, typename
        /// Base::param_type val, Tag const&)`
        ///
        /// The tag parameter may be used to pass runtime arguments in (like a
        /// length), if required.
        ///
        /// A traits class should inherit from `BaseSerializationTraits<T>` to
        /// get useful typedefs.
        ///
        /// See the serializers below for std::string for useful
        /// examples/starting points.
        ///
        /// The dummy template parameter exists for usage of `enable_if`.
        template <typename Tag, typename Dummy = void>
        struct SerializationTraits {
            /// If you get this error, you probably are trying to
            /// serialize/deserialize a type we haven't handled before. In most
            /// cases, you can just add a SerializationTraits specialization for
            /// the DefaultSerializationTag<T> (see docs above) and if it's a
            /// struct, it can probably contain primarily calls to serializeRaw
            /// and deserializeRaw.
            ///
            /// The weird thing we assert here is because we have to make the
            /// assertion dependent on a template type or it will get tripped at
            /// point of declaration, rather than instantiation.
            static_assert(!MissingSerializationTraitsForTagOrType<Tag>::value,
                          "You must implement a SerializationTraits "
                          "specialization for this type/tag!");
        };

        /// @brief Serialize a value to a buffer, with optional tag to specify
        /// non-default traits.
        template <typename T, typename BufferType,
                  typename Tag = DefaultSerializationTag<T> >
        inline void serializeRaw(BufferType &buf, T const &v,
                                 Tag const &tag = Tag()) {
            SerializationTraits<Tag>::serialize(buf, v, tag);
        }

{'fraction_non_alphanumeric': 0.10016264570344267, 'fraction_numerical': 0.014909189482244511, 'mean_word_length': 2.4659464537341473, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4723313', 'n_tokens_mistral': 2080, 'n_tokens_neox': 1990, 'n_words': 673}