/* * ObjectSerializerTraits.h * * This source file is part of the FoundationDB open source project * * Copyright 2013-2022 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; template struct scalar_traits : std::false_type { constexpr static size_t size = 0; template static void save(uint8_t*, const T&, Context&); // 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 { // May be called multiple times during one serialization. Guaranteed not to be called after save. template static size_t size(const T&, Context&); // Guaranteed to be called only once during serialization template static void save(uint8_t*, const T&, Context&); // 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 serialize_raw : std::false_type { template static uint8_t* save_raw(Context& context, const T& obj); }; 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; // The number of entries in this vector template static size_t num_entries(VectorLike&, Context&); // Return an insert_iterator starting with an empty vector. |size| is the // number of elements to be inserted. Implementations may want to allocate // enough memory up front to hold |size| elements. template static insert_iterator insert(VectorLike&, size_t size, Context&); // Return an iterator to read from this vector. template static iterator begin(const VectorLike&, Context&); }; template struct union_like_traits : std::false_type { using Member = UnionLike; using alternatives = pack<>; template static uint8_t index(const Member&, Context&); template static bool empty(const Member& variant, Context&); template static const index_t& get(const Member&, Context&); template static void assign(Member&, const Alternative&, Context&); 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&, Context&); template static void assign(Member&, const index_t&, Context&); template static void done(Member&, Context&); }; template struct union_like_traits> : std::true_type { using Member = std::variant; using alternatives = pack; template static uint8_t index(const Member& variant, Context&) { return variant.index(); } template static bool empty(const Member& variant, Context&) { return false; } template static const index_t& get(const Member& variant, Context&) { return std::get>(variant); } template static void assign(Member& member, const Alternative& a, Context&) { static_assert(std::is_same_v, Alternative>); member = a; } };