User Tools

Site Tools


dev:atomic_parameters

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
dev:atomic_parameters [2014/09/09 18:42] – created tsmall1dev:atomic_parameters [2014/09/21 17:54] (current) – [Make Getter and Setter] tsmall1
Line 5: Line 5:
 ===== Declare Compilation Constants ===== ===== Declare Compilation Constants =====
  
-First, we need to describe in the Element how big of a field we want to use, and where to place it inside the atom. We need to use the bits on the right hand part of the atom first, since a number of ones on the left hand side are already used. For instance, let's declare a small field storing random byte:+First, we need to describe in the Element how big of a field we want to use, and where to place it inside the atom. We need to use the bits on the right hand part of the atom first, since a number of ones on the left hand side are already used. For instance, let's declare a small field which can hold a byte:
  
 <code> <code>
Line 12: Line 12:
     BITS = P::BITS_PER_ATOM,     BITS = P::BITS_PER_ATOM,
          
-    RANDOM_BITS_LENGTH = 8, +    BYTE_BITS_LENGTH = 8, 
-    RANDOM_BITS_POSITION = BITS - RANDOM_BITS_LENGTH - 1 +    BYTE_BITS_POSITION = BITS - BYTE_BITS_LENGTH - 1 
-}+};
 </code> </code>
  
Line 20: Line 20:
  
 <code> <code>
-typedef BitField<BitVector<BITS>, RANDOM_BITS_LENGTHRANDOM_BITS_POSITIONAFRandomBits;+typedef BitField<BitVector<BITS>, BYTE_BITS_LENGTHBYTE_BITS_POSITIONAFByteBits;
 </code> </code>
  
 +make sure that BitField is included in the file by adding:
 +
 +<code>
 +#include "BitField.h"
 +</code>
 +
 +to the top of the file.
 +
 +===== Make Getter and Setter =====
 +
 +We now need methods which access these bits. In the public section of your elemenet, place:
 +
 +<code>
 +u32 GetByteBits(const T& us) const
 +{
 +    return AFByteBits::Read(this->GetBits(us));
 +}
 +
 +void SetByteBits(T& us, u32 bits) const
 +{
 +    AFByteBits::Write(this->GetBits(us), bits & 0xff);
 +}
 +</code>
 +
 +Now we are able to Get and Set these bits.
 +
 +===== Set Default Value =====
 +
 +We should also set some sort of default value for these bits. This is done by overriding the **GetDefaultAtom()** method. Place in the public section of your element:
 +
 +<code>
 +
 +static const u32 TYPE()
 +{
 +    return THE_INSTANCE.GetType();
 +}
 +
 +virtual const T& GetDefaultAtom() const
 +{
 +    static T defaultAtom(TYPE(), 0, 0, 0);
 +    this->SetByteBits(defaultAtom, 0 /* Or other default value */);
 +    
 +    return defaultAtom;
 +}
 +</code>
  
 +In this example, the default atom has its bitfield set to 0. We can use this call during the behavior method as well to read or write bits of other atoms.
dev/atomic_parameters.1410288156.txt.gz · Last modified: 2014/09/09 18:42 by tsmall1