/* * ObjectSerializerTraits.h * * This source file is part of the FoundationDB open source project * * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors * * 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. */ #pragma once #include #include #include #include #include #include #include template struct is_fb_function_t : std::false_type {}; template struct is_fb_function_t::type> : std::true_type {}; template constexpr bool is_fb_function = is_fb_function_t::value; template typename std::enable_if, void>::type serializer(Visitor& visitor, Items&... items) { visitor(items...); } template struct pack {}; template struct index_impl; template struct index_impl> { using type = typename index_impl>::type; }; template struct index_impl<0, pack> { using type = T; }; template using index_t = typename index_impl::type; struct Block { const uint8_t* data; size_t size; }; template Block unownedPtr(T* t, size_t s) { return Block{ t, s }; } template struct scalar_traits : std::false_type { constexpr static size_t size = 0; static void save(uint8_t*, const T&); // Context is an arbitrary type that is plumbed by reference throughout the // load call tree. template static void load(const uint8_t*, T&, Context&); }; template struct dynamic_size_traits : std::false_type { static Block save(const T&); static void serialization_done(const T&); // Optional. Called after the last call to save. // Context is an arbitrary type that is plumbed by reference throughout the // load call tree. template static void load(const uint8_t*, size_t, T&, Context&); }; template struct serializable_traits : std::false_type { template static void serialize(Archiver& ar, T& v); }; template struct vector_like_traits : std::false_type { // Write this at the beginning of the buffer using value_type = uint8_t; using iterator = void; using insert_iterator = void; static size_t num_entries(VectorLike&); template static void reserve(VectorLike&, size_t, Context&); static insert_iterator insert(VectorLike&); static iterator begin(const VectorLike&); }; template struct union_like_traits : std::false_type { using Member = UnionLike; using alternatives = pack<>; static uint8_t index(const Member&); static bool empty(const Member& variant); template static const index_t& get(const Member&); template static const void assign(Member&, const Alternative&); template static void done(Member&, Context&); }; // TODO(anoyes): Implement things that are currently using scalar traits with // struct-like traits. template struct struct_like_traits : std::false_type { using Member = StructLike; using types = pack<>; template static const index_t& get(const Member&); template static const void assign(Member&, const index_t&); template static void done(Member&, Context&); }; template struct union_like_traits> : std::true_type { using Member = std::variant; using alternatives = pack; static uint8_t index(const Member& variant) { return variant.index(); } static bool empty(const Member& variant) { return false; } template static const index_t& get(const Member& variant) { return std::get>(variant); } template static const void assign(Member& member, const Alternative& a) { static_assert(std::is_same_v, Alternative>); member = a; } };