EnigmaMachineCore 0.1.0
A modular Enigma Machine simulation in C++20
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Rotor Class Reference

Class representing a rotor in the Enigma machine. This class inherits from the Transformer class and implements the functionality for transforming positions based on the rotor's transformation lookup table (LUT). It also handles the rotor's rotation and notch position. More...

#include <Rotor.hpp>

+ Inheritance diagram for Rotor:
+ Collaboration diagram for Rotor:

Public Member Functions

 Rotor (const RotorConfig &config)
 Constructor for the Rotor class. Initializes the rotor with a configuration.
 
 Rotor (RotorConfig &&config)
 
 ~Rotor () override=default
 
AlphabetIndex transform (AlphabetIndex position, bool reverse=false) const override
 Transforms the given position based on the transformation lookup table (LUT).
 
AlphabetIndex transformForward (AlphabetIndex position) const override
 Transforms the given position forward using the forward LUT.
 
AlphabetIndex transformReverse (AlphabetIndex position) const override
 Transforms the given position in reverse using the reverse LUT.
 
int rotate () override
 Rotates the rotor by one position. If the rotor reaches the notch position, it returns 1, indicating that the next rotor should rotate. Otherwise, it returns 0.
 
void setPosition (int position) override
 Sets the rotor to a specific position.
 
int getPosition () const override
 Gets the current position of the rotor.
 
bool isNotchPosition (int position) const
 Checks if the given position is the notch position.
 
- Public Member Functions inherited from Transformer
 Transformer ()
 Constructor for the Transformer class. Initializes the transformer type to NotDefined.
 
virtual ~Transformer ()=default
 
int sizeOfLookupTable () const
 Calculates the size of the transformation lookup table (LUT).
 
TransformerType getType () const
 Returns the type of the transformer.
 

Private Member Functions

void initReverseLookupTable ()
 Generates the reverse transformation lookup table.
 
int initRotorPosition (int offset=0)
 Initializes the rotor position. This function sets the initial position of the rotor based on the provided offset.
 

Private Attributes

int notchPosition
 
int rotationCount
 

Additional Inherited Members

- Protected Member Functions inherited from Transformer
void setTransformValue (int row, int col, AlphabetIndex value)
 Sets a value in the transformation lookup table.
 
AlphabetIndex getTransformValue (int row, int col) const
 Gets a value from the transformation lookup table.
 
void fillTransformRow (int row, AlphabetIndex value)
 Fills a row of the transformation lookup table with a specific value.
 
void copyTransformRow (int row, const std::array< AlphabetIndex, TRANSFORMER_SIZE > &values)
 Copies a whole array into a row of the transformation lookup table.
 
const std::array< AlphabetIndex, TRANSFORMER_SIZE > & getTransformRow (int row) const
 Gets a read-only reference to a row in the transformation lookup table. Useful for using standard algorithms like std::find.
 
- Protected Attributes inherited from Transformer
TransformerType type
 

Detailed Description

Class representing a rotor in the Enigma machine. This class inherits from the Transformer class and implements the functionality for transforming positions based on the rotor's transformation lookup table (LUT). It also handles the rotor's rotation and notch position.

Definition at line 17 of file Rotor.hpp.

Constructor & Destructor Documentation

◆ Rotor() [1/2]

Rotor::Rotor ( const RotorConfig config)
explicit

Constructor for the Rotor class. Initializes the rotor with a configuration.

Parameters
configThe RotorConfig structure containing wiring and notch info.

Initializes the Rotor by:

  1. Setting the notch position and initial rotation count.
  2. Validating the wiring size against TRANSFORMER_SIZE.
  3. Populating the forward transformation table.
  4. Generating the reverse transformation table via initReverseLookupTable.
Exceptions
std::runtime_errorIf the wiring size in the config is incorrect.

Definition at line 22 of file Rotor.cpp.

References Transformer::copyTransformRow(), initReverseLookupTable(), initRotorPosition(), RotorConfig::notchPosition, notchPosition, rotationCount, Rotor, Transformer::type, and RotorConfig::wiring.

+ Here is the call graph for this function:

◆ Rotor() [2/2]

Rotor::Rotor ( RotorConfig &&  config)
explicit

Definition at line 33 of file Rotor.cpp.

References Transformer::copyTransformRow(), initReverseLookupTable(), initRotorPosition(), notchPosition, rotationCount, Rotor, and Transformer::type.

+ Here is the call graph for this function:

◆ ~Rotor()

Rotor::~Rotor ( )
overridedefault

Member Function Documentation

◆ getPosition()

int Rotor::getPosition ( ) const
overridevirtual

Gets the current position of the rotor.

Returns
int The current position (0 to TRANSFORMER_SIZE - 1).

Reimplemented from Transformer.

Definition at line 157 of file Rotor.cpp.

References rotationCount.

◆ initReverseLookupTable()

void Rotor::initReverseLookupTable ( )
private

Generates the reverse transformation lookup table.

Exceptions
std::runtime_errorIf reverse mapping generation fails.

Generates the mathematical inverse of the forward wiring to handle the return signal.

Definition at line 50 of file Rotor.cpp.

References Transformer::getTransformRow(), Transformer::setTransformValue(), and TRANSFORMER_SIZE.

Referenced by Rotor(), and Rotor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initRotorPosition()

int Rotor::initRotorPosition ( int  offset = 0)
private

Initializes the rotor position. This function sets the initial position of the rotor based on the provided offset.

Parameters
offsetThe initial offset for the rotor position.
Returns
int Returns 0 on success.

Sets the internal rotation counter to the specified offset.

Definition at line 77 of file Rotor.cpp.

References rotationCount.

Referenced by Rotor(), and Rotor().

+ Here is the caller graph for this function:

◆ isNotchPosition()

bool Rotor::isNotchPosition ( int  position) const

Checks if the given position is the notch position.

Parameters
positionThe position to check.
Returns
true if the position is the notch position, false otherwise.

Definition at line 82 of file Rotor.cpp.

References notchPosition.

Referenced by rotate().

+ Here is the caller graph for this function:

◆ rotate()

int Rotor::rotate ( )
overridevirtual

Rotates the rotor by one position. If the rotor reaches the notch position, it returns 1, indicating that the next rotor should rotate. Otherwise, it returns 0.

Returns
int Returns 1 if the rotor reached the notch position, otherwise returns 0.

Increments the rotor's position by one step (modulo TRANSFORMER_SIZE). Checks if the new position corresponds to the notch position.

Returns
1 if the rotor is now at the notch position (triggering a carry), 0 otherwise.

Implements Transformer.

Definition at line 143 of file Rotor.cpp.

References isNotchPosition(), rotationCount, and TRANSFORMER_SIZE.

+ Here is the call graph for this function:

◆ setPosition()

void Rotor::setPosition ( int  position)
overridevirtual

Sets the rotor to a specific position.

Parameters
positionThe position to set the rotor to (0 to TRANSFORMER_SIZE - 1).

Manually sets the current rotational position of the rotor. This is used for setting up the initial machine state key.

Reimplemented from Transformer.

Definition at line 155 of file Rotor.cpp.

References rotationCount.

◆ transform()

AlphabetIndex Rotor::transform ( AlphabetIndex  position,
bool  reverse = false 
) const
overridevirtual

Transforms the given position based on the transformation lookup table (LUT).

Parameters
positionThe current position in the rotor.
reverseIf true, transforms using the reverse LUT; otherwise, uses the forward LUT.
Returns
AlphabetIndex The transformed position.

This method is const-qualified, ensuring that the signal transformation does not modify the internal state of the rotor.

Handles the relative coordinate shift caused by the rotor's rotation.

  1. Shift input relative to rotor position.
  2. Apply internal wiring permutation.
  3. Shift output back to absolute machine coordinate system.

Implements Transformer.

Definition at line 93 of file Rotor.cpp.

References transformForward(), and transformReverse().

+ Here is the call graph for this function:

◆ transformForward()

AlphabetIndex Rotor::transformForward ( AlphabetIndex  position) const
overridevirtual

Transforms the given position forward using the forward LUT.

Parameters
positionThe current position in the rotor (0 to TRANSFORMER_SIZE - 1).
Returns
AlphabetIndex The transformed position (0 to TRANSFORMER_SIZE - 1).

Implements the forward signal path through the rotor's specific wiring, accounting for current rotation offset.

Performs the forward signal pass (right-to-left).

  1. Adjust input position by current rotation offset.
  2. Look up the internal wiring map (row 0).
  3. Adjust output back to the machine's absolute reference frame.

Implements Transformer.

Definition at line 103 of file Rotor.cpp.

References Transformer::getTransformValue(), rotationCount, and TRANSFORMER_SIZE.

Referenced by transform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ transformReverse()

AlphabetIndex Rotor::transformReverse ( AlphabetIndex  position) const
overridevirtual

Transforms the given position in reverse using the reverse LUT.

Parameters
positionThe current position in the rotor (0 to TRANSFORMER_SIZE - 1).
Returns
AlphabetIndex The transformed position (0 to TRANSFORMER_SIZE - 1).

Implements the return signal path through the rotor's specific wiring, accounting for current rotation offset.

Performs the reverse signal pass (left-to-right).

  1. Adjust input position by current rotation offset.
  2. Look up the internal wiring map (row 1 - inverse).
  3. Adjust output back to the machine's absolute reference frame.

Implements Transformer.

Definition at line 123 of file Rotor.cpp.

References Transformer::getTransformValue(), rotationCount, and TRANSFORMER_SIZE.

Referenced by transform().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ notchPosition

int Rotor::notchPosition
private

Definition at line 19 of file Rotor.hpp.

Referenced by Rotor(), Rotor(), and isNotchPosition().

◆ rotationCount

int Rotor::rotationCount
private

The documentation for this class was generated from the following files: