| 1 |
/*************************************************************************************** |
| 2 |
* This file is dedicated to the public domain. If your jurisdiction requires a * |
| 3 |
* specific license: * |
| 4 |
* * |
| 5 |
* Copyright (c) Stephen McDowell, 2017-2024 * |
| 6 |
* License: CC0 1.0 Universal * |
| 7 |
* License Text: https://creativecommons.org/publicdomain/zero/1.0/legalcode * |
| 8 |
**************************************************************************************/ |
| 9 |
/** |
| 10 |
* \file |
| 11 |
* |
| 12 |
* \brief This file tests struct nesting with template specializations. |
| 13 |
*/ |
| 14 |
#pragma once |
| 15 |
|
| 16 |
#include <string> |
| 17 |
#include <type_traits> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
/** Specialized struct nesting for the joy and fun of all: |
| 21 |
* https://github.com/svenevs/exhale/issues/156 */ |
| 22 |
namespace special { |
| 23 |
/// Normal struct nesting. |
| 24 |
struct Normal { |
| 25 |
/// The id `"Normal"`. |
| 26 |
std::string id() const { return "Normal"; } |
| 27 |
|
| 28 |
/// Nested struct. |
| 29 |
struct Nested { |
| 30 |
/// The id `"Nested"`. |
| 31 |
std::string id() const { return "Nested"; } |
| 32 |
|
| 33 |
/// Deeper nesting like wow. |
| 34 |
struct Like { |
| 35 |
/// The id `"Like"`. |
| 36 |
std::string id() const { return "Like"; } |
| 37 |
|
| 38 |
/// Even deeper nesting `\o/`. |
| 39 |
struct Usual { |
| 40 |
/// The id `"Usual"`. |
| 41 |
std::string id() const { return "Usual"; } |
| 42 |
}; |
| 43 |
}; |
| 44 |
}; |
| 45 |
}; |
| 46 |
|
| 47 |
/** A dummy struct with template parameters to use for testing nesting |
| 48 |
* specialization name parsing with "template template parameters". */ |
| 49 |
template <int Twidth, int Theight> |
| 50 |
struct Image { |
| 51 |
static constexpr int width = Twidth;///< Width of the image. |
| 52 |
static constexpr int height = Theight;///< Height of the image. |
| 53 |
}; |
| 54 |
|
| 55 |
/// Unspecialized template class with nesting. |
| 56 |
template <class TImage, typename Tdata_t, int Tchannels> |
| 57 |
class ImageBuffer { |
| 58 |
public: |
| 59 |
using Image_t = TImage;///< Convenience typedef for template `TImage`. |
| 60 |
using data_t = Tdata_t;///< Convenience typedef for template `Tdata_t`. |
| 61 |
static constexpr int width = Image_t::width;///< The width of the image. |
| 62 |
static constexpr int height = Image_t::height;///< The height of the image. |
| 63 |
static constexpr int channels = Tchannels;///< The number of channels per pixel. |
| 64 |
|
| 65 |
/// Never do this in real life... |
| 66 |
struct Data { |
| 67 |
data_t vals[channels];///< The pixel data. |
| 68 |
}; |
| 69 |
|
| 70 |
/// Checking nested specials too. |
| 71 |
template <int TNested> |
| 72 |
struct SomeThing { |
| 73 |
/// The identifier |
| 74 |
std::string id() const { return std::to_string(TNested); } |
| 75 |
}; |
| 76 |
|
| 77 |
/// How much memory is used. |
| 78 |
constexpr int size() const { |
| 79 |
return width * height * sizeof(Data); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
/// Now lets specialize it partially and arbitrarily change behavior! |
| 84 |
template <class TImage, typename Tdata_t> |
| 85 |
class ImageBuffer<TImage, Tdata_t, 4> { |
| 86 |
public: |
| 87 |
using Image_t = TImage;///< Convenience typedef for template `TImage`. |
| 88 |
using data_t = Tdata_t;///< Convenience typedef for template `Tdata_t`. |
| 89 |
static constexpr int width = Image_t::width;///< The width of the image. |
| 90 |
static constexpr int height = Image_t::height;///< The height of the image. |
| 91 |
static constexpr int channels = 4;///< The number of channels per pixel. |
| 92 |
|
| 93 |
/// So really, don't change how things work like this in real life... |
| 94 |
struct Data { |
| 95 |
data_t r;///< Red. |
| 96 |
data_t g;///< Green. |
| 97 |
data_t b;///< Blue. |
| 98 |
data_t a;///< Alpha. |
| 99 |
}; |
| 100 |
|
| 101 |
/// This one is not as special, right? |
| 102 |
struct SomeThing { |
| 103 |
/// The special identifier. |
| 104 |
std::string id() const { return "I AM SPECIAL!"; } |
| 105 |
}; |
| 106 |
|
| 107 |
/// How much memory is used. |
| 108 |
constexpr int size() const { |
| 109 |
return width * height * 4 * sizeof(data_t); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
/// A fully specialized buffah. |
| 114 |
template <> |
| 115 |
class ImageBuffer<Image<1920, 1080>, float, 128> { |
| 116 |
public: |
| 117 |
using Image_t = Image<1920, 1080>;///< Convenience typedef for the Image class. |
| 118 |
using data_t = float;///< Convenience typedef for underlying data type `float`. |
| 119 |
static constexpr int width = 1920;///< The width of the image. |
| 120 |
static constexpr int height = 1080;///< The height of the image. |
| 121 |
static constexpr int channels = 128;///< The number of channels per pixel. |
| 122 |
|
| 123 |
/// Definitely don't do this. |
| 124 |
struct Data { |
| 125 |
std::vector<float> samples;///< so arbitrary |
| 126 |
}; |
| 127 |
|
| 128 |
/// Super special. |
| 129 |
struct SomeThing { |
| 130 |
/// The spectral identifier. |
| 131 |
std::string id() const { return "Spectral"; } |
| 132 |
}; |
| 133 |
|
| 134 |
/// This is a lie. |
| 135 |
constexpr int size() const { return 88; } |
| 136 |
}; |
| 137 |
|
| 138 |
/** The original example expanded a bit. Thanks florian for making it |
| 139 |
* copy-pasteable :) */ |
| 140 |
template <size_t N> |
| 141 |
struct Base { |
| 142 |
/// How big the base is. |
| 143 |
constexpr size_t size() const { return N; } |
| 144 |
struct A {};///< eeep |
| 145 |
}; |
| 146 |
|
| 147 |
/// Specialized base. |
| 148 |
template <> |
| 149 |
struct Base<2> { |
| 150 |
/// It is bigger for no real reason. |
| 151 |
constexpr size_t size() const { return 42; } |
| 152 |
|
| 153 |
/// Inner struct 1. |
| 154 |
struct InnerStruct { |
| 155 |
/// returns 3 |
| 156 |
int a() const { return 3; }; |
| 157 |
}; |
| 158 |
|
| 159 |
/// Inner struct 2. |
| 160 |
struct AnotherNestedStruct { |
| 161 |
/// returns 4 |
| 162 |
double b() const { return 4; } |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
char base_member{'!'};///< not used |
| 167 |
|
| 168 |
/// Unspecialized inner struct. |
| 169 |
template <size_t M, typename no_use_this = void> |
| 170 |
struct InnerTemplatedStruct { |
| 171 |
/// returns `{` |
| 172 |
char s() const { return '{'; } |
| 173 |
}; |
| 174 |
|
| 175 |
/// Specialized inner struct. |
| 176 |
template <typename dont_use_this> |
| 177 |
struct InnerTemplatedStruct<4, dont_use_this>{ |
| 178 |
/// returns `}` |
| 179 |
char s() const { return '}'; } |
| 180 |
}; |
| 181 |
}; |
| 182 |
|
| 183 |
/// unique snowflakes are unique |
| 184 |
namespace unique { |
| 185 |
/// very unique |
| 186 |
namespace snowflake { |
| 187 |
/// A wrapper struct in a namespace so I can have a final template parameter |
| 188 |
/// with some `::` in it. |
| 189 |
template <auto Epoch> |
| 190 |
struct Ontology { |
| 191 |
/// The current version of the simulation. |
| 192 |
static constexpr auto epoch() { return Epoch; } |
| 193 |
/// The dual of the epoch of the simulation. Same as epoch. |
| 194 |
auto dual() const { return epoch(); } |
| 195 |
}; |
| 196 |
|
| 197 |
/// A very special epoch with a dual meaning. |
| 198 |
template <> |
| 199 |
struct Ontology<11> { |
| 200 |
/// The current version of the simulation. |
| 201 |
static constexpr auto epoch() { return 11; } |
| 202 |
/// The dual of the epoch of the simulation, which is special. ? |
| 203 |
auto dual() const { return 22; } |
| 204 |
}; |
| 205 |
} |
| 206 |
|
| 207 |
/// A nonsense class to specialize. |
| 208 |
template <int X, class T> |
| 209 |
struct Nonsense { |
| 210 |
/// Initializes the thing. |
| 211 |
Nonsense(const T &t) : thing{t} { } |
| 212 |
/// A T reference thing. |
| 213 |
const T &thing; |
| 214 |
|
| 215 |
/// Returns template parameter X. |
| 216 |
int x() const { return X; } |
| 217 |
/// Returns the tea. |
| 218 |
const T &t() const { return thing; } |
| 219 |
}; |
| 220 |
|
| 221 |
/// Partially specialized nonsense. |
| 222 |
template <int X> |
| 223 |
struct Nonsense<X, snowflake::Ontology<X>> { |
| 224 |
/// Makes some nonsense. |
| 225 |
Nonsense(const snowflake::Ontology<X> &so) : thing{so} { } |
| 226 |
/// A snowflake thing. |
| 227 |
const snowflake::Ontology<X> &thing; |
| 228 |
|
| 229 |
/// Returns snowflake epoch plus dual plus X. |
| 230 |
int x() const { |
| 231 |
return snowflake::Ontology<X>::epoch() + thing.dual() + X; |
| 232 |
} |
| 233 |
/// Gives you a snowflake. |
| 234 |
const snowflake::Ontology<X> &t() const { return thing; } |
| 235 |
}; |
| 236 |
|
| 237 |
/// Alias to partial nonsense. |
| 238 |
template <int X> |
| 239 |
using PartialNonsense = Nonsense<X, snowflake::Ontology<X>>; |
| 240 |
|
| 241 |
/// A fully specialized nonsense. |
| 242 |
template <> |
| 243 |
struct Nonsense<11, snowflake::Ontology<11>> { |
| 244 |
/// Fully special nonsense. |
| 245 |
Nonsense(const snowflake::Ontology<11> &so) : thing{so} { } |
| 246 |
/// A special snowflake thing. |
| 247 |
const snowflake::Ontology<11> &thing; |
| 248 |
|
| 249 |
/// Returns snowflake epoch plus dual. |
| 250 |
int x() const { |
| 251 |
return snowflake::Ontology<11>::epoch() + thing.dual(); |
| 252 |
} |
| 253 |
/// Gives you a special snowflake. |
| 254 |
const snowflake::Ontology<11> &t() const { return thing; } |
| 255 |
}; |
| 256 |
|
| 257 |
/// Alias to full nonsense. |
| 258 |
using FullNonsense = Nonsense<11, snowflake::Ontology<11>>; |
| 259 |
} |
| 260 |
|
| 261 |
/** More complicated templates with special characters, doing it on classes |
| 262 |
* intentionally (no functions allowed :p). These are mostly here for help |
| 263 |
* testing limits on tokenize_template edge cases. The tokenize_template |
| 264 |
* function may not even be called with these, but the python test cases |
| 265 |
* have these in there and this code is checked for compilation. */ |
| 266 |
namespace complex { |
| 267 |
/// variadic folding stuff |
| 268 |
template <typename... Ts> |
| 269 |
struct Fold { |
| 270 |
/// make sure your types actually operator+... |
| 271 |
static auto sum(Ts... ts) { return (ts + ...); } |
| 272 |
}; |
| 273 |
|
| 274 |
/// https://en.cppreference.com/w/cpp/types/void_t |
| 275 |
template <class...> |
| 276 |
using void_t = void; |
| 277 |
|
| 278 |
/// primary template handles types that have no nested `type` member: |
| 279 |
/// https://en.cppreference.com/w/cpp/types/void_t |
| 280 |
/// the equals sign yo |
| 281 |
template <class, class=void> |
| 282 |
struct has_type_member : std::false_type { |
| 283 |
/// returns `false`. |
| 284 |
bool meh() const { return false; } |
| 285 |
}; |
| 286 |
|
| 287 |
/// specialization recognizes types that do have a nested `type` member: |
| 288 |
/// https://en.cppreference.com/w/cpp/types/void_t |
| 289 |
template <class T> |
| 290 |
struct has_type_member<T, void_t<typename T::type>> : std::true_type { |
| 291 |
/// returns `true` |
| 292 |
bool meh() const { return true; } |
| 293 |
}; |
| 294 |
|
| 295 |
/// pure nonsense |
| 296 |
template <const int* I> |
| 297 |
struct IntPtr { |
| 298 |
/// gives back the I |
| 299 |
int i() { return *I; } |
| 300 |
}; |
| 301 |
|
| 302 |
/// more nonsense |
| 303 |
template <const int& I> |
| 304 |
struct IntRef { |
| 305 |
/// gives back the I |
| 306 |
int i() { return I; } |
| 307 |
}; |
| 308 |
} |
| 309 |
} |