├── .gitattributes ├── .gitignore ├── .streak ├── LICENSE.txt ├── Main.cpp ├── README.md ├── all_of.hpp ├── amount.hpp ├── apply.hpp ├── compile_time_while.hpp ├── constant_parameter.hpp ├── constexpr_for.hpp ├── constexpr_map.hpp ├── constexpr_range.hpp ├── constexpr_switch.hpp ├── constexpr_tuple_accumulate.hpp ├── constexpr_type_switch.hpp ├── constexpr_while.hpp ├── constructable_with.hpp ├── count_if.hpp ├── enumerate_pack.hpp ├── find.hpp ├── fixed_string.hpp ├── for_each.hpp ├── for_each_index.hpp ├── get.hpp ├── get_nth_element.hpp ├── index_sequence.hpp ├── inlin_if.hpp ├── integral_constant.hpp ├── iota.hpp ├── loop.hpp ├── meta_struct.hpp ├── mlib.sln ├── mlib.vcxproj ├── mlib.vcxproj.filters ├── on_all.hpp ├── one_of.cpp ├── one_of.hpp ├── operation_stack.hpp ├── overload.hpp ├── pack.hpp ├── pack_find.hpp ├── parse_rules.hpp ├── refl_get.hpp ├── repeatedFunctionArguement.hpp ├── select.hpp ├── string_manip.hpp ├── string_parse.hpp ├── sub_pack.hpp ├── sub_tuple.hpp ├── swap.hpp ├── times.hpp ├── trait.hpp ├── transform.hpp ├── tuple.hpp ├── tuple_equal.hpp ├── tuple_find.hpp ├── tuple_inheritance.hpp ├── tuple_reverse.hpp ├── type.hpp ├── value_range.hpp └── value_sequence.hpp /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /.streak: -------------------------------------------------------------------------------- 1 | 2023-03-22T13:33:33+00:00 2 | 2023-03-22T13:33:33+00:00 3 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Robert Shepherd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Main.cpp: -------------------------------------------------------------------------------- 1 | #include // std::tuple 2 | #include // std::cout 3 | #include // std::make_index_sequence 4 | 5 | #include "for_each.hpp" // mlib::for_each 6 | #include "tuple.hpp" // mlib::tuple 7 | #include "transform.hpp" // mlib::transform 8 | #include "select.hpp" // mlib::select 9 | #include "find.hpp" // mlib::find 10 | #include "tuple_reverse.hpp" // mlib::tuple_reverse 11 | #include "constexpr_for.hpp" // mlib::constexpr_for 12 | #include "constexpr_while.hpp" // mlib::constexpr_while 13 | #include "refl_get.hpp" // mlib::refl_get 14 | #include "get_nth_element.hpp" // mlib::get_nth_element 15 | #include "pack.hpp" // mlib::value_pack 16 | #include "constexpr_map.hpp" // mlib::constexpr_map 17 | #include "count_if.hpp" // mlib::count_if 18 | #include "pack_find.hpp" // mlib::pack_find 19 | #include "parse_rules.hpp" // mlib::parse_rules 20 | #include "constexpr_switch.hpp" // mlib::constexpr_switch 21 | #include "constexpr_range.hpp" // mlib::constexpr_range 22 | #include "string_parse.hpp" // mlib::string_parse 23 | #include "compile_time_while.hpp" // mlib::compile_time_while 24 | #include "enumerate_pack.hpp" // mlib::enumerate_pack 25 | 26 | int main() 27 | { 28 | // mlib::for_each 29 | std::tuple t{ 42, 'c', true, 3.142 }; 30 | mlib::for_each<[](auto t) { std::cout << "val: " << t << "\n"; }>(t); 31 | 32 | // mlib::transform 33 | std::tuple t2{ 42, 'c', 3.142 }; 34 | mlib::transform(t2, [](auto& t) {return static_cast(t); }); 35 | 36 | // mlib::select 37 | std::tuple t3{ 'c', true, 3.142 }; 38 | auto tuple = mlib::select(t3, std::index_sequence<1, 2>{}); 39 | mlib::for_each<[](auto& t) {std::cout << "val: " << t << "\n";} >(tuple); 40 | 41 | // note need to add to less than: --\/, will print one hello. 42 | mlib::constexpr_while < 0, [](int t) {t++; return t < 3; }, [&]() {std::cout << "Hello\n"; }, [](int t) {return t + 1; } > (); 43 | 44 | // highest number of recursion. 45 | mlib::constexpr_while < 0, [](int t) {t++; return t < 501; }, []() {std::cout << "."; }, [](int t) {return t + 1; } > (); 46 | 47 | std::cout << "\n\n\n"; 48 | 49 | mlib::compile_time_while < 0, [](int t) {return t < 5; }, [](int t) {return t + 1; }, []() {std::cout << "."; } > (); 50 | 51 | struct foo { int a; double b; char c; }; 52 | foo f{42, 3.14, 'c'}; 53 | auto x = mlib::meta::refl_get(f); 54 | std::cout << std::get<0>(x); 55 | 56 | std::cout << mlib::get_nth_element<2>(42, 'c', 3.14, true) << "\n"; // returns 3.14 57 | 58 | mlib::value_pack<42, 'c', 3.142, true> v{}; 59 | 60 | mlib::constexpr_map < mlib::member_map<42, 'c'>{}, mlib::member_map{} > map{}; 61 | 62 | std::cout << map[mlib::constexpr_parameter{}]; 63 | 64 | constexpr auto z = map.lookup<42>(); 65 | std::cout << z; 66 | 67 | std::tuple tuple_count{ 0, 31.4, 5, 7, 0 }; 68 | std::cout << mlib::count_if([](auto t) {return t > 0; }, tuple_count); 69 | 70 | std::cout << mlib::pack_find<42, 'c', true, 3, 12, 'b', 42, false, 5, 'y'>(); 71 | std::cout << mlib::pack_find<'n', 'c', true, 3, 12, 'b', 42, false, 5, 'y'>(); 72 | 73 | std::cout << "\n"; 74 | 75 | constexpr auto fixed = mlib::fixed_string{ "hello this is me" }; 76 | 77 | constexpr auto result_of_branches = 78 | mlib::parse_rules{}.if_has_character<'h'>([&](auto x) {return mlib::fixed_string{ "ey i don't want a aije" }; }) 79 | .if_has_character<'a'>([&](auto x) {return mlib::fixed_string{ "I dont wnt n eiy" }; }) 80 | .if_has_character<'t'>([&](auto x) {return mlib::fixed_string{ "I dont think that this will happen\n" }; }) 81 | .if_has_character<' '>([&](auto x) {return mlib::fixed_string{ x.data }; }); 82 | mlib::constexpr_switch < 83 | 0 , [](mlib::constexpr_parameter<0>) { std::cout << "it is 0\n"; }, 84 | [](mlib::constexpr_parameter<5>) { std::cout << "it is not 0\n"; }, 85 | [](mlib::constexpr_parameter<'c'>) { std::cout << "it is 'c'\n"; }, 86 | [](mlib::constexpr_parameter) { std::cout << "it is true\n"; }, 87 | [](auto) { std::cout << "I don't know what it is!\n"; } > 88 | value{}; 89 | value(); 90 | 91 | constexpr auto value_index = mlib::constexpr_range<1, 2, 3, 4, 5, 6, 8>{} 92 | .add<5>() 93 | .pop_front() 94 | .map < [](auto x) {return x + 1; } > () 95 | .map_with_location < [](auto x, auto idx) {return x + idx; } > () 96 | .join_with<0, 1, 2, 3, 4>() 97 | .add(mlib::Size{}) 98 | .remove_at<0>() 99 | .add<4>() 100 | .at<4>(); 101 | 102 | constexpr auto parser = mlib::string_parse{}; 103 | 104 | std::cout << parser.template alternate_blur<'-', '+'>().data; 105 | 106 | mlib::enumerate_pack < [](auto index, auto value) {std::cout << index << ": " << value; }, 4, 'c', true, false, 54, 'y', 3.14 > (); 107 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mlib 2 | 3 | *mlib* is a fast, simple c++ metaprogramming library that uses c++20. That also has a tiny bit of reflection! 4 | 5 | ### Motivating Example 6 | 7 | ```C++ 8 | #include // std::tuple 9 | #include // std::cout 10 | #include // std::make_index_sequence 11 | 12 | #include "for_each.hpp" // mlib::for_each 13 | #include "tuple.hpp" // mlib::tuple 14 | #include "transform.hpp" // mlib::transform 15 | #include "select.hpp" // mlib::select 16 | #include "find.hpp" // mlib::find 17 | #include "tuple_reverse.hpp" // mlib::tuple_reverse 18 | #include "constexpr_for.hpp" // mlib::constexpr_for 19 | #include "constexpr_while.hpp" // mlib::constexpr_while 20 | #include "refl_get.hpp" // mlib::refl_get 21 | #include "get_nth_element.hpp" // mlib::get_nth_element 22 | #include "pack.hpp" // mlib::value_pack 23 | #include "constexpr_map.hpp" // mlib::constexpr_map 24 | #include "count_if.hpp" // mlib::count_if 25 | #include "pack_find.hpp" // mlib::pack_find 26 | #include "parse_rules.hpp" // mlib::parse_rules 27 | #include "constexpr_switch.hpp"// mlib::constexpr_switch 28 | #include "constexpr_range.hpp" // mlib::constexpr_range 29 | 30 | int main() 31 | { 32 | // mlib::for_each 33 | std::tuple t{ 42, 'c', true, 3.142 }; 34 | mlib::for_each(t, [](auto& t) { std::cout << "val: " << t << "\n"; }); 35 | 36 | // mlib::transform 37 | std::tuple t2{ 42, 'c', 3.142 }; 38 | mlib::transform(t2, [](auto& t) {return static_cast(t); }); 39 | 40 | // mlib::select 41 | std::tuple t3{ 'c', true, 3.142 }; 42 | auto tuple = mlib::select(t3, std::index_sequence<1, 2>{}); 43 | mlib::for_each(tuple, [](auto& t) {std::cout << "val: " << t << "\n"; }); 44 | 45 | // note need to add to less than: --\/, will print one hello. 46 | mlib::constexpr_while < 0, [](int t) {t++; return t < 3; }, [&]() {std::cout << "Hello\n"; }, [](int t) {return t + 1; } > (); 47 | 48 | // highest number of recursion. 49 | mlib::constexpr_while < 0, [](int t) {t++; return t < 501; }, [&]() {std::cout << "."; }, [](int t) {return t + 1; } > (); 50 | 51 | struct foo { int a; double b; char c; }; 52 | foo f{42, 3.14, 'c'}; 53 | auto x = mlib::meta::refl_get(f); 54 | std::cout << std::get<0>(x); 55 | 56 | std::cout << mlib::get_nth_element<2>(42, 'c', 3.14, true) << "\n"; // returns 3.14 57 | 58 | mlib::value_pack<42, 'c', 3.142, true> v{}; 59 | 60 | mlib::constexpr_map < mlib::member_map<42, 'c'>{}, mlib::member_map{} > map{}; 61 | 62 | std::cout << map[mlib::constexpr_parameter{}]; 63 | 64 | constexpr auto z = map.lookup<42>(); 65 | std::cout << z; 66 | 67 | std::tuple tuple_count{ 0, 31.4, 5, 7, 0 }; 68 | std::cout << mlib::count_if([](auto t) {return t > 0; }, tuple_count); 69 | 70 | std::cout << mlib::pack_find<42, 'c', true, 3, 12, 'b', 42, false, 5, 'y'>(); 71 | std::cout << mlib::pack_find<'n', 'c', true, 3, 12, 'b', 42, false, 5, 'y'>(); 72 | 73 | constexpr auto fixed = mlib::fixed_string{ "hello this is me" }; 74 | 75 | constexpr auto result_of_branches = 76 | mlib::parse_rules{}.if_has_character<'c'>([&](auto x) {return mlib::fixed_string{ "ey i don't want a aije" }; }) 77 | .if_has_character<'a'>([&](auto x) {return mlib::fixed_string{ "I dont wnt n eiy" }; }) 78 | .if_has_character<'t'>([&](auto x) {return mlib::fixed_string{ "I dont think that this will happen\n" }; }) 79 | .if_has_character<' '>([&](auto x) {return mlib::fixed_string{ x.data }; }); 80 | mlib::constexpr_switch < 81 | 5, [](mlib::constexpr_parameter<0>) { std::cout << "it is 0\n"; }, 82 | [](mlib::constexpr_parameter<5>) { std::cout << "it is not 0\n"; }, 83 | [](auto) { std::cout << "I don't know what it is!\n"; } > 84 | value{}; 85 | value(); 86 | 87 | constexpr auto value_index = mlib::constexpr_range<1, 2, 3, 4, 5, 6>{} 88 | .add<5>() 89 | .pop_front() 90 | .map < [](auto x) {return x + 1; } > () 91 | .map_with_location < [](auto x, auto idx) {return x + idx; } > () 92 | .join_with<0, 1, 2, 3, 4>() 93 | .add(mlib::Size{}) 94 | .remove<0>() 95 | .at<10>(); 96 | } 97 | ``` 98 | # Documentation 99 | 100 | ### `mlib::for_each` 101 | 102 | `mlib::for_each` is a `constexpr` function that takes two arguements `std::tuple` and a callable, for instance a lambda. `mlib::for_each`'s has the following outline: 103 | ```C++ 104 | template 105 | constexpr auto for_each(std::tuple& tup, auto& callable) 106 | { 107 | // delegate to for_each_impl 108 | } 109 | ``` 110 | An example use case is passing a lambda expression that outputs the elements in the `std::tuple`: 111 | 112 | ```C++ 113 | std::tuple t{42, 'c', true, 3.142}; 114 | mlib::for_each(t, [&](auto& a){std::cout << t << ", ";}) 115 | ``` 116 | And the output would be the following: 117 | ``` 118 | 42, c, t, 1, 3.142, 119 | ``` 120 | The benchmark for this is as [follows](https://godbolt.org/z/Wq49Yrjo1): 121 | ``` 122 | ------------------------------------------------------ 123 | Benchmark Time CPU Iterations 124 | ------------------------------------------------------ 125 | BM_for_each 144 ns 82.6 ns 8173558 126 | ``` 127 | ------------------------------------------------------------------------------------------------------------------------------------------------------------ 128 | ### `mlib::transform` 129 | `mlib::transform` is a `constexpr` function takes a given tuple, `t` and a given function `f` that transforms one of the elements inside of the tuple. An example invocation of `mlib::transfrom` looks like this: 130 | ```C++ 131 | std::tuple t{1, 4, 7, 'c', true, false}; 132 | mlib::transform(t, [](auto& t){return decltype(t)(static_cast(t));}); 133 | ``` 134 | This invokation, has a tuple t: `std::tuple` and then transforms that tuple using the `[](auto t) { return (static_cast(t)) + 1; };`, and does so accordingly. 135 | 136 | `mlib::transform`'s implementation looks like this: 137 | ```C++ 138 | template 139 | constexpr auto transform_helper(std::tuple t, auto lambda, 140 | std::index_sequence i_s) { 141 | return std::make_tuple(lambda(std::get(t))...); 142 | } 143 | 144 | template 145 | constexpr auto transform(std::tuple t, auto lambda) { 146 | return transform_helper(t, lambda, 147 | std::make_index_sequence{}); 148 | } 149 | ``` 150 | It is a very simple implementation, and `mlib::transform` is a great tool. 151 | 152 | The benchmark is as [follows](https://godbolt.org/z/T73f7GhbP): 153 | ``` 154 | ------------------------------------------------------- 155 | Benchmark Time CPU Iterations 156 | ------------------------------------------------------- 157 | BM_Transform 307 ns 173 ns 3620400 158 | ``` 159 | 160 | ---------------------------------------------------------------------------------------------------------------------------------------------------------- 161 | ### `mlib::fixed_string`. 162 | `mlib::transform` is a string that can be used at compile time, so the size needs to be known at compile time and needs to be passed as an [NTTP](https://stackoverflow.com/questions/70924025/c20-nttp-specialization) or it can be infered by the `constexpr` constructor. Here is how you would have a compile time string, using `fixed_string`: 163 | ```C++ 164 | fixed_string<6> f{"hello"}; // note that `fixed_string` has a `constexpr` constructor taking an array. 165 | ``` 166 | The only hardship is specifying the [NTTP](https://stackoverflow.com/questions/70924025/c20-nttp-specialization). An example implementation of `fixed_string` is like this: 167 | ```C++ 168 | template // I is the size of the string 169 | struct fixed_string 170 | { 171 | // notice the constexpr constructor. 172 | constexpr fixed_string(char(&arr)[I]) 173 | { 174 | // copies `arr` into `m_data` 175 | } 176 | 177 | char m_data[I] = {}; 178 | }; 179 | ``` 180 | 181 | --------------------------------------------------------------------------------------------------- 182 | ### `mlib::all_of` 183 | `mlib::all_of` is a simple feature that checks if all of the booleans, `bools` passed in are all `true`. A simple example is as follows: 184 | ```C++ 185 | mlib::all_of(true, true, 6 == 6, true, 42 == 0, true); // false 186 | ``` 187 | Another example is as follows: 188 | ```C++ 189 | mlib::all_of(true, true, true, true); // true 190 | ``` 191 | So it is a very useful feature, and can be used in many different ways, and example implementation of `mlib::all_of` is as follows: 192 | ```C++ 193 | namespace mlib 194 | { 195 | template 196 | struct all_of 197 | { 198 | static constexpr auto value = b && all_of::value; 199 | }; 200 | 201 | template 202 | struct all_of 203 | { 204 | static constexpr auto value = b; 205 | }; 206 | } // namespace mlib 207 | ``` 208 | --------------------------------------------------------------------------------------------------------------------------------- 209 | ### `mlib::amount` 210 | 211 | `mlib::amount` allows you do do things like you would do in a run time loop but at compile time. You can specify the number of recursions in the `<>` template parameter part. It is a very easy think to use, and can happen at compile time, as long as the callable passed in can run at compile time. Here is an example usage: 212 | ```C++ 213 | amount<10>.times([](){std::cout << "hello" << "\n";}); // note that this doesn't happen at compile time 214 | amount<10>.times([](){0 + 257;}); // happens at compile time, but the result isn't really observable. 215 | ``` 216 | So, as shown it is a very useful feature. An example implementation is as follows: 217 | ```C++ 218 | template 219 | struct amount 220 | { 221 | constexpr auto times(auto&& lambda) const; 222 | }; 223 | 224 | template 225 | static constexpr auto amount_t = amount{}; 226 | 227 | template 228 | constexpr auto amount::times(auto&& lambda) const 229 | { 230 | lambda(); 231 | if constexpr (T - 1 != 0) 232 | { 233 | amount_t.times(lambda); 234 | } 235 | return true; 236 | } 237 | ``` 238 | ----------------------------------------------------------------------------------------------------------------------------------------------- 239 | ### `mlib::one_of` 240 | 241 | `mlib::one_of` is a compile time `or`. You pass in a bunch of bools, and if one of then is `true`, the value that the class `mlib::one_of` holds will be `true`. An example use of `mlib::one_of` is as follows: 242 | ```C++ 243 | bool b = mlib::one_of::value; // b == false 244 | bool c = mlib::one_of::value; // c == true 245 | ``` 246 | So it is very easy to use. An example implementation of `mlib::one_of` is as follows: 247 | ```C++ 248 | namespace mlib 249 | { 250 | template 251 | struct one_of 252 | { 253 | static constexpr bool value = (b || b...); 254 | }; 255 | 256 | template 257 | struct one_of 258 | { 259 | static constexpr bool value = b; 260 | }; 261 | } // namespace mlib 262 | ``` 263 | And thats it for its implementation, note that it's implementation is not recursive! 264 | 265 | ----------------------------------------------------------------------------------------------------------------------------------------------- 266 | ### `mlib::constexpr_map` 267 | 268 | `mlib::constexpr_map` is a compile-time map that also allows compile time lookup using the `operator[]` which takes a `mlib::constexpr_parameter` as it's compile time arguement and a `constexpr` lookup function `lookup` which takes the value to be looked up as a non type template parameter. It has a very cool implementation here is a watered down version of what it looks like: 269 | ```C++ 270 | template struct constexpr_parameter {using type = decltype(T);} 271 | 272 | template struct member_map { 273 | constexpr auto operator[](constexpr_parameter) 274 | { 275 | return B; 276 | } 277 | }; 278 | 279 | template struct constexpr_map : constexpr_parameter::type... 280 | { 281 | using decltype(members)::operator[]...; 282 | // note lookup is missed out here 283 | }; 284 | ``` 285 | But as you can see it is a very cool implementation. Now onto some examples using `mlib::constexpr_map`: 286 | ```C++ 287 | mlib::constexpr_map{}, mlib::member_map{}> map{}; 288 | std::cout << map[mlib::c_p<'c'>]; 289 | ``` 290 | Note that here we are using `mlib::c_p` which is just a `mlib::constexpr_parameter` it looks like this: 291 | ```C++ 292 | template 293 | constexpr auto c_p = constant_parameter{}; 294 | ``` 295 | So in conclusion, `mlib::constexpr_map` is a really cool feature, the only problem is that the map is `constexpr` so therefore cannot add values, change values, etc. 296 | 297 | ----------------------------------------------------------------------------------------------------------------------------------------------- 298 | ### `mlib::meta::refl_get` 299 | `mlib::meta::relf_get` is a useful reflection, that gets all of the members out of a `struct` for example if you had a struct `a`: 300 | ```C++ 301 | struct a 302 | { 303 | int a; 304 | char c; 305 | bool b; 306 | }; 307 | ``` 308 | and so if pass an instance of `a` like this: `a A{42, 'c', true};` to `refl_get` we will get a tuple with the contents of `std::tuple{42, 'c', true}`! It is as easy as that to reflect this! 309 | 310 | It looks like this: 311 | ```C++ 312 | constexpr auto refl_get(auto& t) 313 | { 314 | // implementation 315 | } 316 | ``` 317 | ----------------------------------------------------------------------------------------------------------------------------------------------- 318 | 319 | ### `mlib::string_parse` 320 | 321 | `mlib::string_parse` is a `struct` taking an `mlib::fixed_string`, `str` as a NTTP. It then has member functions: 322 | - `mlib::string_parse::substr` 323 | - `mlib::string_parse::consume_until` 324 | This, `mlib::string_parse` is a very basic example of compile-time string parsing. An example of how `mlib::string_parse` looks like is as follows: 325 | ```C++ 326 | template 327 | struct string_parse 328 | { 329 | template 330 | constexpr auto substr() -> mlib::fixed_string; 331 | 332 | template 333 | constexpr auto consume_until() -> mlib::fixed_string; 334 | 335 | template 336 | constexpr auto blur_with() const noexcept; 337 | 338 | template 339 | constexpr auto blur_until() const noexcept; 340 | }; 341 | ``` 342 | 343 | ## Star History 344 | 345 | 346 | 347 | 348 | 349 | Star History Chart 350 | 351 | 352 | 353 | -------------------------------------------------------------------------------- /all_of.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | template 6 | struct all_of 7 | { 8 | static constexpr auto value = (b && bs...); 9 | }; 10 | 11 | template 12 | struct all_of 13 | { 14 | static constexpr auto value = b; 15 | }; 16 | } // namespace mlib -------------------------------------------------------------------------------- /amount.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::index_sequence 4 | #include // std::size_t 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct amount 10 | { 11 | constexpr auto times(auto&& lambda) const; 12 | }; 13 | 14 | template 15 | static constexpr auto amount_t = amount{}; 16 | 17 | template 18 | constexpr auto amount::times(auto&& lambda) const 19 | { 20 | [] (std::index_sequence) 21 | { 22 | (lambda(), void(indexes), ...); 23 | }(std::make_index_sequence{}); 24 | return true; 25 | } 26 | }; // namespace mlib -------------------------------------------------------------------------------- /apply.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::index_sequence 4 | #include // std::tuple 5 | #include // std::size_t 6 | 7 | namespace mlib { 8 | template 9 | constexpr auto apply(auto lambda, std::index_sequence) 10 | -> decltype(lambda.template operator() < Vs... > ()) 11 | { 12 | return lambda.template operator() < Vs... > (); 13 | } 14 | } // namespace mlib -------------------------------------------------------------------------------- /compile_time_while.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace mlib 9 | { 10 | 11 | template 12 | struct pack 13 | { 14 | constexpr auto operator()() 15 | { 16 | if constexpr (!condition_for_val(start_value)) 17 | { 18 | return 0; 19 | } 20 | else 21 | { 22 | return 1 + pack{}(); 23 | } 24 | } 25 | }; 26 | 27 | template 28 | struct any_first 29 | { 30 | constexpr auto operator()() 31 | { 32 | return Care; 33 | } 34 | }; 35 | 36 | template 37 | struct compile_time_while 38 | { 39 | constexpr auto operator()() 40 | { 41 | // first of all, make a value that is how many times the operations are true. 42 | constexpr auto amount_of_iterations = pack{}(); 43 | // so now that we have the number of iterations, we need to make a tuple of size: amount_of_iterations 44 | // and then we need to do a for_each on them! simple! 45 | constexpr auto tuple_sequence = make_tuple_sequence(); 46 | // now do for_each 47 | for_each_lambda(tuple_sequence); 48 | } 49 | 50 | template 51 | constexpr auto make_tuple_sequence() 52 | { 53 | return[&](std::index_sequence) 54 | { 55 | return std::tuple{ (any_first{}())... }; 56 | }(std::make_index_sequence{}); 57 | } 58 | 59 | template 60 | constexpr auto for_each_lambda(std::tuple tup) 61 | { 62 | [&] (std::index_sequence) 63 | { 64 | (std::get(tup)(), ...); 65 | }(std::make_index_sequence{}); 66 | } 67 | }; 68 | } // namespace mlib 69 | -------------------------------------------------------------------------------- /constant_parameter.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | template 6 | struct constant_parameter 7 | { 8 | using type = decltype(T); 9 | }; 10 | 11 | template 12 | struct constexpr_parameter 13 | { 14 | using type = decltype(T); 15 | }; 16 | } -------------------------------------------------------------------------------- /constexpr_for.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib 7 | { 8 | 9 | struct with_indexes {}; 10 | 11 | template 12 | struct sequence 13 | { 14 | constexpr auto operator()() 15 | { 16 | (Values(), ...); 17 | } 18 | 19 | // requires `indexes` to be the same length as `Values` 20 | template 21 | constexpr auto operator()(std::index_sequence) 22 | { 23 | (Values(indexes), ...); 24 | } 25 | }; 26 | 27 | template 28 | constexpr auto make_sequence_impl() 29 | { 30 | if constexpr (condition(Val)) 31 | { 32 | return make_sequence_impl(); 33 | } 34 | else 35 | { 36 | return sequence{}; 37 | } 38 | } 39 | 40 | template 41 | constexpr auto make_sequence() 42 | { 43 | if constexpr (condition(Val)) 44 | { 45 | return make_sequence_impl(); 46 | } 47 | else 48 | { 49 | return []() {}; 50 | } 51 | } 52 | 53 | template 54 | struct constexpr_for 55 | { 56 | constexpr constexpr_for(auto loop_operation) 57 | { 58 | auto s = make_sequence(); 59 | auto x = [](sequence s_) 60 | { 61 | s_(); 62 | }; 63 | x(s); 64 | } 65 | 66 | constexpr constexpr_for(auto loop_operation, with_indexes) 67 | { 68 | auto s = make_sequence(); 69 | auto x = [](sequence s_) 70 | { 71 | constexpr auto x = std::make_index_sequence{}; 72 | s_(x); 73 | }; 74 | x(s); 75 | } 76 | }; 77 | } // namespace mlib -------------------------------------------------------------------------------- /constexpr_map.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "get_nth_element.hpp" 11 | #include "constant_parameter.hpp" 12 | 13 | namespace mlib { 14 | 15 | template static constexpr auto c_p = constexpr_parameter{}; 16 | 17 | template struct member_map { 18 | constexpr auto operator[](constexpr_parameter) const noexcept { 19 | return B; 20 | } 21 | 22 | constexpr auto key() { return A; } 23 | constexpr auto value() { return B; } 24 | 25 | template constexpr auto has_key() { return X == A; } 26 | template constexpr auto has_value() { return X == B; } 27 | }; 28 | 29 | template 30 | struct constexpr_map : constexpr_parameter::type... { 31 | using decltype(members)::operator[]...; 32 | 33 | template constexpr auto get_member() const noexcept -> decltype(auto) 34 | { 35 | return mlib::get_nth_element(members...); 36 | } 37 | 38 | template constexpr auto lookup() const noexcept { 39 | return this->operator[](constexpr_parameter{}); 40 | } 41 | 42 | template constexpr auto get() const noexcept { 43 | return this->operator[](constexpr_parameter{}); 44 | } 45 | 46 | template 47 | constexpr auto has_value(constexpr_parameter) const noexcept { 48 | return[](std::index_sequence) { 49 | return ((mlib::get_nth_element(members...).template has_value() || ...)) || get_nth_element(members...).template has_value(); 50 | }(std::make_index_sequence{}); 51 | } 52 | 53 | template 54 | constexpr auto has_key(constexpr_parameter) const noexcept { 55 | return[](std::index_sequence) { 56 | return ((mlib::get_nth_element(members...).template has_key() || ...)) || get_nth_element(members...).template has_key(); 57 | }(std::make_index_sequence{}); 58 | } 59 | 60 | constexpr auto get_keys() const noexcept { 61 | return[](std::index_sequence) { 62 | return std::make_tuple( 63 | (mlib::get_nth_element(members...).key())...); 64 | }(std::make_index_sequence{}); 65 | } 66 | 67 | constexpr auto get_values() const noexcept { 68 | return [] (std::index_sequence) { 69 | return std::make_tuple( 70 | (mlib::get_nth_element(members...).value())...); 71 | }(std::make_index_sequence{}); 72 | } 73 | }; 74 | } // namespace mlib -------------------------------------------------------------------------------- /constexpr_range.hpp: -------------------------------------------------------------------------------- 1 | #include // std::index_constexpr_range 2 | #include // std::size_t 3 | #include // std::tuple 4 | #include //std::cout 5 | 6 | #include "get_nth_element.hpp" 7 | 8 | namespace mlib 9 | { 10 | template 11 | struct constexpr_range; 12 | 13 | template 14 | constexpr auto make_index_holds_true() 15 | { 16 | int index = 0; 17 | int values_[sizeof...(values)] = {}; 18 | auto function = [&](auto value) 19 | { 20 | values_[index] = value; 21 | }; 22 | (function(values), ...); 23 | return std::move(values_); 24 | } 25 | 26 | template 27 | constexpr auto get_idxs_out(const T(&arr)[Idx]) 28 | { 29 | return[&](std::index_sequence) 30 | { 31 | return (get_nth_element(constexpr_range{}) + ...); 32 | }(std::make_index_sequence{}); 33 | } 34 | 35 | template 36 | struct value_constexpr_range 37 | { 38 | }; 39 | 40 | template 41 | constexpr auto pop_front_(constexpr_range) 42 | { 43 | return constexpr_range{}; 44 | } 45 | 46 | struct Size {}; 47 | 48 | template 49 | struct constexpr_range 50 | { 51 | template 52 | constexpr auto map() 53 | { 54 | return[&](std::index_sequence) 55 | { 56 | return constexpr_range{}; 57 | }(std::make_index_sequence {}); 58 | } 59 | 60 | template 61 | constexpr auto map_with_location() 62 | { 63 | return[&](std::index_sequence) 64 | { 65 | return constexpr_range{}; 66 | }(std::make_index_sequence {}); 67 | } 68 | 69 | constexpr auto sum() 70 | { 71 | constexpr auto value = [&]() 72 | { 73 | return (vals + ...); 74 | }(); 75 | return constexpr_range{}; 76 | } 77 | 78 | constexpr auto tuple() 79 | { 80 | return std::tuple{ vals... }; 81 | } 82 | 83 | template 84 | constexpr auto add() 85 | { 86 | return constexpr_range{}; 87 | } 88 | 89 | constexpr auto add(Size) 90 | { 91 | return constexpr_range{}; 92 | } 93 | 94 | constexpr auto pop_front() 95 | { 96 | return pop_front_(constexpr_range{}); 97 | } 98 | 99 | template 100 | constexpr auto operator+(constexpr_range) const 101 | { 102 | return constexpr_range{}; 103 | } 104 | 105 | template 106 | constexpr auto join_with(constexpr_range) 107 | { 108 | return constexpr_range{}; 109 | } 110 | 111 | template 112 | constexpr auto join_with() 113 | { 114 | return constexpr_range{}; 115 | } 116 | 117 | template 118 | constexpr auto remove_at() 119 | { 120 | constexpr auto first_part = [&](std::index_sequence) 121 | { 122 | return constexpr_range(vals...)...>{}; 123 | }(std::make_index_sequence{}); 124 | 125 | constexpr auto second_part = [&](std::index_sequence) 126 | { 127 | return constexpr_range(vals...)...>{}; 128 | }(std::make_index_sequence {}); 129 | 130 | return first_part + second_part; 131 | } 132 | 133 | template 134 | constexpr auto add_at() 135 | { 136 | constexpr auto first_part = [&](std::index_sequence) 137 | { 138 | return constexpr_range(vals...)...>{}; 139 | }(std::make_index_sequence{}); 140 | 141 | constexpr auto second_part = [&](std::index_sequence) 142 | { 143 | return constexpr_range(vals...)...>{}; 144 | }(std::make_index_sequence {}); 145 | 146 | return (first_part + mlib::constexpr_range{}) + second_part; 147 | } 148 | 149 | template 150 | constexpr auto at() 151 | { 152 | return get_nth_element(vals...); 153 | } 154 | 155 | template 156 | constexpr auto instances() const noexcept -> int 157 | { 158 | return[=](std::index_sequence) 159 | { 160 | return ((T == mlib::get_nth_element(vals)) + ...); 161 | }(std::make_index_sequence{}); 162 | } 163 | }; 164 | } // namespace mlib -------------------------------------------------------------------------------- /constexpr_switch.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include"constant_parameter.hpp" 4 | 5 | namespace mlib 6 | { 7 | template 8 | struct switch_expressions : constexpr_parameter::type... { 9 | using decltype(members)::operator()...; 10 | }; 11 | 12 | // a constexpr switch 13 | template struct constexpr_switch { 14 | switch_expressions m{}; 15 | 16 | constexpr auto operator()() { m(constexpr_parameter{}); } 17 | }; 18 | } // namespace mlib -------------------------------------------------------------------------------- /constexpr_tuple_accumulate.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::tuple 4 | 5 | namespace mlib { 6 | template 7 | constexpr auto constexpr_tuple_accumulate(std::tuple tup) { 8 | return[=](std::index_sequence) { 9 | return (std::get(tup) + ...); 10 | }(std::make_index_sequence{}); 11 | } 12 | } // namespace mlib -------------------------------------------------------------------------------- /constexpr_type_switch.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib { 4 | template 5 | struct constexpr_parameter { 6 | using type = decltype(T); 7 | }; 8 | 9 | template 10 | struct type {}; 11 | 12 | template 13 | struct switch_expressions : constexpr_parameter::type... { 14 | using decltype(members)::operator()...; 15 | }; 16 | 17 | template 18 | struct constexpr_type_switch { 19 | switch_expressions m{}; 20 | 21 | template 22 | constexpr auto operator()(type) { 23 | return m(type{}); 24 | } 25 | }; 26 | } // namespace mlib -------------------------------------------------------------------------------- /constexpr_while.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace mlib 6 | { 7 | template 8 | constexpr auto constexpr_while_helper() 9 | { 10 | if constexpr (Condition(Value)) 11 | { 12 | ToDo(); 13 | constexpr_while_helper(); 14 | } 15 | else 16 | { 17 | // we end loop 18 | } 19 | } 20 | 21 | template 22 | struct constexpr_while 23 | { 24 | constexpr constexpr_while() 25 | { 26 | if constexpr (Condition(Value)) 27 | { 28 | constexpr_while_helper(); 29 | } 30 | else 31 | { 32 | // we end the program. 33 | } 34 | } 35 | }; 36 | } // namespace mlib -------------------------------------------------------------------------------- /constructable_with.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | #include // std::cout 5 | #include // std::is_aggregate 6 | #include // std::index_sequence 7 | 8 | namespace mlib 9 | { 10 | template 11 | struct convertible_to_anything { 12 | template 13 | operator T() {}; 14 | }; 15 | 16 | template 17 | concept is_constructable_with = requires { 18 | [] (std::index_sequence i_s) 19 | -> decltype(T{ convertible_to_anything{}... }) { 20 | return {}; 21 | }(std::make_index_sequence{}); 22 | }; 23 | 24 | template 25 | concept aggregate_of = std::is_aggregate_v && is_constructable_with && 26 | !is_constructable_with; 27 | 28 | template 29 | constexpr auto number_of_aggregate_members = 30 | [](std::index_sequence i_s) { 31 | return ((aggregate_of *indexes) + ... + 0); 32 | }(std::make_index_sequence<12>{}); 33 | } // namespace mlib -------------------------------------------------------------------------------- /count_if.hpp: -------------------------------------------------------------------------------- 1 | // count_if.hpp 2 | #pragma once 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace mlib { 9 | template 10 | constexpr auto count_if(auto callable, std::tuple& tuple) { 11 | return[&](std::index_sequence) { 12 | return (callable(std::get(tuple)) + ...); 13 | }(std::make_index_sequence{}); 14 | } 15 | 16 | constexpr auto count_if(auto callable, auto... Ts) 17 | { 18 | return (callable(Ts) + ...); 19 | } 20 | } // namespace mlib -------------------------------------------------------------------------------- /enumerate_pack.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib 7 | { 8 | // a replacement for mlib::for_each rather than making a tuple with a pack. 9 | template 10 | constexpr auto enumerate_pack() 11 | { 12 | [&] (std::index_sequence) 13 | { 14 | (callable(indexes, pack), ...); 15 | }(std::make_index_sequence{}); 16 | } 17 | } // namespace mlib -------------------------------------------------------------------------------- /find.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::index_sequence 4 | #include // std::tuple 5 | #include // std::size_t 6 | 7 | #include"for_each.hpp" // mlib::for_each 8 | #include"tuple.hpp" // mlib::tuple 9 | #include"get.hpp" // mlib::get 10 | 11 | namespace mlib 12 | { 13 | template 14 | constexpr auto find_helper(mlib::tuple tup, std::index_sequence) 15 | { 16 | if constexpr (get(tup) == to_find) 17 | { 18 | return index; 19 | } 20 | else if constexpr (get(tup) != to_find) 21 | { 22 | return find_helper(tup.second, std::make_index_sequence{}); 23 | } 24 | } 25 | 26 | template 27 | constexpr auto find_helper(mlib::tuple tup, std::index_sequence) 28 | { 29 | if constexpr (get(tup) == to_find) 30 | { 31 | return index; 32 | } 33 | else if constexpr (get(tup) != to_find) 34 | { 35 | return -1; 36 | } 37 | } 38 | 39 | template 40 | constexpr auto find_helper(mlib::tuple tup, std::index_sequence) 41 | { 42 | if constexpr (mlib::get(tup) == to_find) 43 | { 44 | return index; 45 | } 46 | else if constexpr (get(tup) != to_find) 47 | { 48 | return find_helper(tup.second, std::index_sequence{}); 49 | } 50 | } 51 | 52 | template 53 | constexpr auto find(mlib::tuple t) -> bool 54 | { 55 | return find_helper(t, std::make_index_sequence{}); 56 | } 57 | } // namespace mlib -------------------------------------------------------------------------------- /fixed_string.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct fixed_string 10 | { 11 | constexpr fixed_string(const char(&arr)[i + 1]) 12 | { 13 | std::copy_n(arr, i + 1, data); 14 | } 15 | 16 | constexpr auto size() const { return i; } 17 | 18 | template 19 | constexpr auto nth_element() const 20 | { 21 | return data[I]; 22 | } 23 | 24 | auto operator<=>(const fixed_string&) const = default; 25 | 26 | char data[i + 1] = {}; 27 | }; 28 | 29 | template 30 | fixed_string(const char(&arr)[i])->fixed_string; 31 | } // namespace mlib -------------------------------------------------------------------------------- /for_each.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | #include // std::index_sequence, std::make_index_sequence 5 | #include // std::tuple 6 | 7 | #include"tuple.hpp" // mlib::tuple 8 | #include"get.hpp" // mlib::get 9 | 10 | namespace mlib 11 | { 12 | template 13 | constexpr auto for_each(std::tuple& t) 14 | { 15 | [=] (std::index_sequence) 16 | { 17 | (lambda(std::get(t)), ...); 18 | }(std::make_index_sequence{}); 19 | } 20 | } // namespace mlib -------------------------------------------------------------------------------- /for_each_index.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace mlib { 9 | template 10 | constexpr auto for_each_index(std::tuple tuple) { 11 | [&] (std::index_sequence) { 12 | (lamda(indexes, std::get(tuple)), ...); 13 | }(std::make_index_sequence{}); 14 | } 15 | } // namespace mlib -------------------------------------------------------------------------------- /get.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include"tuple.hpp" // mlib::tuple 4 | 5 | namespace mlib 6 | { 7 | template 8 | constexpr auto get(auto& tup) -> decltype(auto) 9 | { 10 | if constexpr (Index == 0) 11 | { 12 | return tup.first; 13 | } 14 | else if constexpr (Index != 0) 15 | { 16 | return get(tup.second); 17 | } 18 | } 19 | } // namespace mlib -------------------------------------------------------------------------------- /get_nth_element.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib { 7 | template constexpr auto get_nth_element(auto... args) { 8 | return[&](std::index_sequence) { 9 | return [](decltype((void*)Indexes)... DontCare, auto* arg, 10 | auto *...DontCareEither) { return *arg; }(&args...); 11 | }(std::make_index_sequence{}); 12 | } 13 | } // namespace mlib -------------------------------------------------------------------------------- /index_sequence.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | 5 | namespace mlib 6 | { 7 | // index sequence only 8 | template 9 | struct index_sequence 10 | { 11 | }; 12 | 13 | template 14 | struct index_sequence_helper : public index_sequence_helper 15 | { 16 | }; 17 | 18 | template 19 | struct index_sequence_helper<0U, Next ... > 20 | { 21 | using type = index_sequence; 22 | }; 23 | 24 | template 25 | using make_index_sequence = typename index_sequence_helper::type; 26 | } // namespace mlib -------------------------------------------------------------------------------- /inlin_if.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | struct SameAs {}; 6 | 7 | // this is a bad implementation, becuase it requires operator== 8 | template struct is_same { 9 | static constexpr bool value = false; 10 | }; 11 | 12 | template 13 | requires(A{} == B{}) 14 | struct is_same { 15 | static constexpr bool value = true; 16 | }; 17 | 18 | template 19 | struct is_same_value 20 | { 21 | static constexpr auto value = (T == B); 22 | }; 23 | 24 | template 26 | struct inline_if { 27 | constexpr inline_if() {} 28 | 29 | constexpr auto get() { 30 | } 31 | constexpr auto operator()() { return get(); } 32 | 33 | static constexpr auto first = first_; 34 | static constexpr auto second = second_; 35 | }; 36 | 37 | template 38 | struct inline_if, ToCompare, first_, second_> 39 | { 40 | constexpr inline_if() {} 41 | 42 | constexpr auto get() { 43 | if constexpr (is_same::value) { 44 | return first; 45 | } 46 | else if constexpr (!is_same::value) { 47 | return second; 48 | } 49 | constexpr auto operator()() { return get(); } 50 | 51 | static constexpr auto first = first_; 52 | static constexpr auto second = second_; 53 | }; 54 | 55 | template 56 | struct inline_if, ToCompare, first_, second_> 57 | { 58 | constexpr inline_if() {} 59 | 60 | constexpr auto get() { 61 | if constexpr (is_same::value) { 62 | return first; 63 | } 64 | else if constexpr (!is_same::value) { 65 | return second; 66 | } 67 | constexpr auto operator()() { return get(); } 68 | 69 | static constexpr auto first = first_; 70 | static constexpr auto second = second_; 71 | }; 72 | } // namespace mlib -------------------------------------------------------------------------------- /integral_constant.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | template struct integral_constant { 6 | static constexpr T value = I; 7 | using value_type = T; 8 | 9 | constexpr operator value_type() const noexcept { return value; } 10 | 11 | constexpr auto operator()() const noexcept { return value; } 12 | }; 13 | } // namespace mlib -------------------------------------------------------------------------------- /iota.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | constexpr auto iota() 9 | { 10 | return[](std::index_sequence) 11 | { 12 | return std::make_tuple(indexes...); 13 | }(std::make_index_sequence{}); 14 | } -------------------------------------------------------------------------------- /loop.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct loop 10 | { 11 | constexpr loop(auto lambda) 12 | { 13 | [&] (std::index_sequence) 14 | { 15 | (lambda, void(indexes), ...); 16 | }(std::make_index_sequence{}); 17 | } 18 | }; 19 | } // namespace mlib -------------------------------------------------------------------------------- /meta_struct.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::copy_n 4 | #include // std::size_t 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct c_p { using type = decltype(T); }; 10 | 11 | template 12 | struct fixed_string 13 | { 14 | constexpr fixed_string(const char(&arr)[i + 1]) 15 | { 16 | std::copy_n(arr, i + 1, data); 17 | } 18 | 19 | auto operator<=>(const fixed_string&) const = default; 20 | 21 | char data[i + 1] = {}; 22 | }; 23 | 24 | template 25 | fixed_string(const char(&arr)[i])->fixed_string; 26 | 27 | template 28 | struct member 29 | { 30 | static constexpr auto tag() { return str; } 31 | T value; 32 | constexpr auto type() { return T{}; } 33 | }; 34 | 35 | template 36 | struct value_ 37 | { 38 | template 39 | constexpr auto operator=(c_p) 40 | { 41 | return member{}; 42 | } 43 | }; 44 | 45 | template 46 | inline constexpr auto value = value_{}; 47 | 48 | template 49 | struct meta_struct : Members... 50 | { 51 | static constexpr auto members_size = (sizeof(Members) + ... + 0); 52 | }; 53 | 54 | template 55 | constexpr decltype(auto) get(member& m) 56 | { 57 | return (m.value); 58 | } 59 | 60 | template 61 | constexpr decltype(auto) get(Meta_struct&& meta) 62 | { 63 | return get(std::forward(meta)); 64 | } 65 | } // namespace mlib -------------------------------------------------------------------------------- /mlib.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32929.385 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mlib", "mlib.vcxproj", "{2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Debug|x64.ActiveCfg = Debug|x64 17 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Debug|x64.Build.0 = Debug|x64 18 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Debug|x86.ActiveCfg = Debug|Win32 19 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Debug|x86.Build.0 = Debug|Win32 20 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Release|x64.ActiveCfg = Release|x64 21 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Release|x64.Build.0 = Release|x64 22 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Release|x86.ActiveCfg = Release|Win32 23 | {2448DC7E-C79C-4273-83D6-9D0A5CFB8D62}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {B6A30F07-B6C8-485F-A9D8-2B6280ABC47F} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /mlib.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {2448dc7e-c79c-4273-83d6-9d0a5cfb8d62} 25 | mlib 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v143 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v143 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v143 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v143 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | true 77 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 78 | true 79 | stdcpp20 80 | 81 | 82 | Console 83 | true 84 | 85 | 86 | 87 | 88 | Level3 89 | true 90 | true 91 | true 92 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 93 | true 94 | stdcpp20 95 | 96 | 97 | Console 98 | true 99 | true 100 | true 101 | 102 | 103 | 104 | 105 | Level3 106 | true 107 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 108 | true 109 | stdcpp20 110 | 111 | 112 | Console 113 | true 114 | 115 | 116 | 117 | 118 | Level3 119 | true 120 | true 121 | true 122 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 123 | true 124 | stdcpp20 125 | 126 | 127 | Console 128 | true 129 | true 130 | true 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | -------------------------------------------------------------------------------- /mlib.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | Header Files 29 | 30 | 31 | Header Files 32 | 33 | 34 | Header Files 35 | 36 | 37 | Header Files 38 | 39 | 40 | Header Files 41 | 42 | 43 | Header Files 44 | 45 | 46 | Header Files 47 | 48 | 49 | Header Files 50 | 51 | 52 | Header Files 53 | 54 | 55 | Header Files 56 | 57 | 58 | Header Files 59 | 60 | 61 | Header Files 62 | 63 | 64 | Header Files 65 | 66 | 67 | Header Files 68 | 69 | 70 | Header Files 71 | 72 | 73 | Header Files 74 | 75 | 76 | Header Files 77 | 78 | 79 | Header Files 80 | 81 | 82 | Header Files 83 | 84 | 85 | Header Files 86 | 87 | 88 | Header Files 89 | 90 | 91 | Header Files 92 | 93 | 94 | Header Files 95 | 96 | 97 | Header Files 98 | 99 | 100 | Header Files 101 | 102 | 103 | Header Files 104 | 105 | 106 | Header Files 107 | 108 | 109 | Header Files 110 | 111 | 112 | Header Files 113 | 114 | 115 | Header Files 116 | 117 | 118 | Header Files 119 | 120 | 121 | Header Files 122 | 123 | 124 | Header Files 125 | 126 | 127 | Header Files 128 | 129 | 130 | Header Files 131 | 132 | 133 | Header Files 134 | 135 | 136 | Header Files 137 | 138 | 139 | Header Files 140 | 141 | 142 | Header Files 143 | 144 | 145 | Header Files 146 | 147 | 148 | Header Files 149 | 150 | 151 | Header Files 152 | 153 | 154 | Header Files 155 | 156 | 157 | Header Files 158 | 159 | 160 | Header Files 161 | 162 | 163 | Header Files 164 | 165 | 166 | Header Files 167 | 168 | 169 | Header Files 170 | 171 | 172 | Header Files 173 | 174 | 175 | 176 | 177 | Source Files 178 | 179 | 180 | Header Files 181 | 182 | 183 | -------------------------------------------------------------------------------- /on_all.hpp: -------------------------------------------------------------------------------- 1 | // a c++20 alternative to mlib::for_each 2 | #pragma once 3 | 4 | #include // std::tuple 5 | 6 | namespace mlib 7 | { 8 | template 9 | constexpr auto on_all(Tuple&& t, Func&& f) -> void 10 | { 11 | std::apply([&](auto&&... args) { (f(args), ...); }, std::forward(t)); 12 | } 13 | } // namespace mlib -------------------------------------------------------------------------------- /one_of.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robertshepherdcpp/mlib/cd2098385e523678cd9bcc25948e134de008d750/one_of.cpp -------------------------------------------------------------------------------- /one_of.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | template 6 | struct one_of 7 | { 8 | static constexpr bool value = (c ||...); 9 | }; 10 | } // namespace mlib -------------------------------------------------------------------------------- /operation_stack.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include"constant_parameter.hpp" 8 | 9 | namespace mlib 10 | { 11 | struct run_all_t {}; 12 | static constexpr auto run_all = run_all_t{}; 13 | 14 | template 15 | constexpr auto for_each_pack() 16 | { 17 | [&] (std::index_sequence) 18 | { 19 | (callable(operations), ...); 20 | }(std::make_index_sequence{}); 21 | } 22 | 23 | template 24 | struct operation_stack 25 | { 26 | template 27 | constexpr auto enque(mlib::constexpr_parameter) 28 | { 29 | return operation_stack{}; 30 | } 31 | 32 | template 33 | constexpr auto run() 34 | { 35 | for_each_pack < [](auto& x) {x(); }, operations... > (); 36 | }; 37 | 38 | }; 39 | } // namespace mlib -------------------------------------------------------------------------------- /overload.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | template 6 | struct overload : Ts... 7 | { 8 | using Ts::operator()...; 9 | }; 10 | } // namespace mlib -------------------------------------------------------------------------------- /pack.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include"get_nth_element.hpp" 9 | #include"constant_parameter.hpp" 10 | 11 | namespace mlib { 12 | 13 | template 14 | struct type_holder {}; 15 | 16 | template 17 | struct index_pack {}; 18 | 19 | template 20 | struct type_pack { 21 | static constexpr auto pack_size = (sizeof(Ts) + ... + 0); 22 | 23 | using start_type = decltype(mlib::get_nth_element<0>((Ts{})...)); 24 | using end_type = decltype(mlib::get_nth_element<(pack_size - (pack_size - 1)) / 2>((Ts{})...)); 25 | 26 | static constexpr auto start_value = 27 | decltype(mlib::get_nth_element<0>((Ts{})...)){}; 28 | static constexpr auto end_value = 29 | decltype(mlib::get_nth_element<(pack_size - (pack_size - 1)) / 2>((Ts{})...)){}; 30 | constexpr auto begin() { return start_value; } 31 | constexpr auto end() { return end_value; } 32 | 33 | template 34 | constexpr auto operator[](mlib::constexpr_parameter) { 35 | return mlib::get_nth_element((Ts{})...); 36 | } 37 | 38 | template 39 | constexpr auto get() { 40 | return mlib::get_nth_element((Ts{})...); 41 | } 42 | 43 | constexpr type_pack() {} 44 | }; 45 | 46 | template 47 | struct value_pack { 48 | static constexpr auto begin_value = mlib::get_nth_element<0>(Ts...); 49 | static constexpr auto end_value = 50 | mlib::get_nth_element(Ts...); 51 | static constexpr auto size_value = sizeof...(Ts) - 1; 52 | 53 | constexpr auto size() { return size_value; } 54 | 55 | constexpr auto begin() { return begin_value; } 56 | constexpr auto end() { return end_value; }; 57 | 58 | template 59 | constexpr auto operator[](constexpr_parameter) { 60 | return mlib::get_nth_element(Ts...); 61 | } 62 | 63 | template 64 | constexpr auto for_each_helper(auto& lambda, std::index_sequence) 65 | { 66 | return mlib::value_pack(Ts...))...>{}; 67 | } 68 | 69 | constexpr auto for_each(auto& lambda) 70 | { 71 | return for_each_helper(lambda, std::make_index_sequence < sizeof...(Ts)>{}); 72 | } 73 | 74 | template 75 | constexpr auto get() { 76 | return mlib::get_nth_element(Ts...); 77 | } 78 | 79 | template 80 | constexpr auto select(std::index_sequence i_s) 81 | { 82 | return value_pack<(mlib::get_nth_element(Ts...))...>{}; 83 | } 84 | 85 | constexpr value_pack() { /*Does Nothing!*/} 86 | 87 | constexpr auto tuple() { return std::tuple{Ts...}; } 88 | }; 89 | } // namespace mlib -------------------------------------------------------------------------------- /pack_find.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct first 10 | { 11 | constexpr auto operator()() 12 | { 13 | return A; 14 | } 15 | }; 16 | 17 | template 18 | struct value_is_value 19 | { 20 | constexpr auto operator()() const 21 | { 22 | if constexpr (decltype(value_two)(value) == value_two) 23 | { 24 | return Index; 25 | } 26 | else 27 | { 28 | return 0; 29 | } 30 | } 31 | 32 | constexpr value_is_value() {} 33 | }; 34 | 35 | template 36 | constexpr auto pack_find() 37 | { 38 | constexpr auto x = [&](std::index_sequence) 39 | { 40 | return (value_is_value{}() + ...); 41 | }(std::make_index_sequence{}); 42 | if constexpr ((x == 0) && mlib::first{}() != value_to_find) 43 | { 44 | return -1; 45 | } 46 | 47 | return x; 48 | } 49 | } // namespace mlib -------------------------------------------------------------------------------- /parse_rules.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include"fixed_string.hpp" 4 | #include"string_parse.hpp" 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct parse_rules { 10 | template 11 | constexpr auto if_has_character(auto lambda) 12 | { 13 | if constexpr (string_parse{}.template character_occurences() > 1) 14 | { 15 | return parse_rules{}; 16 | } 17 | else 18 | { 19 | return parse_rules{}; 20 | } 21 | } 22 | 23 | template 24 | constexpr auto has_sizeof(auto lambda) 25 | { 26 | if constexpr (str.size()) 27 | { 28 | return lambda(str); 29 | } 30 | else 31 | { 32 | return str; 33 | } 34 | } 35 | }; 36 | } // namespace mlib -------------------------------------------------------------------------------- /refl_get.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | #include // std::cout 5 | #include // std::tuple 6 | #include // std::is_aggregate 7 | #include // std::index_sequence 8 | 9 | #include"constructable_with.hpp" 10 | 11 | namespace mlib { 12 | namespace meta { 13 | template 14 | constexpr auto refl_get(T t) { 15 | if constexpr (number_of_aggregate_members == 1) { 16 | auto [a] = t; 17 | return a; 18 | } 19 | else if constexpr (number_of_aggregate_members == 2) { 20 | auto [a, b] = t; 21 | return std::tuple{a, b}; 22 | } 23 | else if constexpr (number_of_aggregate_members == 3) { 24 | auto [a, b, c] = t; 25 | return std::tuple{a, b, c}; 26 | } 27 | else if constexpr (number_of_aggregate_members == 4) { 28 | auto [a, b, c, d] = t; 29 | return std::tuple{ 30 | a, b, c, d}; 31 | } 32 | else if constexpr (number_of_aggregate_members == 5) { 33 | auto [a, b, c, d, e] = t; 34 | return std::tuple{a, b, c, d, e}; 36 | } 37 | else if constexpr (number_of_aggregate_members == 6) { 38 | auto [a, b, c, d, e, f] = t; 39 | return std::tuple{a, b, c, d, e, f}; 41 | } 42 | else if constexpr (number_of_aggregate_members == 7) { 43 | auto [a, b, c, d, e, f, g] = t; 44 | return std::tuple{a, b, c, d, 46 | e, f, g}; 47 | } 48 | else if constexpr (number_of_aggregate_members == 8) { 49 | auto [a, b, c, d, e, f, g, h] = t; 50 | return std::tuple{ 52 | a, b, c, d, e, f, g, h}; 53 | } 54 | else if constexpr (number_of_aggregate_members == 9) { 55 | auto [a, b, c, d, e, f, g, h, i] = t; 56 | return std::tuple{a, b, c, d, e, f, g, h, i}; 59 | } 60 | else if constexpr (number_of_aggregate_members == 10) { 61 | auto [a, b, c, d, e, f, g, h, i, j] = t; 62 | return std::tuple{a, b, c, d, e, 65 | f, g, h, i, j}; 66 | } 67 | else if constexpr (number_of_aggregate_members == 11) 68 | { 69 | auto [a, b, c, d, e, f, g, h, i, j, k] = t; 70 | return std::tuple{a, b, c, d, e, f, g, h, i, j, k}; 72 | } 73 | } 74 | template 75 | constexpr auto get(auto& obj) 76 | { 77 | return std::get(refl_get(obj)); 78 | } 79 | 80 | } // namespace meta 81 | } // namespace mlib -------------------------------------------------------------------------------- /repeatedFunctionArguement.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::index_sequence 4 | #include // std::function 5 | 6 | namespace mlib 7 | { 8 | template 9 | struct always_type_t 10 | { 11 | using type = Type; 12 | }; 13 | 14 | template 15 | using always_type = always_type_t::type; 16 | 17 | template 18 | struct repeatedFunctionArgument 19 | { 20 | constexpr repeatedFunctionArgument(auto function) { m_function = std::function{ function }; } 21 | template constexpr returnType callFunction(Ts... args) const noexcept { return m_function(args...); } 22 | 23 | using type = decltype( 24 | [](std::index_sequence) 25 | { 26 | return std::function... args)>{}; 27 | }(std::make_index_sequence{}) 28 | ); 29 | 30 | type m_function; 31 | }; 32 | } // namespace mlib -------------------------------------------------------------------------------- /select.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::index_sequence 4 | #include // std::size_t 5 | #include // std::remove_cv_reference 6 | 7 | namespace mlib 8 | { 9 | template 10 | auto select(std::tuple tuple, std::index_sequence) 11 | { 12 | using result = std::tuple>...>; 13 | return result{ std::get(tuple)... }; 14 | } 15 | } // namespace mlib -------------------------------------------------------------------------------- /string_manip.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include"get_nth_element.hpp" 9 | 10 | namespace mlib 11 | { 12 | template 13 | struct fixed_string_ { 14 | constexpr fixed_string_(const char(&arr)[i + 1]) { 15 | std::copy_n(arr, i + 1, data); 16 | } 17 | 18 | constexpr fixed_string_(char(&arr)[i + 1]) 19 | { 20 | std::copy_n(arr, i + 1, data); 21 | } 22 | 23 | constexpr auto size() const { return i; } 24 | 25 | auto operator<=>(const fixed_string_&) const = default; 26 | 27 | template 28 | constexpr auto nth_element() const { return data[J]; } 29 | 30 | auto data_() 31 | { 32 | return data; 33 | } 34 | 35 | char data[i + 1] = {}; 36 | }; 37 | 38 | template 39 | fixed_string_(const char(&arr)[i])->fixed_string_; 40 | 41 | template 42 | fixed_string_(char(&arr)[i])->fixed_string_; 43 | 44 | template 45 | struct manipulate_str { 46 | constexpr auto operator()() 47 | { 48 | } 49 | 50 | template 51 | constexpr auto on_all() 52 | { 53 | return[](std::index_sequence) 54 | { 55 | char arr[] = { T(s.template nth_element())... }; 56 | return fixed_string_{ arr }; 57 | }(std::make_index_sequence{}); 58 | } 59 | 60 | template 61 | constexpr auto get_until() 62 | { 63 | return[](std::index_sequence) { 64 | char arr[I + 1] = { s.template nth_element()... }; 65 | arr[I] = 0; 66 | return fixed_string_{ arr }; 67 | }(std::make_index_sequence{}); 68 | } 69 | 70 | template 71 | constexpr auto select() 72 | { 73 | char arr[sizeof...(indexes)] = {s.template nth_element()...}; 74 | return fixed_string_{arr}; 75 | } 76 | }; 77 | } // namespace mlib -------------------------------------------------------------------------------- /string_parse.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "fixed_string.hpp" 12 | #include "constant_parameter.hpp" 13 | 14 | namespace mlib 15 | { 16 | template 17 | constexpr auto find_index_arr() { 18 | if constexpr (str.template nth_element() == c) { 19 | return index; 20 | } 21 | else { 22 | if constexpr (str.size() <= index) 23 | { 24 | return -1; 25 | } 26 | else 27 | { 28 | return find_index_arr(); 29 | } 30 | } 31 | } 32 | 33 | template 34 | constexpr auto find_index_arr() { 35 | if constexpr (str.template nth_element<0>() == c) { 36 | return 0; 37 | } 38 | else { 39 | return find_index_arr(); 40 | } 41 | } 42 | 43 | template 44 | constexpr auto function_return() 45 | { 46 | return c; 47 | } 48 | 49 | template 50 | struct if_is 51 | { 52 | constexpr if_is() {} 53 | 54 | constexpr auto operator()() const noexcept 55 | { 56 | if constexpr (condition) 57 | { 58 | return value; 59 | } 60 | else 61 | { 62 | return return_; 63 | } 64 | } 65 | }; 66 | 67 | template 68 | constexpr auto array_to_fixed_string(std::array arr) 69 | { 70 | return fixed_string{arr.data()}; 71 | } 72 | 73 | 74 | template 75 | constexpr auto when_less_than_idx() 76 | { 77 | if constexpr (index < until) 78 | { 79 | return blur; 80 | } 81 | else 82 | { 83 | return other; 84 | } 85 | } 86 | 87 | template 88 | constexpr auto when_greater_than_idx() 89 | { 90 | if constexpr (index > until) 91 | { 92 | return blur; 93 | } 94 | else 95 | { 96 | return other; 97 | } 98 | } 99 | 100 | template 101 | struct container; 102 | 103 | template 104 | struct string_parse { 105 | template 106 | constexpr auto substr() const noexcept { 107 | return[&](std::index_sequence) { 108 | constexpr char result[] = { (str.template nth_element())..., '\0' }; 109 | return fixed_string{ result }; 110 | }(std::make_index_sequence{}); 111 | } 112 | 113 | template 114 | constexpr auto take() const noexcept 115 | { 116 | constexpr char result[] = { (str.template nth_element())..., '\0' }; 117 | return fixed_string{ result }; 118 | } 119 | 120 | template 121 | constexpr auto consume_until() const noexcept { 122 | constexpr int index = find_index_arr(); 123 | return substr<0, index>(); 124 | } 125 | 126 | template 127 | constexpr auto find() const noexcept 128 | { 129 | return find_index_arr(); 130 | } 131 | 132 | // make this private 133 | template 134 | constexpr auto function_return_index() const noexcept 135 | { 136 | return Char; 137 | } 138 | 139 | template 140 | constexpr auto if_string_has_change() const noexcept 141 | { 142 | return change_with(); 143 | } 144 | 145 | template 146 | constexpr auto if_string_has() const noexcept 147 | { 148 | if constexpr (occurences<[](auto x){x == c;}>() > 0) 149 | { 150 | return change_with(); 151 | } 152 | } 153 | 154 | template 155 | constexpr auto blur_with() const noexcept 156 | { 157 | return[&](std::index_sequence) 158 | { 159 | const char result[] = { (function_return_index())..., '\0' }; 160 | return fixed_string{ result }; 161 | }(std::make_index_sequence{}); 162 | } 163 | 164 | template 165 | constexpr auto change_with() const noexcept 166 | { 167 | return[&](std::index_sequence) 168 | { 169 | constexpr char result[] = { (lambda(str.template nth_element()))..., '\0' }; 170 | return fixed_string{ result }; 171 | }(std::make_index_sequence{}); 172 | } 173 | 174 | template 175 | constexpr auto occurences() const noexcept 176 | { 177 | return[&](std::index_sequence) 178 | { 179 | return (lambda.template operator() < str.template nth_element() > () + ...); 180 | }(std::make_index_sequence{}); 181 | } 182 | 183 | template 184 | constexpr auto blur_until() const noexcept 185 | { 186 | return [&] (std::index_sequence) 187 | { 188 | const char result[] = { (when_less_than_idx())..., '\0' }; 189 | return fixed_string{ result }; 190 | }(std::make_index_sequence{}); 191 | } 192 | 193 | template 194 | constexpr auto blur_from() const noexcept 195 | { 196 | return[&] (std::index_sequence) 197 | { 198 | const char result[] = { (when_greater_than_idx())..., '\0' }; 199 | return fixed_string{ result }; 200 | }(std::make_index_sequence{}); 201 | } 202 | 203 | template 204 | constexpr auto remove_if() const noexcept 205 | { 206 | return[&](std::index_sequence) 207 | { 208 | constexpr char array[] = { if_is()), str.template nth_element(), ' '>{}()..., '\0' }; 209 | return fixed_string{ array }; 210 | }(std::make_index_sequence{}); 211 | } 212 | 213 | template 214 | constexpr auto remove_if_not() const noexcept 215 | { 216 | return[&](std::index_sequence) 217 | { 218 | constexpr char array[] = { if_is()), str.template nth_element(), ' '>{}()..., '\0' }; 219 | return fixed_string{ array }; 220 | }(std::make_index_sequence{}); 221 | } 222 | 223 | template 224 | constexpr auto replace_with_if() const noexcept 225 | { 226 | return[&](std::index_sequence) 227 | { 228 | constexpr char array[] = { if_is()), str.template nth_element(), c>{}()..., '\0' }; 229 | return fixed_string{ array }; 230 | }(std::make_index_sequence{}); 231 | } 232 | 233 | template 234 | constexpr auto character_occurences() const noexcept 235 | { 236 | return[&](std::index_sequence) 237 | { 238 | return ((str.data[indexes] == character) + ...); 239 | }(std::make_index_sequence{}); 240 | } 241 | 242 | template 243 | constexpr auto character_occurences_from() const noexcept 244 | { 245 | return[&](std::index_sequence) 246 | { 247 | return ((str.data[indexes + from] == character) + ...); 248 | }(std::make_index_sequence{}); 249 | } 250 | 251 | template 252 | constexpr auto character_occurences_until() const noexcept 253 | { 254 | return[&](std::index_sequence) 255 | { 256 | return ((if_is<(indexes < until), (str.data[indexes] == character), 0>{}()) + ...); 257 | }(std::make_index_sequence{}); 258 | } 259 | 260 | template 261 | constexpr auto from_up_to() const noexcept 262 | { 263 | constexpr auto index = find(); 264 | return substr(); 265 | } 266 | 267 | template 268 | constexpr auto from_to_end() const noexcept 269 | { 270 | constexpr auto index = find(); 271 | return substr(); 272 | } 273 | 274 | template 275 | constexpr auto alternate_blur() const noexcept 276 | { 277 | return[&](std::index_sequence) 278 | { 279 | constexpr char arr[] = {(if_is<((indexes % 2) == 0), first, second>{}())..., '\n'}; 280 | return fixed_string{arr}; 281 | }(std::make_index_sequence{}); 282 | } 283 | 284 | constexpr auto non_recuring() const noexcept 285 | { 286 | return[&](std::index_sequence) 287 | { 288 | return ((character_occurences()>() < 2) + ...); 289 | }(std::make_index_sequence{}); 290 | } 291 | 292 | constexpr auto to_container() 293 | { 294 | return [] (std::index_sequence) 295 | { 296 | return container<(str.template nth_element())...>{}; 297 | }(std::make_index_sequence{}); 298 | } 299 | 300 | template 301 | constexpr auto all_characters_are() const noexcept 302 | { 303 | return[&](std::index_sequence) 304 | { 305 | return ((c == str.template nth_element()) && ...); 306 | }(std::make_index_sequence{}); 307 | } 308 | 309 | template 310 | constexpr auto all_characters_are_not() const noexcept 311 | { 312 | return[&](std::index_sequence) 313 | { 314 | return ((c != str.template nth_element()) && ...); 315 | }(std::make_index_sequence{}); 316 | } 317 | 318 | constexpr auto data() const noexcept { return str.data; } 319 | constexpr auto string_view() const noexcept { return std::string_view{ str.data }; } 320 | constexpr auto string() const noexcept { return str; } 321 | constexpr auto number_of_characters() const noexcept { return str.size(); } 322 | constexpr auto size() const noexcept { return str.size(); } 323 | constexpr auto stdstring() const noexcept { return std::string{ str.data }; } 324 | }; 325 | } // namespace mlib 326 | // https://godbolt.org/z/Ghee85T1n -------------------------------------------------------------------------------- /sub_pack.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include"get_nth_element.hpp" 4 | #include"pack.hpp" 5 | 6 | namespace mlib 7 | { 8 | template 9 | constexpr auto sub_pack() /*-> mlib::pack*/ 10 | { 11 | return[&](std::index_sequence) 12 | { 13 | return mlib::pack(packs...)...>{}; 14 | }(std::make_index_sequence{}); 15 | } 16 | } // namespace mlib -------------------------------------------------------------------------------- /sub_tuple.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | #include // std::cout 5 | #include // std::tuple 6 | #include // std::index_sequence 7 | 8 | namespace mlib 9 | { 10 | template 11 | constexpr auto sub_tuple(std::tuple t) { 12 | static_assert(T < sizeof...(Ts), "Invalid Index range"); 13 | return[&](std::index_sequence) { 14 | return std::tuple{ std::get(t)... }; 15 | }(std::make_index_sequence{}); 16 | } 17 | } // namespace mlib -------------------------------------------------------------------------------- /swap.hpp: -------------------------------------------------------------------------------- 1 | #include // std::size_t 2 | #include // std::index_sequence 3 | #include // std::tuple 4 | 5 | #include"get_nth_element.hpp" 6 | 7 | namespace mlib 8 | { 9 | // a class to hold compile time data 10 | template 11 | struct container 12 | { 13 | template 14 | constexpr auto get() 15 | { 16 | return mlib::get_nth_element(Ts...); 17 | } 18 | }; 19 | 20 | // this is a compile time parameter that is easy to pass into things 21 | template 22 | struct c_p__ 23 | { 24 | }; 25 | 26 | // when condition(value) returns TrueValue, else returns FalseValue 27 | template 28 | struct when_ 29 | { 30 | constexpr auto operator()() 31 | { 32 | if constexpr (condition(value)) 33 | { 34 | return TrueValue; 35 | } 36 | if constexpr (other_condition(value)) 37 | { 38 | return SecondTrueValue; 39 | } 40 | else 41 | { 42 | return FalseValue(c_p__{}); 43 | } 44 | } 45 | }; 46 | 47 | // this function swaps Index and Other index then returns the tuple. 48 | template 49 | constexpr auto swap() // -> container 50 | { 51 | return[&](std::index_sequence) 52 | { 53 | return container < (when_ < indexes, [=](auto x) {return x == Index; }, [=](auto x) {return x == OtherIndex; }, mlib::get_nth_element(Ts...), mlib::get_nth_element(Ts...), [=](c_p__) { return mlib::get_nth_element(Ts...); } > {}())... > {}; 54 | }(std::make_index_sequence{}); 55 | } 56 | } // namespace mlib -------------------------------------------------------------------------------- /times.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace mlib 7 | { 8 | 9 | template 10 | struct times 11 | { 12 | constexpr auto operator()(auto callable) 13 | { 14 | [&] (std::index_sequence) 15 | { 16 | (callable, void(indexes), ...); 17 | }(std::make_index_sequence{}); 18 | } 19 | }; 20 | } // namespace mlib -------------------------------------------------------------------------------- /trait.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // only works on Clang and MSVC, not GCC 3 | 4 | namespace mlib 5 | { 6 | template 7 | struct trait_module 8 | { 9 | constexpr auto operator()(auto X) const 10 | { 11 | // we call our NTTP 12 | T(X); 13 | return true; 14 | } 15 | }; 16 | 17 | 18 | template 19 | requires((trait_modules(T), ...)) 20 | struct trait 21 | { 22 | constexpr trait() {} 23 | 24 | static constexpr auto value = true; 25 | }; 26 | } // namespace mlib -------------------------------------------------------------------------------- /transform.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | #include // std::get 5 | 6 | #include"index_sequence.hpp" // mlib::index_sequence 7 | 8 | namespace mlib 9 | { 10 | template 11 | constexpr auto transform_helper(std::tuple& t, F& f, std::index_sequence i_s) 12 | { 13 | return std::make_tuple(f(std::get(t))...); 14 | } 15 | 16 | template 17 | constexpr auto transform(std::tuple& tup, F&& f) 18 | { 19 | return transform_helper(tup, f, std::make_index_sequence{}); 20 | } 21 | } // namespace mlib -------------------------------------------------------------------------------- /tuple.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::forward 4 | 5 | #include"get.hpp" // mlib::get 6 | 7 | namespace mlib 8 | { 9 | template 10 | struct index {}; 11 | 12 | template 13 | struct tuple 14 | { 15 | T first{}; 16 | tuple second{}; 17 | 18 | auto operator==(tuple& t) 19 | { 20 | return first == t.first && second == t.second; 21 | } 22 | 23 | auto operator==(auto& t) 24 | { 25 | return false; 26 | } 27 | 28 | template 29 | decltype(auto) operator[](index) 30 | { 31 | return mlib::get(*this); 32 | } 33 | }; 34 | 35 | template 36 | struct tuple 37 | { 38 | T first{}; 39 | }; 40 | 41 | template 42 | constexpr auto make_tuple(Types... types) 43 | { 44 | return tuple{std::forward(types...)}; 45 | } 46 | 47 | template 48 | constexpr auto make_tuple(Type type) 49 | { 50 | return tuple{std::forward(type)}; 51 | } 52 | 53 | template< int I, typename T > 54 | struct tuple_element; 55 | 56 | template< int I, typename Head, typename... Tail > 57 | struct tuple_element> : tuple_element> 58 | { 59 | }; 60 | 61 | template< typename Head, typename... Tail > 62 | struct tuple_element<0, tuple> 63 | { 64 | using type = Head; 65 | }; 66 | 67 | template 68 | using tuple_element_t = tuple_element::type; 69 | } // namespace mlib -------------------------------------------------------------------------------- /tuple_equal.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::tuple 4 | 5 | namespace mlib 6 | { 7 | constexpr auto equal(auto a, auto b) 8 | { 9 | return a == b; 10 | } 11 | 12 | template 13 | constexpr auto tuple_equal(As tup_a, Bs tup_b) 14 | { 15 | constexpr auto pack_size_a = [](std::tuple) { return sizeof...(Ts); }(tup_a); 16 | constexpr auto pack_size_b = [](std::tuple) { return sizeof...(Ts); }(tup_b); 17 | if constexpr (pack_size_a != pack_size_b) { 18 | return false; 19 | } 20 | else { 21 | return[&](std::index_sequence) 22 | { 23 | return ((equal(std::get(tup_a), std::get(tup_b))) && ...); 24 | }(std::make_index_sequence{}); 25 | } 26 | } 27 | } // namespace mlib -------------------------------------------------------------------------------- /tuple_find.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include"pack_find.hpp" 8 | #include"get_nth_element.hpp" 9 | 10 | namespace mlib 11 | { 12 | template 13 | constexpr auto tuple_find(std::tuple t) 14 | { 15 | constexpr auto x = [&](std::index_sequence) 16 | { 17 | return (mlib::value_is_value(t), value_to_find>{}() + ...); 18 | }(std::make_index_sequence{}); 19 | if constexpr (x == 0 && std::get<0>(t) != value_to_find) 20 | { 21 | return -1; 22 | } 23 | 24 | return x; 25 | } 26 | } // namespace mlib -------------------------------------------------------------------------------- /tuple_inheritance.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::size_t 4 | 5 | namespace mlib 6 | { 7 | template struct tuple_leaf { 8 | T value; 9 | }; 10 | 11 | template struct tuple_impl; 12 | 13 | template struct tuple_impl {}; 14 | 15 | template 16 | struct tuple_impl : public tuple_leaf, 17 | public tuple_leaf {}; 18 | 19 | template 20 | Head get(tuple_impl& x) 21 | { 22 | x.tuple_leaf::value; 23 | } 24 | 25 | template 26 | using Tuple = tuple_impl<0, Types...>; 27 | } // namespace mlib -------------------------------------------------------------------------------- /tuple_reverse.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // std::integer_sequence 4 | #include // std::size_t 5 | #include // std::tuple 6 | #include // std::declval 7 | 8 | namespace mlib 9 | { 10 | template 11 | struct tuple_reverse; 12 | 13 | template<> 14 | struct tuple_reverse> 15 | { 16 | using type = std::tuple<>; 17 | }; 18 | 19 | template 20 | struct tuple_reverse < std::tuple > 21 | { 22 | using head = tuple; 23 | using tail = tuple_reverse>::type; 24 | 25 | using type = decltype(std::tuple_cat(std::declval(), std::declval())); 26 | }; 27 | 28 | template 29 | constexpr auto reverse_tuple_value_impl(T&& t, std::index_sequence) 30 | { 31 | return std::make_tuple(std::get(std::forward(t))...); 32 | } 33 | 34 | template 35 | constexpr auto reverse_tuple_value(T&& t) 36 | { 37 | return reverse_impl(std::forward(t), 38 | std::make_index_sequence::value>()); 39 | } 40 | } -------------------------------------------------------------------------------- /type.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace mlib 4 | { 5 | template 6 | struct type_t 7 | { 8 | constexpr auto operator==(type_t) const 9 | { 10 | return true; 11 | } 12 | constexpr auto operator==(auto) const 13 | { 14 | return false; 15 | } 16 | 17 | constexpr type_t() {} 18 | }; 19 | 20 | template 21 | static constexpr auto type = type_t{}; 22 | } // namespace mlib -------------------------------------------------------------------------------- /value_range.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include"get_nth_element.hpp" 9 | 10 | namespace mlib { 11 | template struct value_range { 12 | constexpr auto operator|(auto lambda) const { return for_each(); } 13 | 14 | template constexpr auto for_each() const { 15 | return value_range{}; 16 | } 17 | 18 | template constexpr auto at() const { return mlib::get_nth_element(Ts...); } 19 | 20 | constexpr auto tuple() const { return std::make_tuple(Ts...); } 21 | }; 22 | } // namespace mlib -------------------------------------------------------------------------------- /value_sequence.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include"get_nth_element.hpp" 4 | 5 | namespace mlib 6 | { 7 | template 8 | struct value_sequence 9 | { 10 | template 11 | constexpr auto at() 12 | { 13 | return mlib::get_nth_element(Ts...); 14 | } 15 | }; 16 | } // namespace mlib --------------------------------------------------------------------------------