123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #import <Realm/RLMConstants.h>
- #import <Realm/RLMSwiftValueStorage.h>
- #import <Realm/RLMValue.h>
- #import <objc/runtime.h>
- #import <realm/array.hpp>
- #import <realm/binary_data.hpp>
- #import <realm/object-store/object.hpp>
- #import <realm/string_data.hpp>
- #import <realm/timestamp.hpp>
- #import <realm/util/file.hpp>
- namespace realm {
- class Decimal128;
- class Mixed;
- class RealmFileException;
- }
- class RLMClassInfo;
- @class RLMObjectSchema;
- @class RLMProperty;
- __attribute__((format(NSString, 1, 2)))
- NSException *RLMException(NSString *fmt, ...);
- NSException *RLMException(std::exception const& exception);
- NSError *RLMMakeError(RLMError code, std::exception const& exception);
- NSError *RLMMakeError(RLMError code, const realm::util::File::AccessError&);
- NSError *RLMMakeError(RLMError code, const realm::RealmFileException&);
- NSError *RLMMakeError(std::system_error const& exception);
- void RLMSetErrorOrThrow(NSError *error, NSError **outError);
- BOOL RLMIsObjectValidForProperty(id obj, RLMProperty *prop);
- void RLMValidateValueForProperty(id obj, RLMObjectSchema *objectSchema,
- RLMProperty *prop, bool validateObjects=false);
- id RLMValidateValue(id value, RLMPropertyType type, bool optional, bool collection,
- NSString *objectClassName);
- void RLMThrowTypeError(id obj, RLMObjectSchema *objectSchema, RLMProperty *prop);
- NSDictionary *RLMDefaultValuesForObjectSchema(RLMObjectSchema *objectSchema);
- BOOL RLMIsDebuggerAttached();
- BOOL RLMIsRunningInPlayground();
- static inline BOOL RLMIsKindOfClass(Class class1, Class class2) {
- while (class1) {
- if (class1 == class2) return YES;
- class1 = class_getSuperclass(class1);
- }
- return NO;
- }
- template<typename T>
- static inline T *RLMDynamicCast(__unsafe_unretained id obj) {
- if ([obj isKindOfClass:[T class]]) {
- return obj;
- }
- return nil;
- }
- static inline id RLMCoerceToNil(__unsafe_unretained id obj) {
- if (static_cast<id>(obj) == NSNull.null) {
- return nil;
- }
- else if (__unsafe_unretained auto optional = RLMDynamicCast<RLMSwiftValueStorage>(obj)) {
- return RLMCoerceToNil(RLMGetSwiftValueStorage(optional));
- }
- return obj;
- }
- template<typename T>
- static inline T RLMCoerceToNil(__unsafe_unretained T obj) {
- return RLMCoerceToNil(static_cast<id>(obj));
- }
- id<NSFastEnumeration> RLMAsFastEnumeration(id obj);
- id RLMBridgeSwiftValue(id obj);
- bool RLMIsSwiftObjectClass(Class cls);
- static inline NSString * RLMStringDataToNSString(realm::StringData stringData) {
- static_assert(sizeof(NSUInteger) >= sizeof(size_t),
- "Need runtime overflow check for size_t to NSUInteger conversion");
- if (stringData.is_null()) {
- return nil;
- }
- else {
- return [[NSString alloc] initWithBytes:stringData.data()
- length:stringData.size()
- encoding:NSUTF8StringEncoding];
- }
- }
- static inline realm::StringData RLMStringDataWithNSString(__unsafe_unretained NSString *const string) {
- static_assert(sizeof(size_t) >= sizeof(NSUInteger),
- "Need runtime overflow check for NSUInteger to size_t conversion");
- return realm::StringData(string.UTF8String,
- [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
- }
- static inline NSData *RLMBinaryDataToNSData(realm::BinaryData binaryData) {
- return binaryData ? [NSData dataWithBytes:binaryData.data() length:binaryData.size()] : nil;
- }
- static inline realm::BinaryData RLMBinaryDataForNSData(__unsafe_unretained NSData *const data) {
-
-
-
- auto bytes = static_cast<const char *>(data.bytes) ?: static_cast<char *>((__bridge void *)data);
- return realm::BinaryData(bytes, data.length);
- }
- static inline NSDate *RLMTimestampToNSDate(realm::Timestamp ts) NS_RETURNS_RETAINED {
- if (ts.is_null())
- return nil;
- auto timeInterval = ts.get_seconds() - NSTimeIntervalSince1970 + ts.get_nanoseconds() / 1'000'000'000.0;
- return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:timeInterval];
- }
- static inline realm::Timestamp RLMTimestampForNSDate(__unsafe_unretained NSDate *const date) {
- if (!date)
- return {};
- auto timeInterval = date.timeIntervalSinceReferenceDate;
- if (isnan(timeInterval))
- return {0, 0};
-
- if (timeInterval >= std::numeric_limits<int64_t>::max() - NSTimeIntervalSince1970)
- return {std::numeric_limits<int64_t>::max(), 1'000'000'000 - 1};
- if (timeInterval - NSTimeIntervalSince1970 < std::numeric_limits<int64_t>::min())
- return {std::numeric_limits<int64_t>::min(), -1'000'000'000 + 1};
- auto seconds = static_cast<int64_t>(timeInterval);
- auto nanoseconds = static_cast<int32_t>((timeInterval - seconds) * 1'000'000'000.0);
- seconds += static_cast<int64_t>(NSTimeIntervalSince1970);
-
- if (nanoseconds < 0 && seconds > 0) {
- nanoseconds += 1'000'000'000;
- --seconds;
- }
- return {seconds, nanoseconds};
- }
- static inline NSUInteger RLMConvertNotFound(size_t index) {
- return index == realm::not_found ? NSNotFound : index;
- }
- static inline void RLMNSStringToStdString(std::string &out, NSString *in) {
- if (!in)
- return;
-
- out.resize([in maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
- if (out.empty()) {
- return;
- }
- NSUInteger size = out.size();
- [in getBytes:&out[0]
- maxLength:size
- usedLength:&size
- encoding:NSUTF8StringEncoding
- options:0 range:{0, in.length} remainingRange:nullptr];
- out.resize(size);
- }
- realm::Mixed RLMObjcToMixed(__unsafe_unretained id value,
- __unsafe_unretained RLMRealm *realm=nil,
- realm::CreatePolicy createPolicy={});
- id RLMMixedToObjc(realm::Mixed const& value,
- __unsafe_unretained RLMRealm *realm=nil,
- RLMClassInfo *classInfo=nullptr);
- realm::Decimal128 RLMObjcToDecimal128(id value);
- realm::UUID RLMObjcToUUID(__unsafe_unretained id const value);
- NSString *RLMDefaultDirectoryForBundleIdentifier(NSString *bundleIdentifier);
- NSDateFormatter *RLMISO8601Formatter();
- template<typename Fn>
- static auto RLMTranslateError(Fn&& fn) {
- try {
- return fn();
- }
- catch (std::exception const& e) {
- @throw RLMException(e);
- }
- }
- static inline bool numberIsInteger(__unsafe_unretained NSNumber *const obj) {
- char data_type = [obj objCType][0];
- return data_type == *@encode(bool) ||
- data_type == *@encode(char) ||
- data_type == *@encode(short) ||
- data_type == *@encode(int) ||
- data_type == *@encode(long) ||
- data_type == *@encode(long long) ||
- data_type == *@encode(unsigned short) ||
- data_type == *@encode(unsigned int) ||
- data_type == *@encode(unsigned long) ||
- data_type == *@encode(unsigned long long);
- }
- static inline bool numberIsBool(__unsafe_unretained NSNumber *const obj) {
-
-
- if ([obj objCType][0] == 'c') {
- return true;
- }
- if (numberIsInteger(obj)) {
- int value = [obj intValue];
- return value == 0 || value == 1;
- }
- return false;
- }
- static inline bool numberIsFloat(__unsafe_unretained NSNumber *const obj) {
- char data_type = [obj objCType][0];
- return data_type == *@encode(float) ||
- data_type == *@encode(short) ||
- data_type == *@encode(int) ||
- data_type == *@encode(long) ||
- data_type == *@encode(long long) ||
- data_type == *@encode(unsigned short) ||
- data_type == *@encode(unsigned int) ||
- data_type == *@encode(unsigned long) ||
- data_type == *@encode(unsigned long long) ||
-
- (data_type == *@encode(double) && (ABS([obj doubleValue]) <= FLT_MAX || isnan([obj doubleValue])));
- }
- static inline bool numberIsDouble(__unsafe_unretained NSNumber *const obj) {
- char data_type = [obj objCType][0];
- return data_type == *@encode(double) ||
- data_type == *@encode(float) ||
- data_type == *@encode(short) ||
- data_type == *@encode(int) ||
- data_type == *@encode(long) ||
- data_type == *@encode(long long) ||
- data_type == *@encode(unsigned short) ||
- data_type == *@encode(unsigned int) ||
- data_type == *@encode(unsigned long) ||
- data_type == *@encode(unsigned long long);
- }
|