C++11 model generation
Project description
C++ Model Generation (CMG)
This package provides a command-line utility cmg and Python dataclasses to describe a schema, then use that schema to generate a model in C++.
You can use this for applications that require complex modeling and high performance.
The resulting code uses C++11 smart pointers for easier memory management.
Installation
Using pip:
pip install cmg
Using poetry:
poetry add cmg
Command-line
Use the cmg command to convert a schema into a set of C++ files:
For example:
cmg --schema examples/solar_system.py --output solar_system
Will create a directory called solar_system containing the following files:
solar_system/
CMakeLists.txt - Example CMakeLists.txt to create a library and build the tests
planet.cpp - Planet class implementation
planet.hpp - Planet class header
root.cpp - Root class implementation
root.hpp - Root class header
solar_system.cpp - SolarSystem class implementation
solar_system.hpp - SolarSystem class header
star.cpp - Star class implementation
star.hpp - Star class header
test_solar_system.cpp - Test suite, using GTest
You can use the CMakeLists.txt file as a starting point for your own build.
NOTE: the whole output directory will be removed and re-created every time you run
cmg.
Schema Structure
The schema consists of a set of classes (Klass) which contain fields (Field).
A tree structure can be created using parent-child relationships (solid lines). Then references can be created between classes in the DAG hierarchy (dotted lines).
Examples
Please see the example schemas in the examples directory.
API Documentation
Full API documentation is available here.
C++ model usage
Each class contains create, update and destroy methods to build and modify your data structure.
Create
Objects are created using the static method create, for example:
auto root = Root::create();
auto solarSystem = SolarSystem::create(root, "The Solar System");
auto sun = Sun::create(solarSystem, "The Sun");
auto earth = Planet::create(solarSystem, "Earth");
earth.lock()->setStar(star);
Classes without parents will create shared pointers, child classes will create weak pointers.
The root variable will be a shared_ptr. The sun and solarSystem variables will be weak_ptr.
Read
Instances of child classes need to be locked before use:
sun.lock()->getName(); // Returns "The Sun"
Instances of root classes (i.e. classes without parents) can be used directly, without lock()
Update
Each field has a getter and setter:
sun.lock()->setName(std::string("Renamed"));
sun.lock()->getName(); // Returns "Renamed"
Destroy
Objects are usually destroyed automatically when their shared_ptr count is zero. But if a parent has a reference to a child, then it will never be deleted from memory. So, calling destroy() on a parent instance will also destroy its children. The parent shared_ptr will still be in memory until the variable referencing it goes out of scope.
Checking references to objects have not expired
In the solar_system example, a Sun is owned by the Root class, but it is referenced by the SolarSystem class.
You can check if a reference to another object is not expired before using it, like this:
auto sun = earth.lock()->getSun();
if (!sun.expired())
{
std::cout << sun->lock().getName() << std::endl;
}
Getting shared_ptr from a child object
To make a shared_ptr using a child's weak_ptr:
auto earthPtr = earth.lock()->getptr();
std::cout << earthPtr.getName() << std::endl;
std::cout << earthPtr.getMass() << std::endl;
Use this to lock a weak object until it goes out of scope. This saves you from locking the object every time you want to call one of its functions.
Help and bug reporting
Please ask any questions or report bugs to the GitHub issues page: https://github.com/johndru-astrophysics/cmg/issues
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cmg-1.0.0.tar.gz.
File metadata
- Download URL: cmg-1.0.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c7ca2e684ceb8fed9fabca3f3b1ff4ff7359c2d1c4a82c55427d2252f941f2d
|
|
| MD5 |
f78fa7bf4109bb1223cbf33a4c20a6f9
|
|
| BLAKE2b-256 |
717e5cdc568e1c4b9b5513972ea9e8f521d8ec54f6128da1df0ba66351880f28
|
Provenance
The following attestation bundles were made for cmg-1.0.0.tar.gz:
Publisher:
python-publish.yml on johndru-astrophysics/cmg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cmg-1.0.0.tar.gz -
Subject digest:
5c7ca2e684ceb8fed9fabca3f3b1ff4ff7359c2d1c4a82c55427d2252f941f2d - Sigstore transparency entry: 157879221
- Sigstore integration time:
-
Permalink:
johndru-astrophysics/cmg@2fdf879070d43b88dc69f6813533811e4f858bcf -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/johndru-astrophysics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2fdf879070d43b88dc69f6813533811e4f858bcf -
Trigger Event:
release
-
Statement type:
File details
Details for the file cmg-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cmg-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e4e479fe51ade1872d1dca355fa0e94f33fd4ad299ab961cd4d9539afa10c8d
|
|
| MD5 |
203f28a68de2faeac56adef837d722e2
|
|
| BLAKE2b-256 |
1b436c76f66076f44316b74587fecc4ab9130d6a26b0745819857505a617c675
|
Provenance
The following attestation bundles were made for cmg-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on johndru-astrophysics/cmg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cmg-1.0.0-py3-none-any.whl -
Subject digest:
0e4e479fe51ade1872d1dca355fa0e94f33fd4ad299ab961cd4d9539afa10c8d - Sigstore transparency entry: 157879222
- Sigstore integration time:
-
Permalink:
johndru-astrophysics/cmg@2fdf879070d43b88dc69f6813533811e4f858bcf -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/johndru-astrophysics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@2fdf879070d43b88dc69f6813533811e4f858bcf -
Trigger Event:
release
-
Statement type: