|
| 1 | +/* |
| 2 | + * Copyright 2023 Robert Bosch GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | +/** |
| 19 | + * \file fable/make_schema.hpp |
| 20 | + * |
| 21 | + * This file provides a facade for the make_schema function which implements |
| 22 | + * a short-cut for the compiler which reduces the excessive resource |
| 23 | + * consumption of the actual implementation. |
| 24 | + */ |
| 25 | + |
| 26 | +#pragma once |
| 27 | + |
| 28 | +#include <string> // for string |
| 29 | + |
| 30 | +#include <fable/schema/array.hpp> // for Array<> |
| 31 | +#include <fable/schema/boolean.hpp> // for Boolean |
| 32 | +#include <fable/schema/confable.hpp> // for FromConfable |
| 33 | +#include <fable/schema/const.hpp> // for Const<> |
| 34 | +#include <fable/schema/duration.hpp> // for Duration<> |
| 35 | +#include <fable/schema/enum.hpp> // for Enum<> |
| 36 | +#include <fable/schema/ignore.hpp> // for Ignore |
| 37 | +#include <fable/schema/interface.hpp> // for Interface, Box |
| 38 | +#include <fable/schema/json.hpp> // for FromJson<> |
| 39 | +#include <fable/schema/map.hpp> // for Map<> |
| 40 | +#include <fable/schema/number.hpp> // for Number<> |
| 41 | +#include <fable/schema/optional.hpp> // for Optional<> |
| 42 | +#include <fable/schema/passthru.hpp> // for Passthru |
| 43 | +#include <fable/schema/path.hpp> // for Path |
| 44 | +#include <fable/schema/string.hpp> // for String |
| 45 | +#include <fable/schema/struct.hpp> // for Struct |
| 46 | +#include <fable/schema/variant.hpp> // for Variant |
| 47 | + |
| 48 | +// It is important that this include comes after all the other ones, |
| 49 | +// so that it has access to ALL the previous definitions. |
| 50 | +#include <fable/schema/magic.hpp> // for make_prototype, ... |
| 51 | + |
| 52 | +namespace fable::schema { |
| 53 | +namespace details { |
| 54 | + |
| 55 | +/** |
| 56 | + * Implements the make_schema function for one datatype |
| 57 | + */ |
| 58 | +template <typename T> |
| 59 | +struct make_schema_wrapper1 { |
| 60 | + static auto make_schema(T* ptr, std::string&& desc) { |
| 61 | + return ::fable::schema::make_schema_impl(ptr, std::move(desc)); |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + */ |
| 67 | +template <typename T, typename P> |
| 68 | +struct make_schema_wrapper2 { |
| 69 | + static auto make_schema(T* ptr, P proto, std::string&& desc) { |
| 70 | + return ::fable::schema::make_schema_impl(ptr, std::move(proto), std::move(desc)); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +} // namespace details |
| 75 | + |
| 76 | +/** |
| 77 | + * Returns the schema for a given datum and its description |
| 78 | + * |
| 79 | + * \param ptr Pointer to the datum |
| 80 | + * \param desc Textual description of the datum |
| 81 | + * \return Schema for the datum |
| 82 | + */ |
| 83 | +template <typename T> |
| 84 | +auto make_schema(T* ptr, std::string&& desc) { |
| 85 | + return details::make_schema_wrapper1<T>::make_schema(ptr, std::move(desc)); |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns the schema for a given datum and its description |
| 90 | + * |
| 91 | + * \param ptr Pointer to the datum |
| 92 | + * \param proto Prototype of the data-value |
| 93 | + * \param desc Textual description of the datum |
| 94 | + * \return Schema for the datum |
| 95 | + * \note Those types which have a prototype, namely string & path |
| 96 | + * do not matter for the compile-time reduction |
| 97 | + */ |
| 98 | +template <typename T, typename P> |
| 99 | +auto make_schema(T* ptr, P proto, std::string&& desc) { |
| 100 | + return details::make_schema_wrapper2<T, P>::make_schema(ptr, std::move(proto), std::move(desc)); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace fable::schema |
0 commit comments