MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Atom.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Atom.h Instance of MFM Element
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef ATOM_H
29 #define ATOM_H
30 
31 #include "itype.h"
32 #include "VD.h"
33 #include "BitVector.h"
34 #include "Random.h"
35 #include "Util.h" /* For REQUIRE */
36 #include "Logger.h"
37 #include "ByteSource.h"
39 
40 namespace MFM
41 {
42 
43  template <class CC> class Element; // Forward declaration
44  template <class CC> class AtomSerializer; // Forward declaration
45  template <class BV, VD::Type, u32, u32> class BitField; // Forward declaration
46 
70  template <class CC>
71  class Atom
72  {
73  // Extract short names for parameter types
77  typedef typename CC::ATOM_TYPE T;
78 
82  typedef typename CC::PARAM_CONFIG P;
83 
87  enum { BPA = P::BITS_PER_ATOM };
88 
92  enum { R = P::EVENT_WINDOW_RADIUS };
93 
94  protected:
100 
101  friend class VD; // Value Descriptors can mess with our bits
102  friend class Element<CC>; // Let Element mess with our bits
103  friend class AtomSerializer<CC>; // Ditto AtomSerializer
104  template <class BV, VD::Type, u32, u32> friend class BitField; // Ditto BitField (all instances)
105 
106  public:
107 
108  Atom()
109  {
110  // Create a compilation error if sizeof(Atom)!=sizeof(m_bits)
111  COMPILATION_REQUIREMENT<sizeof(Atom)==sizeof(m_bits)>();
112  }
113 
123  static bool IsType(const T& atom, u32 type)
124  {
125  return atom.GetType()==type;
126  }
127 
137  static bool IsSameType(const T& atom, const T& other)
138  {
139  return atom.GetType()==other.GetType();
140  }
141 
151  bool IsSane() const
152  {
153  return static_cast<const T*>(this)->IsSaneImpl();
154  }
155 
156 
167  {
168  return static_cast<T*>(this)->HasBeenRepairedImpl();
169  }
170 
178  u32 GetType() const
179  {
180  return static_cast<const T*>(this)->GetTypeImpl();
181  }
182 
188  void Print(ByteSink & ostream) const
189  {
190  static_cast<const T*>(this)->PrintImpl(ostream);
191  }
192 
199  void WriteStateBits(ByteSink& ostream) const
200  {
201  static_cast<const T*>(this)->WriteStateBitsImpl(ostream);
202  }
203 
209  void ReadStateBits(const char* hexStr)
210  {
211  static_cast<T*>(this)->ReadStateBitsImpl(hexStr);
212  }
213 
214 
221  void ReadStateBits(const BitVector<BPA> & bv)
222  {
223  static_cast<T*>(this)->ReadStateBitsImpl(bv);
224  }
225 
226 
232  void WriteBits(ByteSink& ostream) const
233  {
234  m_bits.Print(ostream);
235  }
236 
243  void ReadBits(const char* hexStr)
244  {
245  u32 len = strlen(hexStr);
246  u32 bitLen = len * 4;
247 
248  if(bitLen > BPA)
249  {
250  FAIL(ILLEGAL_ARGUMENT);
251  }
252 
253  m_bits.Clear();
254 
255  u32 bitIdx = 0;
256  char hexVal;
257 
258  for(s32 i = len - 1; i >= 0; i--)
259  {
260  hexVal = hexStr[i];
261  if(hexVal >= '0' && hexVal <= '9')
262  {
263  hexVal -= '0';
264  }
265  else if(hexVal >= 'a' && hexVal >= 'f')
266  {
267  hexVal = hexVal - 'a' + 10;
268  }
269  else if(hexVal >= 'A' && hexVal <= 'F')
270  {
271  hexVal = hexVal - 'A' + 10;
272  }
273  else
274  {
275  FAIL(ILLEGAL_ARGUMENT);
276  }
277 
278  for(s32 j = 3; j >= 0; j--)
279  {
280  bitIdx = i * 4 + j;
281  m_bits.WriteBit(bitIdx, !!(hexVal & (1 << j)));
282  }
283  }
284  }
285 
295  void XRay(Random& rand, u32 bitOdds)
296  {
297  for(u32 i = 0; i < BPA; i++)
298  {
299  if(rand.OneIn(bitOdds))
300  {
301  m_bits.ToggleBit(i);
302  }
303  }
304  }
305 
306  bool operator==(const Atom & rhs) const
307  {
308  return m_bits == rhs.m_bits;
309  }
310 
311  bool operator!=(const Atom & rhs) const
312  {
313  return !(*this == rhs);
314  }
315  };
316 
317 
318 } /* namespace MFM */
319 
320 #endif /*ATOM_H*/
void WriteStateBits(ByteSink &ostream) const
Definition: Atom.h:199
u32 GetType() const
Definition: Atom.h:178
void Clear()
Definition: BitVector.tcc:26
Definition: Random.h:45
void ReadStateBits(const BitVector< BPA > &bv)
Definition: Atom.h:221
Definition: VD.h:44
Definition: Atom.h:71
void WriteBit(u32 idx, bool bit)
Definition: BitVector.tcc:41
void WriteBits(ByteSink &ostream) const
Definition: Atom.h:232
void Print(ByteSink &ostream) const
Definition: BitVector.tcc:202
bool OneIn(u32 odds)
Definition: Random.h:96
bool IsSane() const
Definition: Atom.h:151
bool ToggleBit(const u32 idx)
Definition: BitVector.tcc:54
Definition: Atom.h:45
void ReadStateBits(const char *hexStr)
Definition: Atom.h:209
bool HasBeenRepaired()
Definition: Atom.h:166
void XRay(Random &rand, u32 bitOdds)
Definition: Atom.h:295
Definition: ByteSink.h:47
static bool IsSameType(const T &atom, const T &other)
Definition: Atom.h:137
void Print(ByteSink &ostream) const
Definition: Atom.h:188
Definition: P1Atom.h:50
Definition: Atom.h:44
void ReadBits(const char *hexStr)
Definition: Atom.h:243
Definition: Atom.h:43
BitVector< BPA > m_bits
Definition: Atom.h:99
static bool IsType(const T &atom, u32 type)
Definition: Atom.h:123