This project contains a library for C++ AST parsing, metaprogramming and reflection

Overview

Meta C++

This project contains a library for C++ AST parsing, metaprogramming and reflection.

Also included is a tool for generating the necessary meta and reflection information for use by each of the respective libraries.

Dependencies

  • CMake 3.21+
  • A C++17 compiler
  • Libclang
  • Boost system

Including in a project

Although standalone compilation is supported and is important for bug-testing and development, the entire project is intended to be included as a subproject.

Fetching the library

As a git submodule (recommended)

From the source directory of a project initialized with git run the following command:

git submodule add --depth 1 https://github.com/RamblingMadMan/metacpp.git deps/metacpp

Replace deps/metacpp with your own location if required.

Then from within your projects main CMakeLists.txt add the following before any targets that depend on the library:

add_subdirectory(deps/metacpp)

Using FetchContent

From within your projects main CMakeLists.txt add the following before any targets that depend on the library:

FetchContent_Declare(
    metapp
    GIT_REPOSITORY https://github.com/RamblingMadMan/metacpp.git
)

FetchContent_MakeAvailable(metapp)

Generating information

For any targets that will use the meta or refl libraries, add the following to the correct CMakeLists.txt:

target_reflect(
   )
  

Usage

CMakeLists.txt:

add_executable(example example.h example.cpp)

target_reflect(example)

example.h:

method2(); }; enum class example_enum{ case_0 = 69, case_1 = 420, case_3 = 1337 };">
#include <string_view>
#include <tuple>

[[my::attrib(1, "2", 3.0)]]
class example{
    public:
        void method1(std::string_view s) const noexcept;
        void method1(const std::string &str) noexcept;

        std::tuple<int, float, char> method2();
};

enum class example_enum{
	case_0 = 69,
	case_1 = 420,
	case_3 = 1337
};

example.cpp:

>([] { // get method names std::cout << "Method " << Method::name << "\n"; // as well as information about the method std::cout << "\t" "pointer type: " << meta::type_name << "\n"; // iterate over every parameter of a method meta::for_all ([] { // get parameter types using param_type = typename Param::type; // and names std::cout << "\t" "parameter '" << Param::name << "': " << meta::type_name << "\n"; }); // and don't forget the result type std::cout << "\t" "result type: " << meta::type_name << "\n"; }); // alias the info for the example class using example_info = meta::class_info ; // iterate over all class attributes meta::for_all >([] { // supports [[scoped::attributes]] if constexpr(!Attrib::scope.empty()){ std::cout << Attrib::scope << "::"; } // get attribute names std::cout << Attrib::name; // also supports [[attributes(with, "args")]] if constexpr(!Attrib::args::empty){ std::cout << "("; // iterate over all attribute arguments meta::for_all_i ([] { if constexpr(Idx != 0){ std::cout << ", "; } std::cout << Arg::value; }); std::cout << ")"; } }); using enum_info = meta::enum_info ; // get the name of an enum and if it is scoped std::cout << (enum_info::is_scoped ? "Scoped " : "") << "Enum " << enum_info::name << "\n"; // iterate over all enum values meta::for_all >([] { // get the value name and unsigned integer representation std::cout << "\t" "Enum value '" << Value::name << "' == " << Value::value << "\n"; }); // get enum values from strings at compile time static_assert(meta::get_value ("case_0") == static_cast (69)); static_assert(meta::get_value ("case_1") == static_cast (420)); static_assert(meta::get_value ("case_2") == static_cast (1337)); // -------------- // C++20 Features // -------------- // do compile-time queries on class info static_assert(example_info::query_methods<"method1">::size == 2); // specify a signature static_assert(example_info::query_methods<"method1", void(std::string_view)>::size == 1); // or just part of one static_assert(example_info::query_methods<"method2", meta::ignore()>::size == 1); // also supports attribute queries using my_attribs = example_info::query_attributes<"my", "attrib">; static_assert(my_attribs::size == 1); static_assert(my_attribs::args::size == 3); // do some checking on the attribute arguments static_assert(get_t ::value == R"(1)"); static_assert(get_t ::value == R"("2")"); static_assert(get_t ::value == R"(3.0)"); }">
#include <iostream>

#include "example.meta.h"

int main(int argc, char *argv[]){
    // --------------
    // C++17 Features
    // --------------

    // iterate over all public class methods
    meta::for_all
                            
                             >([]<
                             class 
                             Method>{

        
                             // get method names
        std::cout << 
                             "Method " << Method::name << 
                             "\n";

        
                             // as well as information about the method
        std::cout << 
                             "\t"  
                             "pointer type: " << meta::type_name
                             
                               << 
                              "\n";

        
                              // iterate over every parameter of a method
        meta::for_all<
                              typename Method::params>([]<
                              class 
                              Param>{

            
                              // get parameter types
            
                              using param_type = 
                              typename Param::type;

            
                              // and names
            std::cout << 
                              "\t"  
                              "parameter '" << Param::name << 
                              "': " << meta::type_name
                              
                                << 
                               "\n";

        });

        
                               // and don't forget the result type
        std::cout << 
                               "\t"  
                               "result type: " << meta::type_name<
                               typename Method::result> << 
                               "\n";
    });

    
                               // alias the info for the example class
    
                               using example_info = meta::class_info
                               
                                ;

    
                                // iterate over all class attributes
    meta::for_all
                                
                                 
                                  >([]<
                                  class 
                                  Attrib>{ 
                                  // supports [[scoped::attributes]] 
                                  if 
                                  constexpr(!Attrib::scope.
                                  empty()){ std::cout << Attrib::scope << 
                                  "::"; } 
                                  // get attribute names std::cout << Attrib::name; 
                                  // also supports [[attributes(with, "args")]] 
                                  if 
                                  constexpr(!Attrib::args::empty){ std::cout << 
                                  "("; 
                                  // iterate over all attribute arguments meta::for_all_i<
                                  typename Attrib::args>([]<
                                  class 
                                  Arg, std::
                                  size_t Idx>{ 
                                  if 
                                  constexpr(Idx != 
                                  0){ std::cout << 
                                  ", "; } std::cout << Arg::value; }); std::cout << 
                                  ")"; } }); 
                                  using enum_info = meta::enum_info
                                  
                                   ; 
                                   // get the name of an enum and if it is scoped std::cout << (enum_info::is_scoped ? 
                                   "Scoped " : 
                                   "") << 
                                   "Enum " << enum_info::name << 
                                   "\n"; 
                                   // iterate over all enum values meta::for_all
                                   
                                    
                                     >([]<
                                     class 
                                     Value>{ 
                                     // get the value name and unsigned integer representation std::cout << 
                                     "\t" 
                                     "Enum value '" << Value::name << 
                                     "' == " << Value::value << 
                                     "\n"; }); 
                                     // get enum values from strings at compile time 
                                     static_assert(meta::get_value
                                     
                                      (
                                      "case_0") == 
                                      static_cast
                                      
                                       (
                                       69)); 
                                       static_assert(meta::get_value
                                       
                                        (
                                        "case_1") == 
                                        static_cast
                                        
                                         (
                                         420)); 
                                         static_assert(meta::get_value
                                         
                                          (
                                          "case_2") == 
                                          static_cast
                                          
                                           (
                                           1337)); 
                                           // -------------- 
                                           // C++20 Features 
                                           // -------------- 
                                           // do compile-time queries on class info 
                                           static_assert(example_info::query_methods<
                                           "method1">::size == 
                                           2); 
                                           // specify a signature 
                                           static_assert(example_info::query_methods<
                                           "method1", 
                                           void(std::string_view)>::size == 
                                           1); 
                                           // or just part of one 
                                           static_assert(example_info::query_methods<
                                           "method2", 
                                           meta::ignore()>::size == 
                                           1); 
                                           // also supports attribute queries 
                                           using my_attribs = example_info::query_attributes<
                                           "my", 
                                           "attrib">; 
                                           static_assert(my_attribs::size == 
                                           1); 
                                           static_assert(my_attribs::args::size == 
                                           3); 
                                           // do some checking on the attribute arguments 
                                           static_assert(
                                           get_t
                                           
                                            0>::value == 
                                            R"(1)"); 
                                            static_assert(
                                            get_t
                                            
                                             1>::value == 
                                             R"("2")"); 
                                             static_assert(
                                             get_t
                                             
                                              2>::value == 
                                              R"(3.0)"); }
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
You might also like...
This project contains information on the PC-Link hardware interface for some early 2000 Sony HiFi decks
This project contains information on the PC-Link hardware interface for some early 2000 Sony HiFi decks

pclink-i2c This project contains information on the PC-Link hardware interface for some early 2000 Sony HiFi decks, such as the CMT-DC500MD (also know

A single-header C/C++ library for parsing and evaluation of arithmetic expressions

ceval A C/C++ header for parsing and evaluation of arithmetic expressions. [README file is almost identical to that of the ceval library] Functions ac

A single-header C/C++ library for parsing and evaluation of arithmetic expressions

ceval A C/C++ header for parsing and evaluation of arithmetic expressions. [README file is almost identical to that of the ceval library] Functions ac

An 802.11 Frame Generation and Parsing Library in C

libwifi 802.11 Parsing / Generation library Build Status OS Architecture Linux x86_64 What is this? libwifi is a C library with a permissive license f

Just a basic mini library for parsing simple files that only have variables written and with Lua extension.

C++ Parser Lua file config Just a basic mini library for parsing simple files that only have variables written and with Lua extension. Note: At the mo

ZSV/lib: a fast CSV parsing library and standalone utility
ZSV/lib: a fast CSV parsing library and standalone utility

Please note: this code is still alpha / pre-production. Everything here should be considered preliminary. If you like ZSVlib, please give it a star! Z

A C/C++ library for parsing and evaluation of arithmetic expressions.

ceval A C/C++ header for parsing and evaluation of arithmetic expressions. Functions accessibe from main() Function Argument(s) Return Value ceval_res

Parsing Expression Grammar Template Library
Parsing Expression Grammar Template Library

Welcome to the PEGTL The Parsing Expression Grammar Template Library (PEGTL) is a zero-dependency C++ header-only parser combinator library for creati

A lightweight C++14 parsing library for tmx map files created with the Tiled map editor

tmxlite Description A lightweight C++14 parsing library for tmx map files created with the Tiled map editor. Requires no external linking, all depende

Owner
Keith Hammond
Keith Hammond
Intuitive & Powerful C++20 consteval metaprogramming library(via value).

meta-value-list This library provides a bunch of consteval toolsets to do metaprogramming, and provides the pipeline syntactic sugar for function comb

Netcan 35 Oct 21, 2022
Intuitive & Powerful C++20 consteval metaprogramming library(via value).

meta-list This library provides a bunch of consteval toolsets to do metaprogramming, and provides the pipeline syntactic sugar for function combinatio

Netcan 35 Oct 21, 2022
A tiny metaprogramming library

Meta: A tiny metaprogramming library Meta is a tiny and header-only C++11 metaprogramming library released under the Boost Software License. Supported

Eric Niebler 272 Nov 22, 2022
Creating sepia, reflection, grayscale, and blur filters from scratch and returns a modified image

image-filter Created sepia, reflection, grayscale, and blur filters from scratch and returning a modified image Directories: images: contains sample i

Martin Guevara 1 Oct 14, 2021
Meta - static reflection tools for c++. i mostly use this with entt.

meta Static reflection tools for C++. I use it with EnTT but it can work with anything. The main features the library provides are: Registering types

Nikhilesh S 9 Jul 12, 2022
C parsing, semantic analys, generate a graph from a source code. An educational project during my third year of Computer Science Licence.

Pour compiler le programme, il suffit d'exécuter compiler.sh avec la commande "./compiler.sh" en se trouvant dans le dossier racine du projet. Un fich

Jean Philippe Carlens 3 Feb 3, 2022
This project contains the main ROS 2 packages of Xiaomi CyberDog®.

Xiaomi CyberDog ROS 2 文档包含简体中文和English 简介 - Introduction 本项目包含小米铁蛋®的ROS 2主要功能包. This project contains the main ROS 2 packages of Xiaomi CyberDog®. 基本信

null 383 Dec 31, 2022
Contains a sudoku solver - OCR. Project done with classmates during third semester at EPITA.

sudokUwU sudokUwU is a sudoku solver made by 4 students at EPITA. This project is a mandatory work from S3 cycle! The Team Johan Tran Adrian Grillet V

okywu 2 May 5, 2022
This project contains three scripts to help working with the steam-runtime, especially outside of Steam.

This project contains three scripts to help working with the steam-runtime, especially outside of Steam. See these blog posts for more details: steam-

Jørgen P. Tjernø 12 Sep 27, 2022