My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
states.cpp
Go to the documentation of this file.
1 /*
2  * states.cpp - save-state variable class implementation
3  *
4  * Copyright (C) 2004 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id: states.cpp 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "states.h"
34 
35 // Some definitions for the save-state variables.
36 #define STATE_SHIFT 3
37 #define STATE_NUM 8
38 #define STATE_MASK 7
39 
40 // Constructor creates an unnamed instance of the states class.
41 template <class state_type_t>
43  nstates = 0;
44  currentstate = 0;
45  stateval = NULL;
46 }
47 
48 /* The copy constructor creates a new instance based on the given
49  states object. */
50 template <class state_type_t>
52  nstates = c.nstates;
53  currentstate = c.currentstate;
54 
55  // copy state variables if necessary
56  if (nstates && c.stateval) {
57  int size = nstates * sizeof (state_type_t) * STATE_NUM;
58  stateval = (state_type_t *) malloc (size);
59  memcpy (stateval, c.stateval, size);
60  }
61  else stateval = NULL;
62 }
63 
64 // Destructor deletes a states object.
65 template <class state_type_t>
67  if (stateval) free (stateval);
68 }
69 
70 /* The function allocates and initializes memory for the save-state
71  variables. */
72 template <class state_type_t>
74  if (stateval != NULL) free (stateval);
75  if (nstates) {
76  stateval = (state_type_t *)
77  calloc (nstates, sizeof (state_type_t) * STATE_NUM);
78  }
79  currentstate = 0;
80 }
81 
82 // Clears the save-state variables.
83 template <class state_type_t>
85  if (nstates && stateval)
86  memset (stateval, 0, nstates * sizeof (state_type_t) * STATE_NUM);
87  currentstate = 0;
88 }
89 
90 /* The function returns a save-state variable at the given position.
91  Higher positions mean earlier states. By default the function
92  returns the current state of the save-state variable. */
93 template <class state_type_t>
94 state_type_t states<state_type_t>::getState (int state, int n) {
95  int i = (n + currentstate) & STATE_MASK;
96  return stateval[(state << STATE_SHIFT) + i];
97 }
98 
99 /* This function applies the given value to a save-state variable.
100  Higher positions mean earlier states. By default the function sets
101  the current state of the save-state variable. */
102 template <class state_type_t>
103 void states<state_type_t>::setState (int state, state_type_t val, int n) {
104  int i = (n + currentstate) & STATE_MASK;
105  stateval[(state << STATE_SHIFT) + i] = val;
106 }
107 
108 // Shifts one state forward.
109 template <class state_type_t>
111  if (--currentstate < 0) currentstate = STATE_NUM - 1;
112 }
113 
114 // Shifts one state backward.
115 template <class state_type_t>
117  currentstate = (currentstate + 1) & STATE_MASK;
118 }
119 
120 /* This function applies the given value to a save-state variable through
121  all history values. */
122 template <class state_type_t>
123 void states<state_type_t>::fillState (int state, state_type_t val) {
124  state_type_t * p = &stateval[state << STATE_SHIFT];
125  for (int i = 0; i < STATE_NUM; i++) *p++ = val;
126 }
127 
128 /* This function stores the values of the given state into the given
129  pointer location. */
130 template <class state_type_t>
131 void states<state_type_t>::saveState (int state, state_type_t * values) {
132  for (int i = 0; i < STATE_NUM; i++) values[i] = getState (state, i);
133 }