|
| 1 | +/* Copyright 2018 Rene Widera |
| 2 | + * |
| 3 | + * This file is part of PMacc. |
| 4 | + * |
| 5 | + * PMacc is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of either the GNU General Public License or |
| 7 | + * the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * PMacc is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License and the GNU Lesser General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * and the GNU Lesser General Public License along with PMacc. |
| 19 | + * If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#include <boost/preprocessor/repetition/repeat_from_to.hpp> |
| 25 | + |
| 26 | + |
| 27 | +namespace pmacc |
| 28 | +{ |
| 29 | +namespace compileTime |
| 30 | +{ |
| 31 | + /** get character of an C-string |
| 32 | + * |
| 33 | + * @tparam T_len length of the string |
| 34 | + * |
| 35 | + * @param cstr input string |
| 36 | + * @param idx index of the character |
| 37 | + * @return if x < T_len character at index idx, else '0' |
| 38 | + */ |
| 39 | + template< |
| 40 | + int T_len |
| 41 | + > |
| 42 | + constexpr auto |
| 43 | + elem_at( |
| 44 | + char const ( & cstr )[ T_len ], |
| 45 | + size_t const idx |
| 46 | + ) |
| 47 | + -> char |
| 48 | + { |
| 49 | + return idx < T_len ? cstr[ idx ] : 0; |
| 50 | + } |
| 51 | + |
| 52 | + /** compile time string |
| 53 | + * |
| 54 | + * The size of the instance is 1 byte. |
| 55 | + */ |
| 56 | + template< char ... T_c > |
| 57 | + struct String |
| 58 | + { |
| 59 | + /** get stored string */ |
| 60 | + static auto |
| 61 | + str() |
| 62 | + -> std::string |
| 63 | + { |
| 64 | + return std::string( |
| 65 | + std::array< |
| 66 | + char, |
| 67 | + sizeof...( T_c ) + 1 |
| 68 | + >( { |
| 69 | + T_c ..., |
| 70 | + // at terminal zero to support empty strings |
| 71 | + 0 |
| 72 | + } ).data( ) |
| 73 | + ); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + |
| 78 | +#define PMACC_CHAR_AT_N(z, n, name ) pmacc::compileTime::elem_at< sizeof(name) >( name, n ), |
| 79 | + |
| 80 | +/** create a compile time string type |
| 81 | + * |
| 82 | + * Support strings with up to 64 characters. |
| 83 | + * Longer strings are cropped to 64 characters. |
| 84 | + * |
| 85 | + * usage example: |
| 86 | + * @code{.cpp} |
| 87 | + * // create an instance of the compile time string |
| 88 | + * auto particleName = PMACC_CSTRING( "electrons" ){}; |
| 89 | + * // create a C++ type (can be used as template parameter) |
| 90 | + * using Electrons = PMACC_CSTRING( "electrons" ); |
| 91 | + * @endcode |
| 92 | + */ |
| 93 | + |
| 94 | +#define PMACC_CSTRING( str ) \ |
| 95 | + /* // PMACC_CSTRING("example") is transformed in \ |
| 96 | + * pmacc::compileTime::String< \ |
| 97 | + * pmacc::compileTime::elem_at< sizeof("example") >( sizeof("example", 0 ), \ |
| 98 | + * pmacc::compileTime::elem_at< sizeof("example") >( sizeof("example", 1 ), \ |
| 99 | + * ... \ |
| 100 | + * pmacc::compileTime::elem_at< sizeof("example") >( sizeof("example", 63 ), \ |
| 101 | + * 0 \ |
| 102 | + * > \ |
| 103 | + */ \ |
| 104 | + pmacc::compileTime::String< \ |
| 105 | + BOOST_PP_REPEAT_FROM_TO( \ |
| 106 | + 0, \ |
| 107 | + /* support up to 64 charactres */ \ |
| 108 | + 64, \ |
| 109 | + PMACC_CHAR_AT_N, \ |
| 110 | + str \ |
| 111 | + ) \ |
| 112 | + /* add a end zero because PMACC_CHAR_AT_N end with a comma */ \ |
| 113 | + 0 \ |
| 114 | + > |
| 115 | + |
| 116 | +} // namespace compileTime |
| 117 | +} // namespace pmacc |
0 commit comments