Commit 54df6f11 authored by Andrew Twyman's avatar Andrew Twyman

Test const methods

Added a test to prove that const methods for C++ work, and documented rules for const & static methods.
parent 14ac0f69
......@@ -225,6 +225,7 @@ methods. Djinni is capable of generating equality and order comparators, impleme
as operator overloading in C++ and standard comparison functions in Java / Objective-C.
Things to note:
- All fields in the record are compared in the order they appear in the record declaration.
If you need to add a field later, make sure the order is correct.
- Ordering comparison is not supported for collection types, optionals, and booleans.
......@@ -232,6 +233,18 @@ Things to note:
types of comparators as the outer record.
### Interface
#### Special Methods for C++ Only
`+c` interfaces (implementable only in C++) can have methods flagged with the special keywords const and static which have special effects in C++:
special_methods = interface +c {
const accessor_method();
static factory_method();
}
- `const` methods will be declared as const in C++, though this cannot be enforced on callers in other languages, which lack this feature.
- `static` methods will become a static method of the C++ class, which can be called from other languages without an object. This is often useful for factory methods to act as a cross-language constructor.
#### Exception Handling
When an interface implemented in C++ throws a `std::exception`, it will be translated to a
`java.lang.RuntimeException` in Java or an `NSException` in Objective-C. The `what()` message
......
......@@ -264,15 +264,17 @@ private def resolveRecord(scope: Scope, r: Record) {
}
private def resolveInterface(scope: Scope, i: Interface) {
// Check for static methods in Java or Objective-C; not allowed
// Const and static methods are only allowed on +c (only) interfaces
if (i.ext.java || i.ext.objc) {
for (m <- i.methods) {
if (m.static)
throw Error(m.ident.loc, "static not allowed for +j or +o interfaces").toException
if (m.const)
throw Error(m.ident.loc, "const method not allowed for +j or +o interfaces").toException
throw Error(m.ident.loc, "const method not allowed for +j or +o +p interfaces").toException
}
}
// Static+const isn't valid
if (i.ext.cpp) {
for (m <- i.methods) {
if (m.static && m.const)
......
......@@ -17,7 +17,7 @@ client_interface = interface +j +o {
}
reverse_client_interface = interface +c {
return_str(): string;
const return_str(): string;
meth_taking_interface(i: reverse_client_interface): string;
meth_taking_optional_interface(i: optional<reverse_client_interface>): string;
......
......@@ -13,7 +13,7 @@ class ReverseClientInterface {
public:
virtual ~ReverseClientInterface() {}
virtual std::string return_str() = 0;
virtual std::string return_str() const = 0;
virtual std::string meth_taking_interface(const std::shared_ptr<ReverseClientInterface> & i) = 0;
......
......@@ -2,7 +2,7 @@
namespace testsuite {
std::string ReverseClientInterfaceImpl::return_str() {
std::string ReverseClientInterfaceImpl::return_str() const {
return "test";
}
......
......@@ -7,7 +7,7 @@ class ReverseClientInterfaceImpl : public ReverseClientInterface {
ReverseClientInterfaceImpl() {}
virtual ~ReverseClientInterfaceImpl() {}
virtual std::string return_str() override;
virtual std::string return_str() const override;
virtual std::string meth_taking_interface(const std::shared_ptr<ReverseClientInterface> & i) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment