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 (refactor):
My suggestion of using NSNotFound was meant to only apply to the ObjC language binding.  NSNotFound is sensible for ObjC, but not for any other language.  I think that the original -1 value still makes sense for C++, Java, Python, etc.

Pseudo-code of ObjC binding for what I was intending:

// don’t recall whether it should be NSInteger or NSUInteger
- (NSUInteger)findFirstRow {
  size_t idx = find();
  if (idx == -1) {
      return NSNotFound;
  } else {
      return idx;
  }
  }

On Apr 7, 2014, at 4:45 AM, bmunkholm notifications@github.com wrote:

> In src/tightdb/objc/util_noinst.hpp:
> 
> > @@ -109,7 +109,13 @@ inline NSObject\* to_objc_object(tightdb::Mixed m)
> >  }
> > 
> > -// A few nice helpers
> > +inline NSUInteger was_not_found(size_t n)
> > +{
> > -    if (n == tightdb::not_found)
> > -        return (NSUInteger)NSNotFound;
> >   remove cast
> 
> —
> Reply to this email directly or view it on GitHub.


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)
{