|
| 1 | +#pragma once |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2026, Roland Bock |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * Redistributions of source code must retain the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer. |
| 12 | + * |
| 13 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +#ifndef SQLPP_INCLUDE_REFLECTION |
| 31 | +#if defined(__cpp_impl_reflection) && __cpp_impl_reflection >= 202506L |
| 32 | +#define SQLPP_INCLUDE_REFLECTION 1 |
| 33 | +#else |
| 34 | +#define SQLPP_INCLUDE_REFLECTION 0 |
| 35 | +#endif |
| 36 | +#endif |
| 37 | + |
| 38 | +#if SQLPP_INCLUDE_REFLECTION == 1 |
| 39 | + |
| 40 | +#include <meta> |
| 41 | +#include <optional> |
| 42 | + |
| 43 | +#include <sqlpp23/core/detail/fixed_string.h> |
| 44 | + |
| 45 | +namespace sqlpp::meta { |
| 46 | + |
| 47 | +template <::sqlpp::detail::fixed_string Alias> |
| 48 | +struct reflection_alias { |
| 49 | + struct _sqlpp_name_tag { |
| 50 | + [[maybe_unused]] static constexpr bool require_quotes = false; |
| 51 | + [[maybe_unused]] static constexpr auto name = Alias; |
| 52 | + |
| 53 | + template <typename T> |
| 54 | + struct _member_impl_outer_t { |
| 55 | + struct _member_impl_inner_t; |
| 56 | + |
| 57 | + consteval { |
| 58 | + ::std::meta::define_aggregate( |
| 59 | + ^^_member_impl_inner_t, |
| 60 | + { |
| 61 | + ::std::meta::data_member_spec( |
| 62 | + ^^T, { |
| 63 | + .name = _sqlpp_name_tag::name, |
| 64 | + .alignment = std::nullopt, |
| 65 | + .bit_width = std::nullopt, |
| 66 | + .no_unique_address = false})}); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + // separate struct with base class necessary because it's not possible to |
| 71 | + // add a member function with `define_aggregate` |
| 72 | + template <typename T> |
| 73 | + struct _member_t : _member_impl_outer_t<T>::_member_impl_inner_t { |
| 74 | + auto& operator()(this auto&& self) { |
| 75 | + // used to access members of base |
| 76 | + constexpr auto ctx1 = ::std::meta::access_context::current(); |
| 77 | + constexpr auto ctx2 = ctx1.via(^^_member_t); |
| 78 | + |
| 79 | + // get first (and single) member of this struct which has `Alias` as its |
| 80 | + // name |
| 81 | + return self |
| 82 | + .[: ::std::meta::nonstatic_data_members_of( |
| 83 | + ^^typename _member_impl_outer_t<T>::_member_impl_inner_t, |
| 84 | + ctx2)[0]:]; |
| 85 | + } |
| 86 | + }; |
| 87 | + }; |
| 88 | +}; |
| 89 | + |
| 90 | +template <::sqlpp::detail::fixed_string Alias> |
| 91 | +consteval auto make_alias() -> ::sqlpp::meta::reflection_alias<Alias> { |
| 92 | + return {}; |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace sqlpp::meta |
| 96 | +#endif |
0 commit comments