/*
 * Renjin : JVM-based interpreter for the R language for the statistical analysis
 * Copyright Â© 2010-2019 BeDataDriven Groep B.V. and contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, a copy is available at
 * https://www.gnu.org/licenses/gpl-2.0.txt
 */

package org.renjin.gcc.runtime;

import java.lang.reflect.Field;

// GENERATED BY generate.groovy
// DO NOT MODIFY DIRECTLY


/**
 * Pointer to a static field of type short
 */
public class ShortFieldPtr extends AbstractPtr {

  private final Field field;

  public static Ptr addressOf(Class declaringClass, String fieldName) {
    try {
      return new ShortFieldPtr(declaringClass.getField(fieldName));
    } catch(Exception e) {
      throw new Error(e);
    }
  }

  public ShortFieldPtr(Field field) {
    this.field = field;
  }

  @Override
  public Object getArray() {
    return field;
  }

  @Override
  public final int getOffsetInBytes() {
    return 0;
  }

  @Override
  public Ptr realloc(int newSizeInBytes) {
    throw new UnsupportedOperationException("TODO");
  }

  @Override
  public Ptr pointerPlus(int bytes) {
    if(bytes == 0) {
      return this;
    } else {
      return new OffsetPtr(this, bytes);
    }
  }

  @Override
  public short getShort() {
    try {
      return field.getShort(null);
    } catch (IllegalAccessException e) {
      // Should not be reachable: we compile global variables
      // to public static members
      throw new Error(e);
    }
  }

  @Override
  public void setShort(short value) {
    try {
      field.setShort(null, value);
    } catch (IllegalAccessException e) {
      // Should not be reachable: we compile global variables
      // to public static members
      throw new Error(e);
    }
  }

  @Override
  public short getShort(int offset) {
    if(offset == 0) {
      return getShort();
    }
    throw new IndexOutOfBoundsException();
  }


  @Override
  public void setShort(int offset, short intValue) {
    if(offset == 0) {
      setShort(intValue);
    } else {
      throw new IndexOutOfBoundsException();
    }
  }


  @Override
  public short getAlignedShort(int index) {
    if(index == 0) {
      return getShort();
    } else {
      throw new IndexOutOfBoundsException();
    }
  }

  @Override
  public void setAlignedShort(int index, short value) {
    if(index == 0) {
      setShort(value);
    } else {
      throw new IndexOutOfBoundsException();
    }
  }

  @Override
  public byte getByte(int offset) {
    return getByteViaShort(offset);
  }

  @Override
  public void setByte(int offset, byte value) {
    setByteViaShort(offset, value);
  }


  @Override
  public int toInt() {
    return 0;
  }

  @Override
  public boolean isNull() {
    return false;
  }
}
