/* * states.cpp - save-state variable class implementation * * Copyright (C) 2004 Stefan Jahn * * This 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, or (at your option) * any later version. * * This software 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 package; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, * Boston, MA 02110-1301, USA. * * $Id: states.cpp,v 1.3 2005/06/02 18:17:51 raimi Exp $ * */ #if HAVE_CONFIG_H # include #endif #include #include #include #include "states.h" // Some definitions for the save-state variables. #define STATE_SHIFT 3 #define STATE_NUM 8 #define STATE_MASK 7 // Constructor creates an unnamed instance of the states class. template states::states () { nstates = 0; currentstate = 0; stateval = NULL; } /* The copy constructor creates a new instance based on the given states object. */ template states::states (const states & c) { nstates = c.nstates; currentstate = c.currentstate; // copy state variables if necessary if (nstates && c.stateval) { int size = nstates * sizeof (state_type_t) * STATE_NUM; stateval = (state_type_t *) malloc (size); memcpy (stateval, c.stateval, size); } else stateval = NULL; } // Destructor deletes a states object. template states::~states () { if (stateval) free (stateval); } /* The function allocates and initializes memory for the save-state variables. */ template void states::initStates (void) { if (stateval != NULL) free (stateval); if (nstates) { stateval = (state_type_t *) calloc (nstates, sizeof (state_type_t) * STATE_NUM); } currentstate = 0; } // Clears the save-state variables. template void states::clearStates (void) { if (nstates && stateval) memset (stateval, 0, nstates * sizeof (state_type_t) * STATE_NUM); currentstate = 0; } /* The function returns a save-state variable at the given position. Higher positions mean earlier states. By default the function returns the current state of the save-state variable. */ template state_type_t states::getState (int state, int n) { int i = (n + currentstate) & STATE_MASK; return stateval[(state << STATE_SHIFT) + i]; } /* This function applies the given value to a save-state variable. Higher positions mean earlier states. By default the function sets the current state of the save-state variable. */ template void states::setState (int state, state_type_t val, int n) { int i = (n + currentstate) & STATE_MASK; stateval[(state << STATE_SHIFT) + i] = val; } // Shifts one state forward. template void states::nextState (void) { if (--currentstate < 0) currentstate = STATE_NUM - 1; } // Shifts one state backward. template void states::prevState (void) { currentstate = (currentstate + 1) & STATE_MASK; } /* This function applies the given value to a save-state variable through all history values. */ template void states::fillState (int state, state_type_t val) { state_type_t * p = &stateval[state << STATE_SHIFT]; for (int i = 0; i < STATE_NUM; i++) *p++ = val; } /* This function stores the values of the given state into the given pointer location. */ template void states::saveState (int state, state_type_t * values) { for (int i = 0; i < STATE_NUM; i++) values[i] = getState (state, i); }