Giter Club home page Giter Club logo

header-parser's Introduction

header-parser

Extracts information from an annotated C++ header and outputs it as JSON for use in code generation tools.

Build Status

Why?

During my game development carreer I have found the availability of RunTime Type Information very useful in for example the following use cases:

  • Serialization (binary and text based)
  • Script bindings
  • Remote procedure calls
  • Generating models (for use in other languages)
  • Dependency injection
  • Reflection

Adding this information to my C++ source always resulted in a lot of code bloat. This library provides a way of extracting RTTI information for processing in other tools almost straight from C++ constructs.

Because the library is used to generate code it assumes the passed in code will compile. This makes sense because if the code already doesn't compile it also doesn't make sense to generate code for it (because it doesn't compile anyway). This allows the parser to be very simple and most importantly be extremely fast. For example it does not perform name lookups because it can assume names are already valid in the code project. It also doesn't substitute macros or includes. However, because of this, macros that define C++ constructs will not resolve correctly and therefor will not be included in the output.

This library is currently used in production at Abbey Games to generate extensive C++ Lua bindings and documentation for internal use.

Example

Given the input file:

#include <vector>

namespace test 
{
  TCLASS()
  class Foo : public Bar 
  {
  protected:
    TFUNC(Arg=3)
    bool ProtectedFunction(std::vector<int> args) const;

  public:
    TENUM()
    enum Enum
    {
      FirstValue,
      SecondValue = 3
    };

  public:
    TPROPERTY()
    int ThisIsAProperty;
  };
}

When ran with header-parser example1.h -c TCLASS -e TENUM -f TFUNC -p TPROPERTY produces the following output:

[
    {
        "type": "include",
        "file": "vector"
    },
    {
        "type": "namespace",
        "name": "test",
        "members": [
            {
                "type": "class",
                "line": 12,
                "meta": {},
                "name": "Foo",
                "parents": [
                    {
                        "access": "public",
                        "name": {
                            "type": "literal",
                            "name": "Bar"
                        }
                    }
                ],
                "members": [
                    {
                        "type": "function",
                        "macro": "TFUNC",
                        "line": 16,
                        "meta": {
                            "Arg": 3
                        },
                        "access": "protected",
                        "returnType": {
                            "type": "literal",
                            "name": "bool"
                        },
                        "name": "ProtectedFunction",
                        "arguments": [
                            {
                                "type": {
                                    "type": "template",
                                    "name": "std::vector",
                                    "arguments": [
                                        {
                                            "type": "literal",
                                            "name": "int"
                                        }
                                    ]
                                },
                                "name": "args"
                            }
                        ],
                        "const": true
                    },
                    {
                        "type": "enum",
                        "line": 20,
                        "access": "public",
                        "meta": {},
                        "name": "Enum",
                        "members": [
                            {
                                "key": "FirstValue"
                            },
                            {
                                "key": "SecondValue",
                                "value": "3"
                            }
                        ]
                    },
                    {
                        "type": "property",
                        "line": 28,
                        "meta": {},
                        "access": "public",
                        "dataType": {
                            "type": "literal",
                            "name": "int"
                        },
                        "name": "ThisIsAProperty"
                    }
                ]
            }
        ]
    }
]

header-parser's People

Contributors

baszalmstra avatar svddries avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.