Repository: realm/realm-swift
File: src/tightdb/objc/util_noinst.hpp (C++)

Code before:
            return [NSNumber numberWithLongLong:m.get_int()];
        case tightdb::type_Float:
            return [NSNumber numberWithFloat:m.get_float()];
        case tightdb::type_Double:
            return [NSNumber numberWithDouble:m.get_double()];
        case tightdb::type_DateTime:
            return [NSDate dateWithTimeIntervalSince1970:m.get_datetime().get_datetime()];
        case tightdb::type_String:
            return to_objc_string(m.get_string());
        case tightdb::type_Binary: {
            tightdb::BinaryData bd = m.get_binary();
            return [NSData dataWithBytes:bd.data() length:bd.size()];
        }
        case tightdb::type_Mixed:
            TIGHTDB_ASSERT(false); /* we should never get here */
        case tightdb::type_Table:
            TIGHTDB_ASSERT(false);
    }
    return nil;
}


// A few nice helpers
inline bool nsnumber_is_like_bool(NSObject *obj)
{
    const char* data_type = [(NSNumber *)obj objCType];
    /* @encode(BOOL) is 'B' on iOS 64 and 'c'
     objcType is always 'c'. Therefore compare to "c".
     */
    return data_type[0] == 'c';
}

inline bool nsnumber_is_like_integer(NSObject *obj)
{
    const char* data_type = [(NSNumber *)obj objCType];
    return (strcmp(data_type, @encode(int)) == 0 ||
            strcmp(data_type, @encode(long)) ==  0 ||
            strcmp(data_type, @encode(long long)) == 0 ||
            strcmp(data_type, @encode(unsigned int)) == 0 ||
            strcmp(data_type, @encode(unsigned long)) == 0 ||
            strcmp(data_type, @encode(unsigned long long)) == 0);
}

inline bool nsnumber_is_like_float(NSObject *obj)
{
    const char* data_type = [(NSNumber *)obj objCType];
    return (strcmp(data_type, @encode(float)) == 0 ||
            strcmp(data_type, @encode(int)) == 0 ||
            strcmp(data_type, @encode(long)) ==  0 ||
            strcmp(data_type, @encode(long long)) == 0 ||
            strcmp(data_type, @encode(unsigned int)) == 0 ||

Review comment (suggestion):
I believe that I have done as outlined by @pauldardeau. The core (C++) returns `tightdb::not_found` (= `size_t(-1)`) and I transform that to `NSNotFound` in Objective C (and something different in Python).

The `was_not_found` is a function to do the transform so I didn't have to write the `if` so often.


Code after:
            return [NSNumber numberWithLongLong:m.get_int()];
        case tightdb::type_Float:
            return [NSNumber numberWithFloat:m.get_float()];
        case tightdb::type_Double:
            return [NSNumber numberWithDouble:m.get_double()];
        case tightdb::type_DateTime:
            return [NSDate dateWithTimeIntervalSince1970:m.get_datetime().get_datetime()];
        case tightdb::type_String:
            return to_objc_string(m.get_string());
        case tightdb::type_Binary: {
            tightdb::BinaryData bd = m.get_binary();
            return [NSData dataWithBytes:bd.data() length:bd.size()];
        }
        case tightdb::type_Mixed:
            TIGHTDB_ASSERT(false); /* we should never get here */
        case tightdb::type_Table:
            TIGHTDB_ASSERT(false);
    }
    return nil;
}


inline NSUInteger was_not_found(size_t n)
{
    if (n == tightdb::not_found)
        return (NSUInteger)NSNotFound;
    return (NSUInteger)n;
}

inline bool nsnumber_is_like_bool(NSObject *obj)
{
    const char* data_type = [(NSNumber *)obj objCType];
    /* @encode(BOOL) is 'B' on iOS 64 and 'c'
     objcType is always 'c'. Therefore compare to "c".
     */
    return data_type[0] == 'c';
}

inline bool nsnumber_is_like_integer(NSObject *obj)
{
    const char* data_type = [(NSNumber *)obj objCType];
    return (strcmp(data_type, @encode(int)) == 0 ||
            strcmp(data_type, @encode(long)) ==  0 ||
            strcmp(data_type, @encode(long long)) == 0 ||
            strcmp(data_type, @encode(unsigned int)) == 0 ||
            strcmp(data_type, @encode(unsigned long)) == 0 ||
            strcmp(data_type, @encode(unsigned long long)) == 0);
}

inline bool nsnumber_is_like_float(NSObject *obj)
{