Skip to main content

A C/C++ source generation package.

Project description

Cxx

A C/C++ source generation package.

Usage

First, some context:

  • Documents contain declarations.
  • Declarations contain other declarations, statements and expressions.
  • Some statements contain other statements and expressions.
  • Expressions are either operators or literals. Operators contain other expressions.

The general usage flow is:

  • Populate a Document with Declarations of your choice.
  • Format it as a string with any DocumentFormatter subclass.

Simple Example

import cxx

even_odd_enum = (
    cxx.EnumBuilder("EvenOdd", scoped=True)
    .add_member("Even")
    .add_member("Odd")
    .build()
)

get_even_odd_function = (
    cxx.FunctionBuilder("GetEvenOdd", "EvenOdd")
    .add_specifier("inline")
    .add_argument("int", "value", cxx.literals.integer(0))
    .add_statement(
        cxx.statements.return_(
            cxx.operators.other.conditional(
                cxx.operators.arithmetic.remainder(
                    cxx.literals.identifier("value"),
                    cxx.literals.integer(2),
                ),
                cxx.literals.identifier("EvenOdd::Odd"),
                cxx.literals.identifier("EvenOdd::Even"),
            )
        )
    )
    .build()
)

document = (
    cxx.DocumentBuilder(pragma_once=True)
    .add_declaration(even_odd_enum.definition)
    .add_declaration(get_even_odd_function.definition)
    .build()
)
formatter = cxx.ClangFormatter(configuration_style="google")
print(formatter.format(document))

Output:

#pragma once

enum class EvenOdd {
  Even,
  Odd,
};

inline EvenOdd GetEvenOdd(int value = 0) {
  return value % 2 ? EvenOdd::Odd : EvenOdd::Even;
}

"Factory" API

The "factory" API allows you to create any element:

  • cxx.document(...) -> Document
  • cxx.declarations.[specific declaration type](...) -> Declaration
  • cxx.literals.[specific literal type](...) -> Literal
  • cxx.operators.[specific operator type](...) -> Operator
  • cxx.statements.[specific statement type](...) -> Statement

Some elements are awkward to create this way - such as functions, which contain arguments, internal statements, specifiers, and so on. For those elements, you should use a builder.

Builder API

There are several builders:

  • cxx.ClassBuilder
  • cxx.EnumBuilder
  • cxx.FunctionBuilder
  • cxx.NamespaceBuilder
  • cxx.DocumentBuilder
  • cxx.CompoundStatementBuilder
  • cxx.FunctionCallOperatorBuilder
  • cxx.VariableDeclarationBuilder

Current Unsupported Features

This is a list of language features this package doesn't currently support. This is not due to any inherent difficulty - other things were prioritized.

  • Initializer list - int arr[3] = { 0, 1, 2 };
  • For-range loop - for (auto a : vec) { ... }
  • Structured binding - auto [a, b] = ...;
  • Multiple-variable simple declaration - int a=5, b=2;
  • Multiple-type typedef declaration - typedef int INT, *PINT;
  • Typedef-specifier for declaration - typedef struct { ... } S;
  • Exception handling - try, catch, throw
  • Template and template specialization - template <typename T>
  • New operator - new
  • Delete operator - delete
  • Float/decimal literals - 0.123

Development Setup

Install and configure poetry:

pip3 install poetry
poetry config virtualenvs.in-project true

Install development dependencies and pre-commit hook

poetry install --with dev
pre-commit install

Run the tests:

poetry run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cxx-0.2.0.tar.gz (23.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cxx-0.2.0-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file cxx-0.2.0.tar.gz.

File metadata

  • Download URL: cxx-0.2.0.tar.gz
  • Upload date:
  • Size: 23.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1011-azure

File hashes

Hashes for cxx-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b2852d667beebed71f527188aa00e2eb0355049a6ccc8a083bb1b24486347216
MD5 b00d6ba91848173f7407fc4c15aa6190
BLAKE2b-256 6be4f45c6fce5e5d26eaeafeeb1eef70f0e5d8cf1b880d2fb70dd1bd2a26856f

See more details on using hashes here.

File details

Details for the file cxx-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cxx-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.5 Linux/6.2.0-1011-azure

File hashes

Hashes for cxx-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccb2f6ad1c68d593d683af072075f29472ce7803a86d8e8dd13a9fdeea12f75b
MD5 8793f3786623eacfa94483d67a3b5f1f
BLAKE2b-256 0195d8c7d810733e6a6240b39ed92d311a11352f53c0e12477e93c9a8b7d9155

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page