Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Commit

Permalink
Test const methods
Browse files Browse the repository at this point in the history
Added a test to prove that const methods for C++ work, and documented rules for const & static methods.
  • Loading branch information
Andrew Twyman committed Apr 22, 2016
1 parent 14ac0f6 commit 54df6f1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,26 @@ 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.
- To compare records containing other records, the inner record must derive at least the same
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
Expand Down
6 changes: 4 additions & 2 deletions src/source/resolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test-suite/djinni/client_interface.djinni
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test-suite/generated-src/cpp/reverse_client_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace testsuite {

std::string ReverseClientInterfaceImpl::return_str() {
std::string ReverseClientInterfaceImpl::return_str() const {
return "test";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 54df6f1

Please sign in to comment.