├── .github └── workflows │ ├── build_and_test.yml │ └── emscripten.yml ├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── bench ├── bm_pmt_dict_pack_unpack.cpp ├── bm_pmt_dict_ref.cpp ├── bm_pmt_serialize_uvec.cpp └── meson.build ├── cmake └── Modules │ ├── pmtf.cmake.in │ └── pmtfConfig.cmake.in ├── emscripten-build.ini ├── include └── pmtv │ ├── base64 │ ├── README.md │ └── base64.h │ ├── format.hpp │ ├── meson.build │ ├── pmt.hpp │ ├── rva_variant.hpp │ ├── serialiser.hpp │ ├── type_helpers.hpp │ └── version.hpp ├── meson.build ├── meson_options.txt ├── python └── pmtv │ ├── __init__.py │ ├── bindings │ ├── meson.build │ ├── pmt_python.cc │ └── pmtv_python.cc │ └── meson.build ├── subprojects ├── .gitignore ├── cli11.wrap ├── fmt.wrap └── refl-cpp.wrap └── test ├── meson.build ├── qa_map.cpp ├── qa_pybind.py ├── qa_reflection.cpp ├── qa_scalar.cpp ├── qa_string.cpp ├── qa_uniform_vector.cpp ├── qa_vector_of_pmts.cpp └── test_pybind.py /.github/workflows/build_and_test.yml: -------------------------------------------------------------------------------- 1 | name: build and run tests 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | branches: 8 | - main 9 | jobs: 10 | linux-docker: 11 | # All of these shall depend on the formatting check (needs: check-formatting) 12 | runs-on: ubuntu-24.04 13 | # The GH default is 360 minutes (it's also the max as of Feb-2021). However, 14 | # we should fail sooner. The only reason to exceed this time is if a test 15 | # hangs. 16 | timeout-minutes: 120 17 | strategy: 18 | # Enabling fail-fast would kill all Dockers if one of them fails. We want 19 | # maximum output. 20 | fail-fast: false 21 | matrix: 22 | # For every distro we want to test here, add one key 'distro' with a 23 | # descriptive name, and one key 'containerid' with the name of the 24 | # container (i.e., what you want to docker-pull) 25 | distro: 26 | - name: 'Ubuntu 24.04' 27 | containerid: 'ghcr.io/gnuradio/gnuradio-docker:ubuntu-24.04' 28 | cxxflags: -Werror 29 | - name: 'Fedora 40' 30 | containerid: 'ghcr.io/gnuradio/gnuradio-docker:fedora-40' 31 | cxxflags: '' 32 | # - distro: 'CentOS 8.3' 33 | # containerid: 'gnuradio/ci:centos-8.3-3.9' 34 | # cxxflags: -Werror 35 | # - distro: 'Debian 10' 36 | # containerid: 'gnuradio/ci-debian-10-3.9:1.0' 37 | # cxxflags: -Werror 38 | compiler: 39 | - name: "gcc" 40 | command: "g++" 41 | - name: "clang" 42 | command: "clang++" 43 | name: ${{ matrix.distro.name }} - ${{ matrix.compiler.name }} 44 | container: 45 | image: ${{ matrix.distro.containerid }} 46 | volumes: 47 | - build_data:/build 48 | options: --cpus 2 49 | steps: 50 | - uses: actions/checkout@v3 51 | name: Checkout Project 52 | - name: Meson Setup 53 | env: 54 | CXX: ${{ matrix.compiler.command }} 55 | working-directory: ${{ github.workspace }} 56 | run: '$CXX --version && meson setup build --buildtype=debugoptimized -Denable_testing=true' 57 | - name: Make 58 | working-directory: ${{ github.workspace }}/build 59 | run: 'ninja' 60 | - name: Make Test 61 | working-directory: ${{ github.workspace }}/build 62 | run: 'ninja test' 63 | - uses: actions/upload-artifact@v3 64 | if: failure() 65 | with: 66 | name: Linux_Meson_Testlog 67 | path: build/meson-logs/testlog.txt 68 | -------------------------------------------------------------------------------- /.github/workflows/emscripten.yml: -------------------------------------------------------------------------------- 1 | name: build and run tests 2 | on: 3 | push: 4 | branches: 5 | - main 6 | pull_request: 7 | branches: 8 | - main 9 | jobs: 10 | linux-docker: 11 | # All of these shall depend on the formatting check (needs: check-formatting) 12 | runs-on: ubuntu-24.04 13 | # The GH default is 360 minutes (it's also the max as of Feb-2021). However, 14 | # we should fail sooner. The only reason to exceed this time is if a test 15 | # hangs. 16 | timeout-minutes: 120 17 | strategy: 18 | # Enabling fail-fast would kill all Dockers if one of them fails. We want 19 | # maximum output. 20 | fail-fast: false 21 | matrix: 22 | # For every distro we want to test here, add one key 'distro' with a 23 | # descriptive name, and one key 'containerid' with the name of the 24 | # container (i.e., what you want to docker-pull) 25 | distro: 26 | - name: 'Ubuntu 24.04' 27 | containerid: 'ghcr.io/gnuradio/gnuradio-docker:ubuntu-24.04' 28 | cxxflags: -Werror 29 | compiler: 30 | - name: "emscripten" 31 | command: "emcc" 32 | name: ${{ matrix.distro.name }} - ${{ matrix.compiler.name }} 33 | container: 34 | image: ${{ matrix.distro.containerid }} 35 | volumes: 36 | - build_data:/build 37 | options: --cpus 2 38 | steps: 39 | - uses: actions/checkout@v3 40 | name: Checkout Project 41 | - name: Install emscripten 42 | run: | 43 | DEBIAN_FRONTEND=noninteractive apt-get install -qy bzip2 clang 44 | cd 45 | git clone https://github.com/emscripten-core/emsdk.git 46 | cd emsdk 47 | # Download and install the latest SDK tools. 48 | ./emsdk install latest 49 | # Make the "latest" SDK "active" for the current user. (writes .emscripten file) 50 | ./emsdk activate latest 51 | 52 | # - name: Install emscripten 53 | # run: | 54 | # pwd 55 | # echo 56 | # ls ${{ github.workspace }} 57 | # echo 58 | # ls . 59 | # echo 60 | # ls emsdk 61 | - name: Configure Meson 62 | shell: bash 63 | working-directory: ${{ github.workspace }} 64 | run: | 65 | source ~/emsdk/emsdk_env.sh 66 | tee emscripten-toolchain.ini </dev/null 67 | [constants] 68 | toolchain = '$HOME/emsdk/${{ env.EM_CACHE_FOLDER }}/upstream/emscripten/' 69 | EOF 70 | meson setup build --cross-file emscripten-toolchain.ini --cross-file emscripten-build.ini -Denable_python=false -Denable_testing=false 71 | - name: Make 72 | working-directory: ${{ github.workspace }}/build 73 | run: 'ninja' 74 | - name: Run Test File 75 | shell: bash 76 | working-directory: ${{ github.workspace }}/build 77 | run: | 78 | source ~/emsdk/emsdk_env.sh 79 | ${EMSDK_NODE} bench/bm_pmt_dict_ref.js 80 | - uses: actions/upload-artifact@v3 81 | if: failure() 82 | with: 83 | name: Linux_Meson_Testlog 84 | path: build/meson-logs/testlog.txt 85 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # Project Workspaces 35 | **/.vscode/ 36 | 37 | # Byte-compiled / optimized / DLL files 38 | __pycache__/ 39 | *.py[cod] 40 | *$py.class 41 | 42 | # Vi temporary files 43 | *.swp 44 | *.swo 45 | 46 | build/ 47 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gnuradio/pmt/e1a46cb61decb044f6ab0a58a77211beb3630340/.gitmodules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PMT Library 2 | 3 | https://wiki.gnuradio.org/index.php/Polymorphic_Types_(PMTs) 4 | 5 | PMT Objects represent a serializable container of data that can be passed across various interfaced. In GNU Radio, these are used to carry data on stream tags or through message passing, and could be used to pass data to external consumers of GNU Radio. This PMT library restructures the API beyond what has previously been included in the GNU Radio codebase to more closely align with the functionality and usability in the C++ Standard Template Library, and uses std::variant for the underlying data storage and serialization 6 | 7 | ## Dependencies 8 | 9 | - meson 10 | - ninja 11 | - C++20 12 | 13 | ## Installation 14 | 15 | PMTlib uses meson and ninja to manage the build process, which can be installed via pip and your package manager 16 | 17 | ```bash 18 | pip install meson 19 | cd pmt 20 | meson setup build --buildtype=debugoptimized --prefix=[PREFIX] --libdir=lib 21 | cd build 22 | ninja 23 | ninja install 24 | ``` 25 | 26 | 27 | ## Build with emscripten 28 | The path to `emsdk` must be provided. It can be done in 2 different ways. 29 | 30 | ### Option 1 31 | Create file `emscripten-toolchain.ini` in the project root directory with the following content: 32 | ```text 33 | [constants] 34 | toolchain = '/path/to/emsdk/upstream/emscripten/' 35 | ``` 36 | 37 | And then execute the following command: 38 | ```bash 39 | meson setup build_wasm --cross-file emscripten-toolchain.ini --cross-file emscripten-build.ini -Denable_python=false -Denable_testing=true 40 | ``` 41 | 42 | ### Option 2 43 | Set `toolchain` constant directly in `emscripten-build.ini` file in `[constants]` section: 44 | ```text 45 | [constants] 46 | ... 47 | toolchain = '/path/to/emsdk/upstream/emscripten/' 48 | ``` 49 | And then execute the following command: 50 | ```bash 51 | meson setup build_wasm --cross-file emscripten-build.ini -Denable_python=false -Denable_testing=true 52 | ``` 53 | 54 | 55 | The next steps are: 56 | ```bash 57 | cd build_wasm 58 | ninja 59 | ninja test 60 | ``` 61 | 62 | ### Potential problems 63 | If error `Dependency gtest found: NO` occurred, do the following commands: 64 | ```bash 65 | cd [pmt_project_root_dir] 66 | meson wrap install gtest 67 | ``` 68 | 69 | It creates `subprojects/gtest.wrap` wrap file for `gtest`. 70 | -------------------------------------------------------------------------------- /bench/bm_pmt_dict_pack_unpack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #if defined(__clang__) || defined(__GNUC__) 6 | #pragma GCC diagnostic push // ignore warning of external libraries that from this lib-context we do not have any control over 7 | #pragma GCC diagnostic ignored "-Wall" 8 | #pragma GCC diagnostic ignored "-Wold-style-cast" 9 | #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion" 10 | #ifndef __clang__ // only for GCC, not Clang 11 | #pragma GCC diagnostic ignored "-Wuseless-cast" 12 | #endif 13 | #endif 14 | 15 | #include "CLI/App.hpp" 16 | #include "CLI/Config.hpp" 17 | #include "CLI/Formatter.hpp" 18 | 19 | #if defined(__clang__) || defined(__GNUC__) 20 | #pragma GCC diagnostic pop 21 | #endif 22 | 23 | #include 24 | #include 25 | 26 | using namespace pmtv; 27 | 28 | bool run_test(const int32_t times, int32_t nitems) 29 | { 30 | 31 | bool valid = true; 32 | for (int32_t i = 0; i < times; i++) { 33 | // Create the dictionary 34 | pmtv::map_t starting_map; 35 | 36 | #if 1 37 | for (int32_t k = 0; k < nitems; k++) { 38 | auto key = fmt::format("key{}", k); 39 | auto value = pmt(k); 40 | 41 | starting_map[key] = value; 42 | } 43 | auto d_in = pmt(starting_map); 44 | #else 45 | auto d_in = map::make(starting_map); 46 | for (int32_t k = 0; k < nitems; k++) { 47 | auto key = std::string("key" + std::to_string(k)); 48 | auto value = scalar::make(k); 49 | 50 | d_in->set(key, value); 51 | } 52 | #endif 53 | 54 | #if 0 55 | auto d_out = d_in->value(); 56 | 57 | for (int32_t k = 0; k < nitems; k++) { 58 | auto key = std::string("key" + std::to_string(k)); 59 | auto value = scalar::make(k); 60 | 61 | if (std::static_pointer_cast>(d_out[key])->value() != k) { 62 | valid = false; 63 | } 64 | } 65 | #endif 66 | } 67 | return valid; 68 | } 69 | 70 | int main(int argc, char* argv[]) 71 | { 72 | int32_t samples = 10000; 73 | int32_t items = 100; 74 | 75 | 76 | CLI::App app{ "Benchmarking Script for Dictionary Packing and Unpacking" }; 77 | 78 | // app.add_option("-h,--help", "display help"); 79 | app.add_option("--samples", samples, "Number of times to perform lookup"); 80 | app.add_option("--items", items, "Number of items in dict"); 81 | 82 | CLI11_PARSE(app, argc, argv); 83 | 84 | { 85 | 86 | auto t1 = std::chrono::steady_clock::now(); 87 | 88 | auto valid = run_test(samples, items); 89 | 90 | auto t2 = std::chrono::steady_clock::now(); 91 | auto time = static_cast(1e-9) * static_cast(std::chrono::duration_cast(t2 - t1).count()); 92 | 93 | std::cout << "[PROFILE_TIME]" << time << "[PROFILE_TIME]" << std::endl; 94 | std::cout << "[PROFILE_VALID]" << valid << "[PROFILE_VALID]" << std::endl; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /bench/bm_pmt_dict_ref.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #if defined(__clang__) || defined(__GNUC__) 6 | #pragma GCC diagnostic push // ignore warning of external libraries that from this lib-context we do not have any control over 7 | #pragma GCC diagnostic ignored "-Wall" 8 | #pragma GCC diagnostic ignored "-Wold-style-cast" 9 | #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion" 10 | #ifndef __clang__ // only for GCC, not Clang 11 | #pragma GCC diagnostic ignored "-Wuseless-cast" 12 | #endif 13 | #endif 14 | 15 | #include "CLI/App.hpp" 16 | #include "CLI/Config.hpp" 17 | #include "CLI/Formatter.hpp" 18 | 19 | #if defined(__clang__) || defined(__GNUC__) 20 | #pragma GCC diagnostic pop 21 | #endif 22 | 23 | #include 24 | #include 25 | 26 | using namespace pmtv; 27 | 28 | bool run_test(const int32_t times, pmt& d, int32_t index) 29 | { 30 | std::stringbuf sb; // fake channel 31 | 32 | auto key = fmt::format("key{}", index); 33 | 34 | auto themap = pmtv::get_map(d); 35 | 36 | bool valid = true; 37 | for (int32_t i = 0; i < times; i++) { 38 | auto ref = themap[key]; 39 | 40 | // if (ref == nullptr) 41 | // valid = false; 42 | auto s = pmtv::cast(ref); 43 | if (s != index) { 44 | valid = false; 45 | } 46 | } 47 | return valid; 48 | } 49 | 50 | int main(int argc, char* argv[]) 51 | { 52 | int32_t samples = 10000; 53 | int32_t items = 100; 54 | int32_t index = 0; 55 | 56 | 57 | CLI::App app{ "Benchmarking Script for Dictionary Packing and Unpacking" }; 58 | 59 | // app.add_option("-h,--help", "display help"); 60 | app.add_option("--samples", samples, "Number of times to perform lookup"); 61 | app.add_option("--items", items, "Number of items in dict"); 62 | app.add_option("--index", index, "Index for lookup"); 63 | 64 | CLI11_PARSE(app, argc, argv); 65 | 66 | { 67 | // Create the dictionary 68 | pmtv::map_t starting_map; 69 | for (int32_t k = 0; k < items; k++) { 70 | // auto key = std::string("key" + std::to_string(k)); 71 | // auto value = scalar(k); 72 | 73 | starting_map["key" + std::to_string(k)] = pmt(k); 74 | } 75 | 76 | auto d = pmt(starting_map); 77 | 78 | auto t1 = std::chrono::steady_clock::now(); 79 | 80 | auto valid = run_test(samples, d, index); 81 | 82 | auto t2 = std::chrono::steady_clock::now(); 83 | auto time = static_cast(1e-9) * static_cast(std::chrono::duration_cast(t2 - t1).count()); 84 | 85 | std::cout << "[PROFILE_TIME]" << time << "[PROFILE_TIME]" << std::endl; 86 | std::cout << "[PROFILE_VALID]" << valid << "[PROFILE_VALID]" << std::endl; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /bench/bm_pmt_serialize_uvec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #if defined(__clang__) || defined(__GNUC__) 5 | #pragma GCC diagnostic push // ignore warning of external libraries that from this lib-context we do not have any control over 6 | #pragma GCC diagnostic ignored "-Wall" 7 | #pragma GCC diagnostic ignored "-Wold-style-cast" 8 | #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion" 9 | #ifndef __clang__ // only for GCC, not Clang 10 | #pragma GCC diagnostic ignored "-Wuseless-cast" 11 | #endif 12 | #endif 13 | 14 | #include "CLI/App.hpp" 15 | #include "CLI/Config.hpp" 16 | #include "CLI/Formatter.hpp" 17 | 18 | #if defined(__clang__) || defined(__GNUC__) 19 | #pragma GCC diagnostic pop 20 | #endif 21 | 22 | #include 23 | #include 24 | 25 | using namespace pmtv; 26 | 27 | bool run_test(const int32_t times, const std::vector& data) 28 | { 29 | bool valid = true; 30 | 31 | std::stringbuf sb; // fake channel 32 | for (int i = 0; i < times; i++) { 33 | sb.str(""); // reset channel to empty 34 | // auto p1 = vector(data); 35 | pmt p1 = data; 36 | pmtv::serialize(sb, p1); 37 | auto p2 = pmtv::deserialize(sb); 38 | if (p1 != p2) 39 | valid = false; 40 | } 41 | 42 | return valid; 43 | } 44 | 45 | int main(int argc, char* argv[]) 46 | { 47 | int32_t samples = 1000000; 48 | std::size_t veclen = 1024; 49 | 50 | CLI::App app{ "Benchmarking Script for Uniform Vector Serialization" }; 51 | 52 | // app.add_option("-h,--help", "display help"); 53 | app.add_option("--samples", samples, "Number of Samples"); 54 | app.add_option("--veclen", veclen, "Vector Length"); 55 | 56 | CLI11_PARSE(app, argc, argv); 57 | 58 | { 59 | std::vector data(veclen); 60 | std::iota(data.begin(), data.end(), 0); 61 | 62 | auto t1 = std::chrono::steady_clock::now(); 63 | 64 | auto valid = run_test(samples, data); 65 | 66 | auto t2 = std::chrono::steady_clock::now(); 67 | auto time = static_cast(1e-9) * static_cast(std::chrono::duration_cast(t2 - t1).count()); 68 | 69 | std::cout << "[PROFILE_TIME]" << time << "[PROFILE_TIME]" << std::endl; 70 | std::cout << "[PROFILE_VALID]" << valid << "[PROFILE_VALID]" << std::endl; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /bench/meson.build: -------------------------------------------------------------------------------- 1 | incdir = include_directories('../include') 2 | 3 | srcs = ['bm_pmt_serialize_uvec', 4 | 'bm_pmt_dict_ref', 5 | 'bm_pmt_dict_pack_unpack'] 6 | 7 | deps = [pmt_dep, CLI11_dep, fmt_dep] 8 | 9 | if (CLI11_dep.found()) 10 | foreach s : srcs 11 | e = executable(s, 12 | s + '.cpp', 13 | include_directories : incdir, 14 | dependencies: deps, 15 | install : false) 16 | # test(s, e) 17 | endforeach 18 | endif 19 | -------------------------------------------------------------------------------- /cmake/Modules/pmtf.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKAGE_INIT@ -------------------------------------------------------------------------------- /cmake/Modules/pmtfConfig.cmake.in: -------------------------------------------------------------------------------- 1 | if(NOT PKG_CONFIG_FOUND) 2 | INCLUDE(FindPkgConfig) 3 | endif() 4 | PKG_CHECK_MODULES(PC_PMTF pmtf) 5 | 6 | FIND_PATH( 7 | PMTF_INCLUDE_DIRS 8 | NAMES gnuradio/block.hh 9 | HINTS $ENV{PMTF_DIR}/include 10 | ${PC_PMTF_INCLUDEDIR} 11 | PATHS ${CMAKE_INSTALL_PREFIX}/include 12 | /usr/local/include 13 | /usr/include 14 | ) 15 | 16 | FIND_LIBRARY( 17 | PMTF_LIBRARIES 18 | NAMES cusp 19 | HINTS $ENV{PMTF_DIR}/lib 20 | ${PC_PMTF_LIBDIR} 21 | PATHS ${CMAKE_INSTALL_PREFIX}/lib 22 | ${CMAKE_INSTALL_PREFIX}/lib64 23 | /usr/local/lib 24 | /usr/local/lib64 25 | /usr/lib 26 | /usr/lib64 27 | @libdir@ 28 | ) 29 | 30 | INCLUDE(FindPackageHandleStandardArgs) 31 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(PMTF DEFAULT_MSG PMTF_LIBRARIES PMTF_INCLUDE_DIRS) 32 | MARK_AS_ADVANCED(PMTF_LIBRARIES PMTF_INCLUDE_DIRS) 33 | -------------------------------------------------------------------------------- /emscripten-build.ini: -------------------------------------------------------------------------------- 1 | [constants] 2 | cflags = [] 3 | ldflags = ['-v'] 4 | # 'toolchain' must be supplied somehow 5 | # either replace this with the /absolute/path/to/emsdk 6 | # ... or supply a second machine file with this variable defined 7 | #toolchain = '' 8 | 9 | [binaries] 10 | ar = toolchain / 'emar' 11 | c = toolchain / 'emcc' 12 | cpp = toolchain / 'em++' 13 | ranlib = toolchain / 'emranlib' 14 | file_packager = toolchain / 'tools/file_packager' 15 | exe_wrapper = 'node' 16 | 17 | [properties] 18 | needs_exe_wrapper = true 19 | source_map_base = 'http://localhost:6931/' 20 | 21 | [built-in options] 22 | b_ndebug = 'true' 23 | b_pie = false 24 | b_pch = true 25 | b_staticpic = false 26 | c_args = cflags 27 | c_link_args = ldflags 28 | c_thread_count = 0 29 | cpp_args = cflags 30 | cpp_eh = 'none' 31 | cpp_link_args = ldflags 32 | cpp_rtti = false 33 | cpp_thread_count = 0 34 | default_library = 'static' 35 | optimization = 's' 36 | wrap_mode = 'forcefallback' 37 | 38 | [host_machine] 39 | cpu = 'mvp' 40 | cpu_family = 'wasm32' 41 | endian = 'little' 42 | system = 'emscripten' -------------------------------------------------------------------------------- /include/pmtv/base64/README.md: -------------------------------------------------------------------------------- 1 | B64 encoder chosen based on this stackoverflow: 2 | https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c/41094722#41094722 3 | -------------------------------------------------------------------------------- /include/pmtv/base64/base64.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. 3 | * 4 | * @APPLE_LICENSE_HEADER_START@ 5 | * 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. 7 | * 8 | * This file contains Original Code and/or Modifications of Original Code 9 | * as defined in and that are subject to the Apple Public Source License 10 | * Version 2.0 (the 'License'). You may not use this file except in 11 | * compliance with the License. Please obtain a copy of the License at 12 | * http://www.opensource.apple.com/apsl/ and read it before using this 13 | * file. 14 | * 15 | * The Original Code and all software distributed under the License are 16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 20 | * Please see the License for the specific language governing rights and 21 | * limitations under the License. 22 | * 23 | * @APPLE_LICENSE_HEADER_END@ 24 | */ 25 | /* ==================================================================== 26 | * Copyright (c) 1995-1999 The Apache Group. All rights reserved. 27 | * 28 | * Redistribution and use in source and binary forms, with or without 29 | * modification, are permitted provided that the following conditions 30 | * are met: 31 | * 32 | * 1. Redistributions of source code must retain the above copyright 33 | * notice, this list of conditions and the following disclaimer. 34 | * 35 | * 2. Redistributions in binary form must reproduce the above copyright 36 | * notice, this list of conditions and the following disclaimer in 37 | * the documentation and/or other materials provided with the 38 | * distribution. 39 | * 40 | * 3. All advertising materials mentioning features or use of this 41 | * software must display the following acknowledgment: 42 | * "This product includes software developed by the Apache Group 43 | * for use in the Apache HTTP server project (http://www.apache.org/)." 44 | * 45 | * 4. The names "Apache Server" and "Apache Group" must not be used to 46 | * endorse or promote products derived from this software without 47 | * prior written permission. For written permission, please contact 48 | * apache@apache.org. 49 | * 50 | * 5. Products derived from this software may not be called "Apache" 51 | * nor may "Apache" appear in their names without prior written 52 | * permission of the Apache Group. 53 | * 54 | * 6. Redistributions of any form whatsoever must retain the following 55 | * acknowledgment: 56 | * "This product includes software developed by the Apache Group 57 | * for use in the Apache HTTP server project (http://www.apache.org/)." 58 | * 59 | * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY 60 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 62 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR 63 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 64 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 65 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 66 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 68 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 69 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 70 | * OF THE POSSIBILITY OF SUCH DAMAGE. 71 | * ==================================================================== 72 | * 73 | * This software consists of voluntary contributions made by many 74 | * individuals on behalf of the Apache Group and was originally based 75 | * on public domain software written at the National Center for 76 | * Supercomputing Applications, University of Illinois, Urbana-Champaign. 77 | * For more information on the Apache Group and the Apache HTTP server 78 | * project, please see . 79 | * 80 | */ 81 | 82 | 83 | #ifndef _BASE64_H_ 84 | #define _BASE64_H_ 85 | 86 | #include 87 | 88 | #ifdef __cplusplus 89 | extern "C" { 90 | #endif 91 | 92 | // int Base64encode_len(int len); 93 | // int Base64encode(char* coded_dst, const char* plain_src, int len_plain_src); 94 | 95 | // int Base64decode_len(const char* coded_src); 96 | // int Base64decode(char* plain_dst, const char* coded_src); 97 | 98 | // #ifdef __cplusplus 99 | // } 100 | // #endif 101 | 102 | /* Base64 encoder/decoder. Originally Apache file ap_base64.c 103 | */ 104 | 105 | /* aaaack but it's fast and const should make it shared text page. */ 106 | static const unsigned char pr2six[256] = { 107 | /* ASCII table */ 108 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 109 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 110 | 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 111 | 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 112 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, 26, 27, 28, 113 | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 114 | 49, 50, 51, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 115 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 116 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 117 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 118 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 119 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 120 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 121 | }; 122 | 123 | static inline int Base64decode_len(const char* bufcoded) 124 | { 125 | const auto* bufin {reinterpret_cast(bufcoded)}; 126 | while (pr2six[*(bufin++)] <= 63) 127 | ; 128 | auto nprbytes {static_cast(bufin - reinterpret_cast(bufcoded)) - 1}; 129 | return ((nprbytes + 3) / 4) * 3 + 1; 130 | } 131 | 132 | static inline int Base64decode(char* bufplain, const char* bufcoded) 133 | { 134 | const auto* bufin {reinterpret_cast(bufcoded)}; 135 | while (pr2six[*(bufin++)] <= 63) 136 | ; 137 | auto nprbytes {static_cast(bufin - reinterpret_cast(bufcoded)) - 1}; 138 | 139 | auto* bufout {reinterpret_cast(bufplain)}; 140 | bufin = reinterpret_cast(bufcoded); 141 | 142 | while (nprbytes > 4) { 143 | *(bufout++) = static_cast(pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); 144 | *(bufout++) = static_cast(pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); 145 | *(bufout++) = static_cast(pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); 146 | bufin += 4; 147 | nprbytes -= 4; 148 | } 149 | 150 | /* Note: (nprbytes == 1) would be an error, so just ingore that case */ 151 | if (nprbytes > 1) { 152 | *(bufout++) = static_cast(pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); 153 | } 154 | if (nprbytes > 2) { 155 | *(bufout++) = static_cast(pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); 156 | } 157 | if (nprbytes > 3) { 158 | *(bufout++) = static_cast(pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); 159 | } 160 | 161 | *(bufout++) = '\0'; 162 | return ((nprbytes + 3) / 4) * 3 - ((4 - nprbytes) & 3); 163 | } 164 | 165 | static const char basis_64[] = 166 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 167 | 168 | static inline int Base64encode_len(int len) { return ((len + 2) / 3 * 4) + 1; } 169 | 170 | static inline int Base64encode(char* encoded, const char* string, int len) 171 | { 172 | int i {0}; 173 | char* p {encoded}; 174 | for (i = 0; i < len - 2; i += 3) { 175 | *p++ = basis_64[(string[i] >> 2) & 0x3F]; 176 | *p++ = basis_64[((string[i] & 0x3) << 4) | ((string[i + 1] & 0xF0) >> 4)]; 177 | *p++ = basis_64[((string[i + 1] & 0xF) << 2) | ((string[i + 2] & 0xC0) >> 6)]; 178 | *p++ = basis_64[string[i + 2] & 0x3F]; 179 | } 180 | if (i < len) { 181 | *p++ = basis_64[(string[i] >> 2) & 0x3F]; 182 | if (i == (len - 1)) { 183 | *p++ = basis_64[((string[i] & 0x3) << 4)]; 184 | *p++ = '='; 185 | } 186 | else { 187 | *p++ = basis_64[((string[i] & 0x3) << 4) | ((string[i + 1] & 0xF0) >> 4)]; 188 | *p++ = basis_64[((string[i + 1] & 0xF) << 2)]; 189 | } 190 | *p++ = '='; 191 | } 192 | 193 | *p++ = '\0'; 194 | return static_cast(p - encoded); 195 | } 196 | 197 | #ifdef __cplusplus 198 | } 199 | #endif 200 | 201 | #endif //_BASE64_H_ -------------------------------------------------------------------------------- /include/pmtv/format.hpp: -------------------------------------------------------------------------------- 1 | // Support for std::format is really spotty. 2 | // Gcc12 does not support it. 3 | // Eventually replace with std::format when that is widely available. 4 | #include 5 | #include 6 | 7 | namespace fmt { 8 | template <> 9 | struct formatter { 10 | template 11 | constexpr auto parse(ParseContext& ctx) { 12 | return ctx.begin(); 13 | } 14 | 15 | template 16 | auto format(const pmtv::map_t::value_type& kv, FormatContext& ctx) const { 17 | return fmt::format_to(ctx.out(), "{}: {}", kv.first, kv.second); 18 | } 19 | }; 20 | 21 | template 22 | struct formatter { 23 | template 24 | constexpr auto parse(ParseContext& ctx) { 25 | return ctx.begin(); 26 | } 27 | 28 | template 29 | auto format(const C& arg, FormatContext& ctx) const { 30 | if (arg.imag() >= 0) 31 | return fmt::format_to(ctx.out(), "{0}+j{1}", arg.real(), arg.imag()); 32 | else 33 | return fmt::format_to(ctx.out(), "{0}-j{1}", arg.real(), -arg.imag()); 34 | } 35 | }; 36 | 37 | 38 | template 39 | struct formatter

40 | { 41 | 42 | template 43 | constexpr auto parse(ParseContext& ctx) { 44 | return ctx.begin(); 45 | } 46 | 47 | template 48 | auto format(const P& value, FormatContext& ctx) const { 49 | return std::visit([&ctx](const auto arg) { 50 | using namespace pmtv; 51 | using T = std::decay_t; 52 | if constexpr (Scalar || Complex) 53 | return fmt::format_to(ctx.out(), "{}", arg); 54 | else if constexpr (std::same_as) 55 | return fmt::format_to(ctx.out(), "{}", arg); 56 | else if constexpr (UniformVector || UniformStringVector) 57 | return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", ")); 58 | else if constexpr (std::same_as>) { 59 | return fmt::format_to(ctx.out(), "[{}]", fmt::join(arg, ", ")); 60 | } else if constexpr (PmtMap) { 61 | return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(arg, ", ")); 62 | } else if constexpr (std::same_as) 63 | return fmt::format_to(ctx.out(), "null"); 64 | return fmt::format_to(ctx.out(), "unknown type {}", typeid(T).name()); 65 | }, value); 66 | 67 | } 68 | }; 69 | 70 | } // namespace fmt 71 | 72 | namespace pmtv { 73 | template 74 | std::ostream& operator<<(std::ostream& os, const P& value) { 75 | os << fmt::format("{}", value); 76 | return os; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /include/pmtv/meson.build: -------------------------------------------------------------------------------- 1 | files = [ 2 | 'format.hpp', 3 | 'pmt.hpp', 4 | 'rva_variant.hpp', 5 | 'type_helpers.hpp', 6 | 'serialiser.hpp', 7 | 'version.hpp' 8 | ] 9 | 10 | install_headers(files, subdir : 'pmtv') 11 | install_headers('base64/base64.h', subdir : 'pmtv/base64') -------------------------------------------------------------------------------- /include/pmtv/pmt.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | namespace pmtv { 12 | 13 | using pmt = pmt_var_t; 14 | using map_t = std::map>; 15 | 16 | template 17 | inline constexpr std::in_place_type_t> vec_t{}; 18 | 19 | template 20 | concept IsPmt = std::is_same_v; 21 | 22 | // template 23 | // auto get_vector(V value) -> decltype(std::get>(value) { 24 | // return std::get>(value); 25 | // } 26 | template 27 | std::vector &get_vector(V value) { 28 | return std::get>(value); 29 | } 30 | 31 | template 32 | std::span get_span(V &value) { 33 | return std::span(std::get>(value)); 34 | } 35 | 36 | template 37 | map_t &get_map(V &value) { 38 | return std::get(value); 39 | } 40 | 41 | template 42 | size_t elements(const P &value) { 43 | return std::visit( 44 | [](const auto &arg) -> size_t { 45 | using T = std::decay_t; 46 | if constexpr (std::same_as) 47 | return 0; 48 | else if constexpr (std::ranges::range) 49 | return arg.size(); 50 | return 1; 51 | }, 52 | value.get_base()); 53 | } 54 | 55 | template 56 | size_t bytes_per_element(const P &value) { 57 | return std::visit( 58 | [](const auto &arg) -> size_t { 59 | using T = std::decay_t; 60 | if constexpr (std::same_as) 61 | return 0; 62 | else if constexpr (std::ranges::range) 63 | return sizeof(typename T::value_type); 64 | return sizeof(T); 65 | }, 66 | value.get_base()); 67 | } 68 | 69 | // Allows us to cast from a pmt like this: auto x = cast(mypmt); 70 | template 71 | T cast(const P &value) { 72 | return std::visit( 73 | [](const auto &arg) -> T { 74 | using U = std::decay_t; 75 | if constexpr (std::convertible_to || (Complex < T > && Complex < U > )) { 76 | if constexpr (Complex < T >) { 77 | if constexpr (std::integral || std::floating_point) { 78 | return std::complex(static_cast(arg)); 79 | } else { 80 | return static_cast(arg); 81 | } 82 | } else { 83 | return static_cast(arg); 84 | } 85 | } 86 | // else if constexpr (PmtMap && PmtMap) { 87 | // return std::get>>(arg); 88 | // } 89 | else 90 | throw std::runtime_error("Invalid PMT Cast " + std::string(typeid(T).name()) + " " + 91 | std::string(typeid(U).name())); 92 | }, 93 | value); 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /include/pmtv/rva_variant.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | From https://github.com/codeinred/recursive-variant 3 | 4 | Note that is may not be needed anymore in c++23. 5 | See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2162r0.html 6 | 7 | Boost Software License - Version 1.0 - August 17th, 2003 8 | 9 | © 2021 Alecto Irene Perez 10 | 11 | Permission is hereby granted, free of charge, to any person or organization 12 | obtaining a copy of the software and accompanying documentation covered by this 13 | license (the "Software") to use, reproduce, display, distribute, execute, and 14 | transmit the Software, and to prepare derivative works of the Software, and to 15 | permit third-parties to whom the Software is furnished to do so, all subject to 16 | the following: 17 | 18 | The copyright notices in the Software and this entire statement, including the 19 | above license grant, this restriction and the following disclaimer, must be 20 | included in all copies of the Software, in whole or in part, and all derivative 21 | works of the Software, especially those created in whole or in part by Deep 22 | Neural Networks, Language Models, or other such programs advertised as "AI" or 23 | as "Artificial Intelligence" or as "Machine Learning", either with or without 24 | human input or intervention, unless such copies or derivative works are solely 25 | in the form of machine-executable object code generated by a source language 26 | processor. 27 | 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 30 | FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 31 | COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES 32 | OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 33 | OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 34 | */ 35 | #ifndef RECURSIVE_VARIANT_AUTHORITY_VARIANT_HPP 36 | #define RECURSIVE_VARIANT_AUTHORITY_VARIANT_HPP 37 | #include 38 | #include 39 | 40 | namespace rva { 41 | /** 42 | * @brief replace is a template type used to implement replace_t. It provides 43 | * member, a using declaration named `type`. 44 | * 45 | * @tparam T the type to transform. 46 | * @tparam Find the type to find 47 | * @tparam Replace the type to replace it with. 48 | */ 49 | template 50 | struct replace; 51 | /** 52 | * @brief replace is a template that takes a type T (which itself might be a 53 | * template), and replaces all instances of *Find* with *Replace*. For example: 54 | * 55 | * - `replace_t` -> `int` 56 | * - `replace_t, char, int>` -> `std::vector` 57 | * 58 | * @tparam T the type to transform. 59 | * @tparam Find the type to find 60 | * @tparam Replace the type to replace it with. 61 | */ 62 | template 63 | using replace_t = typename replace::type; 64 | 65 | struct self_t { 66 | }; 67 | 68 | // See: https://en.cppreference.com/w/cpp/utility/variant 69 | template 70 | class variant : public std::variant>...> 71 | { 72 | public: 73 | using base_type = std::variant>...>; 74 | constexpr static bool nothrow_swappable = std::is_nothrow_swappable_v; 75 | 76 | using base_type::base_type; 77 | 78 | // Observers 79 | using base_type::index; 80 | using base_type::valueless_by_exception; 81 | 82 | // Modifiers 83 | using base_type::operator=; 84 | using base_type::emplace; 85 | using base_type::swap; 86 | 87 | variant() = default; 88 | variant(variant const&) = default; 89 | variant(variant&&) = default; 90 | 91 | variant& operator=(variant const&) = default; 92 | variant& operator=(variant&&) = default; 93 | 94 | constexpr void swap(variant& other) noexcept(nothrow_swappable) 95 | { 96 | base_type::swap(other); 97 | } 98 | constexpr base_type& get_base() & noexcept { return *this; } 99 | constexpr base_type const& get_base() const& noexcept { return *this; } 100 | constexpr base_type&& get_base() && noexcept { return *this; } 101 | constexpr base_type const&& get_base() const&& noexcept { return *this; } 102 | 103 | constexpr base_type* get_pointer_to_base() noexcept { return this; } 104 | constexpr base_type const* get_pointer_to_base() const noexcept { return this; } 105 | 106 | auto operator<=>(variant const&) const = default; 107 | bool operator==(variant const&) const = default; 108 | 109 | size_t size() 110 | { 111 | return std::visit( 112 | [](const auto& arg) -> size_t { 113 | using TYPE = std::decay_t; 114 | if constexpr (std::same_as) 115 | return 0; 116 | else if constexpr (std::ranges::range) 117 | return arg.size(); 118 | return 1; 119 | }, 120 | get_base()); 121 | } 122 | }; 123 | 124 | // See: https://en.cppreference.com/w/cpp/utility/variant/visit 125 | template 126 | constexpr decltype(auto) visit(Visitor&& visitor, Variants&&... variants) 127 | { 128 | return std::visit(std::forward(visitor), 129 | std::forward(variants).get_base()...); 130 | } 131 | template 132 | constexpr R visit(Visitor&& visitor, Variants&&... variants) 133 | { 134 | return std::visit(std::forward(visitor), 135 | std::forward(variants).get_base()...); 136 | } 137 | 138 | // See: https://en.cppreference.com/w/cpp/utility/variant/get 139 | template 140 | constexpr decltype(auto) get(rva::variant& v) 141 | { 142 | return std::get(std::forward(v).get_base()); 143 | } 144 | template 145 | constexpr decltype(auto) get(rva::variant&& v) 146 | { 147 | return std::get(std::forward(v).get_base()); 148 | } 149 | template 150 | constexpr decltype(auto) get(const rva::variant& v) 151 | { 152 | return std::get(std::forward(v).get_base()); 153 | } 154 | template 155 | constexpr decltype(auto) get(const rva::variant&& v) 156 | { 157 | return std::get(std::forward(v).get_base()); 158 | } 159 | template 160 | constexpr T& get(rva::variant& v) 161 | { 162 | return std::get(std::forward(v).get_base()); 163 | } 164 | template 165 | constexpr T&& get(rva::variant&& v) 166 | { 167 | return std::get(std::forward(v).get_base()); 168 | } 169 | template 170 | constexpr const T& get(const rva::variant& v) 171 | { 172 | return std::get(std::forward(v).get_base()); 173 | } 174 | template 175 | constexpr const T&& get(const rva::variant&& v) 176 | { 177 | return std::get(std::forward(v).get_base()); 178 | } 179 | 180 | // See: https://en.cppreference.com/w/cpp/utility/variant/get_if 181 | template 182 | constexpr auto* get_if(rva::variant* pv) noexcept 183 | { 184 | return std::get_if(pv->get_pointer_to_base()); 185 | } 186 | template 187 | constexpr auto const* get_if(const rva::variant* pv) noexcept 188 | { 189 | return std::get_if(pv->get_pointer_to_base()); 190 | } 191 | template 192 | constexpr auto* get_if(rva::variant* pv) noexcept 193 | { 194 | return std::get_if(pv->get_pointer_to_base()); 195 | } 196 | template 197 | constexpr auto const* get_if(const rva::variant* pv) noexcept 198 | { 199 | return std::get_if(pv->get_pointer_to_base()); 200 | } 201 | 202 | template 203 | constexpr bool holds_alternative(const rva::variant& v) noexcept 204 | { 205 | return std::holds_alternative(v.get_base()); 206 | } 207 | } // namespace rva 208 | 209 | template 210 | struct std::hash> : std::hash> { 211 | using base_type = std::hash>; 212 | using base_type::base_type; 213 | hash() = default; 214 | hash(hash const&) = default; 215 | hash(hash&&) = default; 216 | size_t operator()(rva::variant const& v) const 217 | { 218 | return base_type::operator()(v.get_base()); 219 | } 220 | }; 221 | 222 | template 223 | struct std::variant_size> 224 | : std::integral_constant { 225 | }; 226 | template 227 | struct std::variant_size> 228 | : std::integral_constant { 229 | }; 230 | 231 | template 232 | struct std::variant_alternative> 233 | : std::variant_alternative::base_type> { 234 | }; 235 | template 236 | struct std::variant_alternative> 237 | : std::variant_alternative::base_type> { 238 | }; 239 | 240 | // Implementation for replace 241 | namespace rva { 242 | template 243 | struct replace { 244 | using type = T; 245 | }; 246 | template 247 | struct replace { 248 | using type = Replace; 249 | }; 250 | template 251 | struct replace { 252 | using type = Replace*; 253 | }; 254 | template 255 | struct replace { 256 | using type = Replace&; 257 | }; 258 | template 259 | struct replace { 260 | using type = Replace&&; 261 | }; 262 | template 263 | struct replace { 264 | using type = Replace[]; 265 | }; 266 | template 267 | struct replace { 268 | using type = Replace[N]; 269 | }; 270 | template 271 | struct replace { 272 | using type = const Replace; 273 | }; 274 | template 275 | struct replace { 276 | using type = const Replace*; 277 | }; 278 | template 279 | struct replace { 280 | using type = const Replace&; 281 | }; 282 | template 283 | struct replace { 284 | using type = const Replace[]; 285 | }; 286 | template 287 | struct replace { 288 | using type = const Replace[N]; 289 | }; 290 | template 291 | struct replace { 292 | using type = replace_t*; 293 | }; 294 | template 295 | struct replace { 296 | using type = replace_t&; 297 | }; 298 | template 299 | struct replace { 300 | using type = replace_t&&; 301 | }; 302 | template 303 | struct replace { 304 | using type = replace_t[]; 305 | }; 306 | template 307 | struct replace { 308 | using type = replace_t[N]; 309 | }; 310 | template 311 | struct replace { 312 | using type = replace_t const; 313 | }; 314 | template 315 | struct replace { 316 | using type = replace_t const*; 317 | }; 318 | template 319 | struct replace { 320 | using type = replace_t const&; 321 | }; 322 | template 323 | struct replace { 324 | using type = replace_t const[]; 325 | }; 326 | template 327 | struct replace { 328 | using type = replace_t const[N]; 329 | }; 330 | 331 | template