Attributes¶
-
enum
Header
¶ This enum is used by several of the other attributes to specify includes in the generated code.
-
enum
ParameterStyle
¶ This enum specifies the way in which an external type is passed in a call to an external function.
-
case
ConstReference
¶ The parameter should be of type
const T&
.
-
case
Pointer
¶ The parameter should be of type
T*
.
-
case
RValueReference
¶ The parameter should be of type
T&&
.
-
case
Value
¶ The parameter should be of type
T
.
-
case
-
enum
EnumKind
¶ This enum specifies whether an enumeration is scoped or unscoped.
-
case
Scoped
¶ This is a scoped enumeration, and is therefore mapped to
enum class
in C++.
-
case
Unscoped
¶ This is an unscoped enumeration, and is therefore mapped to
enum
in C++.
-
case
-
attribute
@mapToEnum
(name : String, kind : EnumKind, header… : Header)¶ appliesto: Enumerations
This attribute specifies that Coco should not generate a C++ enum for this declaration, but should instead reuse a pre-existing declaration. The following example specifies how the Coco enum
E
is mapped to the scoped enumMyEnum
in the generated C++ code:By default, the code generator will assume that all enum cases have the same name in C++. However, enum cases can also be labelled with the
@mapToValue
attribute to map them to different C++ names, as illustrated in the following example:@CPP.mapToEnum("Name::Space::MyNestedEnum", .Unscoped, .User("MyNestedEnum.h")) enum NestedE { case A @CPP.mapToValue("Remapped") case B }
For
enum
declarations that are labelled with@mapToEnum
, we advise also labelling them with the@nonExhaustive
attribute, in order to make this mapping more robust. This will help cover the case where the Coco version of an enum accidentally omits some cases defined in the corresponding version that it is mapped to. This can arise, for example, if the C++ version is extended with additional cases, and the Coco version is not by mistake.See also
@nonExhaustive
attribute.
-
attribute
@mapToMember
(name : String)¶ Applies to: External functions
with at least one argument, and external member functions with any number of arguments.This attribute specifies that a function should be mapped to a member function of the first argument instead of to a top-level function. For example, given:
@CPP.mapToMember("f") external function f(x : T, y : Bool) : Nil = {}
then an expression like
f(a, b)
will be mapped toa.f(b)
in the generated C++ code.
-
attribute
@mapToType
(name : String, parameterStyle : ParameterStyle, header… : Header)¶ Applies to: External types
This attribute specifies how an external type should be mapped into the generated C++. The following example shows how the external datatype
ChronoDuration
is mapped tostd::chrono::duration
in the generated C++ code:
-
attribute
@mapToValue
(name : String, header… : Header)¶ Applies to: External functions
andexternal constants
.This attribute specifies how external functions and external constants are to be mapped into the generated C++. The following example shows how a reference to the standard library
std::this_thread::sleep_for
function can be accessed:Similarly, the following example illustrates how an external constant can be mapped to a constant value declared in C++:
@CPP.mapToValue("cocotec::project::kTimeoutPeriod", .User("cocotec/constants.h")) external val kTimeoutPeriod : Duration = _
Advanced Attributes¶
-
attribute
@binaryOperator
(operator : BinaryOperator)¶ Applies to: External functions
with precisely two arguments, and external member functions with precisely one argument.This attribute is used to specify that the given Coco function should be mapped to the given operator in C++.
-
attribute
@defaultHeaders
(header… : Header)¶ Applies to: module
When applied to a Coco module, this attribute will automatically add the specified headers to all occurrences of
@mapToType
,@mapToValue
, and@mapToEnum
within the module, thereby reducing duplication. For example:In this example, the headers listed in the
@defaultHeaders
attribute on themodule
will be added to the@mapToType
attribute applied to the external type declaration.In order to apply attributes to Coco modules, the keyword
module
must be explicitly used and labelled with the corresponding attributes, as illustrated in the example above.
-
attribute
@include
(header… : Header)¶ Applies to: Declarations When applied to a Coco declaration, this attribute will cause any usage of this declaration to contain the given includes. For example:
will include
#include "hdr"
in the generated C++ file that includesP
.
-
attribute
@implicitCast
¶ Applies to: External functions
with exactly one argument.This attribute is used to specify that an external function represents an implicit cast in C++, and therefore does not need to be represented in the generated code. For example, given:
@CPP.implicitCast external function f(x : Int) : IntLike = _
f(y)
will appear in the generated code asy
.
-
attribute
@unaryOperator
(operator : UnaryOperator)¶ Applies to: External functions
with precisely one argument, and external member functions with no arguments.This attribute is used to specify that the given Coco function should be mapped to the given operator in C++.