Commit 55fcd90e authored by Miro Knejp's avatar Miro Knejp

Provide the builtin "date" as example of a external type

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.
parent 6e958ef2
...@@ -56,7 +56,7 @@ object MExtern { ...@@ -56,7 +56,7 @@ object MExtern {
typename: String, typename: String,
boxed: String, // Java typename used if boxing is required, must be an object. 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. 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. 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( case class Jni(
......
@extern "date.yaml"
date_record = record { date_record = record {
created_at: date; created_at: extern_date;
} } deriving(eq, ord)
map_date_record = record { map_date_record = record {
dates_by_id: map<string, date>; dates_by_id: map<string, extern_date>;
} }
# 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;'
// 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,6 +9,15 @@ ...@@ -9,6 +9,15 @@
struct DateRecord final { struct DateRecord final {
std::chrono::system_clock::time_point created_at; 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) DateRecord(std::chrono::system_clock::time_point created_at)
: created_at(std::move(created_at)) : created_at(std::move(created_at))
{} {}
......
...@@ -12,3 +12,4 @@ djinni/test.djinni ...@@ -12,3 +12,4 @@ djinni/test.djinni
djinni/primtypes.djinni djinni/primtypes.djinni
djinni/constants.djinni djinni/constants.djinni
djinni/date.djinni djinni/date.djinni
djinni/date.yaml
...@@ -3,22 +3,48 @@ ...@@ -3,22 +3,48 @@
package com.dropbox.djinni.test; package com.dropbox.djinni.test;
import java.util.Date;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; 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( public DateRecord(
@Nonnull Date createdAt) { @Nonnull java.util.Date createdAt) {
this.mCreatedAt = createdAt; this.mCreatedAt = createdAt;
} }
@Nonnull @Nonnull
public Date getCreatedAt() { public java.util.Date getCreatedAt() {
return mCreatedAt; 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;
}
} }
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
package com.dropbox.djinni.test; package com.dropbox.djinni.test;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
...@@ -11,15 +10,15 @@ import javax.annotation.Nonnull; ...@@ -11,15 +10,15 @@ import javax.annotation.Nonnull;
public final class MapDateRecord { public final class MapDateRecord {
/*package*/ final HashMap<String, Date> mDatesById; /*package*/ final HashMap<String, java.util.Date> mDatesById;
public MapDateRecord( public MapDateRecord(
@Nonnull HashMap<String, Date> datesById) { @Nonnull HashMap<String, java.util.Date> datesById) {
this.mDatesById = datesById; this.mDatesById = datesById;
} }
@Nonnull @Nonnull
public HashMap<String, Date> getDatesById() { public HashMap<String, java.util.Date> getDatesById() {
return mDatesById; return mDatesById;
} }
} }
...@@ -8,4 +8,6 @@ ...@@ -8,4 +8,6 @@
@property (nonatomic, readonly, nonnull) NSDate * createdAt; @property (nonatomic, readonly, nonnull) NSDate * createdAt;
- (NSComparisonResult)compare:(nonnull DBDateRecord *)other;
@end @end
...@@ -14,4 +14,29 @@ ...@@ -14,4 +14,29 @@
return self; 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 @end
djinni-output-temp/cpp/date_record.hpp 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/map_date_record.hpp
djinni-output-temp/cpp/constants.hpp djinni-output-temp/cpp/constants.hpp
djinni-output-temp/cpp/constants.cpp djinni-output-temp/cpp/constants.cpp
......
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