Add support for mapping size_t #191

Closed
tstgruby opened this Issue Feb 7, 2013 · 8 comments

Projects

None yet

3 participants

@tstgruby
tstgruby commented Feb 7, 2013

Hi.

May be I'm missing something, but I could not find anything in the docs on how to properly map a native size_t with JNA.

JNAerator suggest me to use import com.ochafik.lang.jnaerator.runtime.NativeSize;

Do I really have to?

@twall
Contributor
twall commented Feb 7, 2013
public static class size_t extends IntegerType {                                           
    public size_t() { this(0); }                                                           
    public size_t(long value) { super(Native.SIZE_T_SIZE, value); }                       
} 

offset_t is typically the same size.

On Feb 7, 2013, at 5:51 AM, tstgruby wrote:

Hi.

May be I'm missing something, but I could not find anything in the docs on how to properly map a native size_t with JNA.

JNAerator suggest me to use import com.ochafik.lang.jnaerator.runtime.NativeSize;

Do I really have to?


Reply to this email directly or view it on GitHub.

@tstgruby
tstgruby commented Feb 7, 2013

Thanks a lot, that's quite easy :-)

Wouldn't it make sense to integrate this into JNA?

@twall
Contributor
twall commented Feb 8, 2013

I suppose so, but some folks would want size_t and others would want SizeT or Size_Type :)

@twall twall closed this Feb 8, 2013
@JohnLCaron

How do you do size_t * ?

ie

SizeTByReference ??

@JohnLCaron

size_t is unsigned, so im thinking

        public size_t(long value) { super(Native.SIZE_T_SIZE, value, true); }

is better?

@JohnLCaron

Heres an attempt at size_t * , where I am using SizeT instead of size_t from above:

public class SizeTByReference extends ByReference {
  public SizeTByReference() {
    this(new SizeT());
  }

  public SizeTByReference(SizeT value) {
    super(Native.SIZE_T_SIZE);
    setValue(value);
  }

  public void setValue(SizeT value) {
    Pointer p = getPointer();
    if (Native.SIZE_T_SIZE == 8) {
      p.setLong(0, value.longValue());
    } else {
      p.setInt(0, value.intValue());
    }
  }

  public SizeT getValue() {
    Pointer p = getPointer();
    return new SizeT(Native.SIZE_T_SIZE == 8 ? p.getLong(0) : p.getInt(0));
  }
}
@JohnLCaron

For completeness, heres SizeT class.
I will do a pull request if interested

public class SizeT extends IntegerType {
  public SizeT() { this(0); }
  public SizeT(long value) { super(Native.SIZE_T_SIZE, value, true); }
}
@twall
Contributor
twall commented Jun 1, 2014

This (and the by reference version) look fine, although I prefer not to change the representation of the native type if not strictly necessary (size_t vs SizeT). While ugly by Java standards, “WORD” and “ULONG_PTR” at the least are unambiguous about what they represent.

So “size_t” is appropriate for most platforms, “SIZE_T” for windows. Someone reading “SizeT” could infer that it meant either “size_t” or “SIZE_T”, but there would be some doubt.

Ideally we’d just have a generic Pointer<T> which could automagically handle all the ByReference semantics (anyone want to write up a POC for that?).

On May 31, 2014, at 2:05 PM, John Caron notifications@github.com wrote:

For completeness, will do a pull request if interested

public class SizeT extends IntegerType {

public SizeT() { this(0); }

public SizeT(long value) { super(Native.SIZE_T_SIZE, value, true); }
}

Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment