Skip to content

Commit

Permalink
Provide the builtin "date" as example of a external type
Browse files Browse the repository at this point in the history
This is a demonstration that the generator can be further simplified by moving types which do not require dedicated generator magic to externally declared types.
  • Loading branch information
mknejp committed Jul 13, 2015
1 parent 6e958ef commit 55fcd90
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/source/meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object MExtern {
typename: String,
boxed: String, // Java typename used if boxing is required, must be an object.
reference: Boolean, // True if the unboxed type is an object reference and qualifies for any kind of "nonnull" annotation in Java. Only used for "record" types.
generic: Boolean, // Set to false to exclude type arguments from the Java class. Useful if template arguments are only used in C++.
generic: Boolean, // Set to false to exclude type arguments from the Java class. This is should be true by default. Useful if template arguments are only used in C++.
hash: String // A well-formed expression to get the hash value. Must be a format string with a single "%s" placeholder. Only used for "record" types types with "eq" deriving when needed.
)
case class Jni(
Expand Down
8 changes: 5 additions & 3 deletions test-suite/djinni/date.djinni
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@extern "date.yaml"

date_record = record {
created_at: date;
}
created_at: extern_date;
} deriving(eq, ord)

map_date_record = record {
dates_by_id: map<string, date>;
dates_by_id: map<string, extern_date>;
}
30 changes: 30 additions & 0 deletions test-suite/djinni/date.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This is an example YAML file mimicking the builtin "date" type as external type
---
name: extern_date
typedef: 'record deriving(eq, ord)'
params: []
prefix: ''
cpp:
typename: 'std::chrono::system_clock::time_point'
header: '<chrono>'
byValue: true
objc:
typename: 'NSDate'
header: '<Foundation/Foundation.h>'
boxed: 'NSDate'
pointer: true
hash: '(NSUInteger)%s.timeIntervalSinceReferenceDate'
objcpp:
translator: '::djinni::Date'
header: '"DJIMarshal+Private.h"'
java:
typename: 'java.util.Date'
boxed: 'java.util.Date'
reference: true
generic: true
hash: '%s.hashCode()'
jni:
translator: '::djinni::Date'
header: '"Marshal.hpp"'
typename: jobject
typeSignature: 'Ljava/util/Date;'
35 changes: 35 additions & 0 deletions test-suite/generated-src/cpp/date_record.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from date.djinni

#include "date_record.hpp" // my header


bool operator==(const DateRecord& lhs, const DateRecord& rhs) {
return lhs.created_at == rhs.created_at;
}

bool operator!=(const DateRecord& lhs, const DateRecord& rhs) {
return !(lhs == rhs);
}

bool operator<(const DateRecord& lhs, const DateRecord& rhs) {
if (lhs.created_at < rhs.created_at) {
return true;
}
if (rhs.created_at < lhs.created_at) {
return false;
}
return false;
}

bool operator>(const DateRecord& lhs, const DateRecord& rhs) {
return rhs < lhs;
}

bool operator<=(const DateRecord& lhs, const DateRecord& rhs) {
return !(rhs < lhs);
}

bool operator>=(const DateRecord& lhs, const DateRecord& rhs) {
return !(lhs < rhs);
}
9 changes: 9 additions & 0 deletions test-suite/generated-src/cpp/date_record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
struct DateRecord final {
std::chrono::system_clock::time_point created_at;

friend bool operator==(const DateRecord& lhs, const DateRecord& rhs);
friend bool operator!=(const DateRecord& lhs, const DateRecord& rhs);

friend bool operator<(const DateRecord& lhs, const DateRecord& rhs);
friend bool operator>(const DateRecord& lhs, const DateRecord& rhs);

friend bool operator<=(const DateRecord& lhs, const DateRecord& rhs);
friend bool operator>=(const DateRecord& lhs, const DateRecord& rhs);

DateRecord(std::chrono::system_clock::time_point created_at)
: created_at(std::move(created_at))
{}
Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/inFileList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ djinni/test.djinni
djinni/primtypes.djinni
djinni/constants.djinni
djinni/date.djinni
djinni/date.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,48 @@

package com.dropbox.djinni.test;

import java.util.Date;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public final class DateRecord {
public final class DateRecord implements Comparable<DateRecord> {


/*package*/ final Date mCreatedAt;
/*package*/ final java.util.Date mCreatedAt;

public DateRecord(
@Nonnull Date createdAt) {
@Nonnull java.util.Date createdAt) {
this.mCreatedAt = createdAt;
}

@Nonnull
public Date getCreatedAt() {
public java.util.Date getCreatedAt() {
return mCreatedAt;
}

@Override
public boolean equals(@CheckForNull Object obj) {
if (!(obj instanceof DateRecord)) {
return false;
}
DateRecord other = (DateRecord) obj;
return this.mCreatedAt.equals(other.mCreatedAt);
}

@Override
public int hashCode() {
// Pick an arbitrary non-zero starting value
int hashCode = 17;
hashCode = hashCode * 31 + (mCreatedAt.hashCode());
return hashCode;
}

@Override
public int compareTo(@Nonnull DateRecord other) {
int tempResult;
tempResult = this.mCreatedAt.compareTo(other.mCreatedAt);
if (tempResult != 0) {
return tempResult;
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@

package com.dropbox.djinni.test;

import java.util.Date;
import java.util.HashMap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

public final class MapDateRecord {


/*package*/ final HashMap<String, Date> mDatesById;
/*package*/ final HashMap<String, java.util.Date> mDatesById;

public MapDateRecord(
@Nonnull HashMap<String, Date> datesById) {
@Nonnull HashMap<String, java.util.Date> datesById) {
this.mDatesById = datesById;
}

@Nonnull
public HashMap<String, Date> getDatesById() {
public HashMap<String, java.util.Date> getDatesById() {
return mDatesById;
}
}
2 changes: 2 additions & 0 deletions test-suite/generated-src/objc/DBDateRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

@property (nonatomic, readonly, nonnull) NSDate * createdAt;

- (NSComparisonResult)compare:(nonnull DBDateRecord *)other;

@end
25 changes: 25 additions & 0 deletions test-suite/generated-src/objc/DBDateRecord.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,29 @@ - (id)initWithCreatedAt:(nonnull NSDate *)createdAt
return self;
}

- (BOOL)isEqual:(id)other
{
if (![other isKindOfClass:[DBDateRecord class]]) {
return NO;
}
DBDateRecord *typedOther = (DBDateRecord *)other;
return [self.createdAt isEqual:typedOther.createdAt];
}

- (NSUInteger)hash
{
return NSStringFromClass([self class]).hash ^
((NSUInteger)self.createdAt.timeIntervalSinceReferenceDate);
}

- (NSComparisonResult)compare:(DBDateRecord *)other
{
NSComparisonResult tempResult;
tempResult = [self.createdAt compare:other.createdAt];
if (tempResult != NSOrderedSame) {
return tempResult;
}
return NSOrderedSame;
}

@end
1 change: 1 addition & 0 deletions test-suite/generated-src/outFileList.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
djinni-output-temp/cpp/date_record.hpp
djinni-output-temp/cpp/date_record.cpp
djinni-output-temp/cpp/map_date_record.hpp
djinni-output-temp/cpp/constants.hpp
djinni-output-temp/cpp/constants.cpp
Expand Down

0 comments on commit 55fcd90

Please sign in to comment.