Note

You are not reading the most recent version of this documentation. 1.4.7 is the latest version available.

Attributes

enum Header

This enum is used by several of the other attributes to specify includes in the generated code.

case System(header : String)

header specifies the name of a system file to be included. The generated code for each system file included is #include <header.h>.

case User(header : String)

header specifies the name of a user file to be included. The generated code for each user file included is #include "header.h".

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.

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++.

attribute @mapToEnum(name : String, kind : EnumKind, header : List<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 enum MyEnum in the generated C++ code:

@CPP.mapToEnum("MyEnum", .Scoped, .User("MyEnum.h"))
enum E {
  case A
  case B
}

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

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 to a.f(b) in the generated C++ code.

attribute @mapToType(name : String, parameterStyle : ParameterStyle, header : List<Header>)
Applies to:External types

This attribute specifies how an external type should be mapped into the generated C++. The following example defines how the external datatype ChronoDuration is mapped to std::chrono::duration in the generated C++ code:

@CPP.mapToType("std::chrono::duration", CPP.ParameterStyle.Value, [CPP.Header.System("chrono")])
external type ChronoDuration
attribute @mapToValue(name : String, header : List<Header>)
Applies to:External functions

This attribute specifies how external functions are to be mapped into the generated C++. The following example defines how a reference to the standard library std::this_thread::sleep_for function can be accessed:

@CPP.mapToValue("std::this_thread::sleep_for", [CPP.Header.System("thread"), CPP.Header.System("chrono")])
external function chronoSleep(t : Duration) : Nil = {}

Advanced Attributes

attribute @binaryOperator(operator : BinaryOperator)

Used to specify that the given Coco function, which must be a function with exactly two arguments, should be mapped to the given operator in C++.

enum BinaryOperator
case ArrayDereference
case Divide
case Equals
case GreaterThan
case GreaterThanEquals
case LessThan
case LessThanEquals
case LogicalAnd
case LogicalOr
case Modulus
case Minus
case NotEquals
case Plus
case Times
attribute @include(header : List<Header>)

When applied to a Coco declaration, will cause any usage of this declaration to contain the given includes. For example:

@CPP.include([CPP.Header.User("hdr")])
port P { ... }

will include #include "hdr" in the generated C++ file that includes P.

attribute @implicitCast
Applies to:External functions with exactly one argument

This attribute can be used to specify that an external function represents an implicit cast in C++ and therefored 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 as y.

attribute @unaryOperator(operator : UnaryOperator)

Used to specify that the given Coco function, which must be a function with exactly one argument, should be mapped to the given operator in C++.

enum UnaryOperator
case LogicalNot
case Negate
case PreDecrement
case PreIncrement
case PostDecrement
case PostIncrement