001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj.simulation;
006
007/** Manages simulation callbacks; each object is associated with a callback. */
008public class CallbackStore implements AutoCloseable {
009  /** <b>Note: This interface is for simulation classes only. It should not be used by teams!</b> */
010  interface CancelCallbackFunc {
011    void cancel(int index, int uid);
012  }
013
014  /** <b>Note: This interface is for simulation classes only. It should not be used by teams!</b> */
015  interface CancelCallbackChannelFunc {
016    void cancel(int index, int channel, int uid);
017  }
018
019  /** <b>Note: This interface is for simulation classes only. It should not be used by teams!</b> */
020  interface CancelCallbackNoIndexFunc {
021    void cancel(int uid);
022  }
023
024  /**
025   * <b>Note: This constructor is for simulation classes only. It should not be called by teams!</b>
026   *
027   * @param index TODO
028   * @param uid TODO
029   * @param ccf TODO
030   */
031  public CallbackStore(int index, int uid, CancelCallbackFunc ccf) {
032    this.m_cancelType = kNormalCancel;
033    this.m_index = index;
034    this.m_uid = uid;
035    this.m_cancelCallback = ccf;
036  }
037
038  /**
039   * <b>Note: This constructor is for simulation classes only. It should not be called by teams!</b>
040   *
041   * @param index TODO
042   * @param channel TODO
043   * @param uid TODO
044   * @param ccf TODO
045   */
046  public CallbackStore(int index, int channel, int uid, CancelCallbackChannelFunc ccf) {
047    this.m_cancelType = kChannelCancel;
048    this.m_index = index;
049    this.m_uid = uid;
050    this.m_channel = channel;
051    this.m_cancelCallbackChannel = ccf;
052  }
053
054  /**
055   * <b>Note: This constructor is for simulation classes only. It should not be called by teams!</b>
056   *
057   * @param uid TODO
058   * @param ccf TODO
059   */
060  public CallbackStore(int uid, CancelCallbackNoIndexFunc ccf) {
061    this.m_cancelType = kNoIndexCancel;
062    this.m_uid = uid;
063    this.m_cancelCallbackNoIndex = ccf;
064  }
065
066  private int m_index;
067  private int m_channel;
068  private final int m_uid;
069  private CancelCallbackFunc m_cancelCallback;
070  private CancelCallbackChannelFunc m_cancelCallbackChannel;
071  private CancelCallbackNoIndexFunc m_cancelCallbackNoIndex;
072  private static final int kNormalCancel = 0;
073  private static final int kChannelCancel = 1;
074  private static final int kNoIndexCancel = 2;
075  private int m_cancelType;
076
077  /** Cancel the callback associated with this object. */
078  @Override
079  public void close() {
080    switch (m_cancelType) {
081      case kNormalCancel:
082        m_cancelCallback.cancel(m_index, m_uid);
083        break;
084      case kChannelCancel:
085        m_cancelCallbackChannel.cancel(m_index, m_channel, m_uid);
086        break;
087      case kNoIndexCancel:
088        m_cancelCallbackNoIndex.cancel(m_uid);
089        break;
090      default:
091        assert false;
092        break;
093    }
094    m_cancelType = -1;
095  }
096
097  @SuppressWarnings({"NoFinalizer", "deprecation"})
098  @Override
099  protected void finalize() throws Throwable {
100    try {
101      if (m_cancelType >= 0) {
102        close(); // close open files
103      }
104    } finally {
105      super.finalize();
106    }
107  }
108}