Class¶
Define a base class
base_class = CClass(
name="BaseClass",
members=[
CClass.Constructor(arguments=[CFunction.Argument('arg', Cuint32)], access=CClass.Access.public),
]
)
Define a class that inherits from it
my_class = CClass(
name="InheritingClass",
inherit_from=CClass.Inherit(base_class, access=CClass.Access.public),
members=[
CClass.Using(base_class.constructor, access=CClass.Access.public,
doc=Doc("Constructor", "Reusing constructor from base class")),
CClass.Method('my_method', arguments=[CFunction.Argument('hello', Cuint8)], access=CClass.Access.protected,
static=True, doc=Doc("My Method")),
CClass.Attribute('u8My_attr', Cuint8, initial_value=CLiteral(3), access=CClass.Access.private, static=True,
constexpr=True, doc=Doc("My Attribute")),
CClass.TypeMember(Cuint8.type('NewType'), access=CClass.Access.public,
doc=Doc("New Type", "Defining types inside classes is awesome")),
CClass.FreeStyle("uint8_t freestyle_member = 1;")
],
doc=Doc("Class Example", "This class holds methods and attributes to represent objects")
)
Declarations of classes are not typically used, sometimes for forward declarations.
print(my_class.declare().render())
class InheritingClass;
Definition holds defines all members of the class
print(my_class.define().render())
/**
* @brief Class Example
*
* This class holds methods and attributes to represent objects
*/
class InheritingClass: public BaseClass
{
public:
/**
* @brief Constructor
*
* Reusing constructor from base class
*/
using BaseClass::BaseClass;
/**
* @brief New Type
*
* Defining types inside classes is awesome
*/
typedef uint8_t NewType;
protected:
/**
* @brief My Method
*
* @param hello
*/
static void
my_method(uint8_t hello);
private:
/** @brief My Attribute */
static constexpr uint8_t u8My_attr = 3;
uint8_t freestyle_member = 1;
};
Style module also allows to print members in the order they were declared
style = Style()
style.class_members = Style.ClassMembers.inline_access_preserve_order
print(my_class.define().render(style))
/**
* @brief Class Example
*
* This class holds methods and attributes to represent objects
*/
class InheritingClass: public BaseClass
{
/**
* @brief Constructor
*
* Reusing constructor from base class
*/
public: using BaseClass::BaseClass;
/**
* @brief My Method
*
* @param hello
*/
protected: static void
my_method(uint8_t hello);
/** @brief My Attribute */
private: static constexpr uint8_t u8My_attr = 3;
/**
* @brief New Type
*
* Defining types inside classes is awesome
*/
public: typedef uint8_t NewType;
private: uint8_t freestyle_member = 1;
};
Definitions of members of a class (constructors, methods…) are typically defined together. There is a helper method to get all the members as a CStatements object
print(my_class.all_members_definition().render())
static void InheritingClass::my_method(uint8_t hello)
{
}
To create instance of a class, use the class as a type. Does not support instancing with arguments (Issue #12)
instance = CVariable(
name="my_instance",
c_type=my_class
)
print(instance.declare().render())
InheritingClass my_instance;