/*
 * Renjin : JVM-based interpreter for the R language for the statistical analysis
 * Copyright Â© 2010- ${.now?string('yyyy')} 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
 *
 */

<#assign getter="get${type?capitalize}">
<#assign setter="set${type?capitalize}">

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 ${type}
 */
public class ${className} extends AbstractPtr {

  private final Field field;

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

  public ${className}(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 ${type} ${getter}() {
    try {
      return field.${getter}(null);
    } catch (IllegalAccessException e) {
      // Should not be reachable: we compile global variables
      // to public static members
      throw new Error(e);
    }
  }

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

  @Override
  public ${type} ${getter}(int offset) {
    if(offset == 0) {
      return ${getter}();
    }
    throw new IndexOutOfBoundsException();
  }


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

  <#if type != 'byte' >

  @Override
  public ${type} getAligned${type?capitalize}(int index) {
    if(index == 0) {
      return ${getter}();
    } else {
      throw new IndexOutOfBoundsException();
    }
  }

  @Override
  public void setAligned${type?capitalize}(int index, ${type} value) {
    if(index == 0) {
      ${setter}(value);
    } else {
      throw new IndexOutOfBoundsException();
    }
  }

  @Override
  public byte getByte(int offset) {
    return getByteVia${type?capitalize}(offset);
  }

  @Override
  public void setByte(int offset, byte value) {
    setByteVia${type?capitalize}(offset, value);
  }

  </#if>

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

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

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof IntFieldPtr)) {
      return false;
    }
    IntFieldPtr that = (IntFieldPtr) o;
    return this.field.equals(that.field);
  }

  @Override
  public int hashCode() {
    return field.hashCode();
  }
}
