TuringSim
C++ framework to simulate abstract computing models
word.h
1 #pragma once
2 
3 #include <memory/memoryStructure.h>
4 
5 #include <vector>
6 
10 namespace TuringSim::Memory::Word {
11  template <typename T> class WordObserver;
12  template <typename T> class WordModifier;
13 
20  template<typename T>
21  class Word : public MemoryStructure<T, Word<T>, WordObserver<T>, WordModifier<T>> {
27  friend class WordObserver<T>;
28 
38  friend class WordModifier<T>;
39  public:
40 
43  constexpr Word();
44 
48  constexpr Word(const std::vector<T>& word);
49 
53  constexpr Word(const Word<T>& other);
54 
58  constexpr Word(Word<T>&& other);
59 
64  constexpr Word<T>& operator=(const Word<T>& other);
65 
70  constexpr Word<T>& operator=(Word<T>&& other);
71 
77  ~Word() = default;
78 
83  const T& read();
84 
88  std::optional<T> read_opt() noexcept;
89 
94  [[nodiscard]] const T& front() const;
95 
99  [[nodiscard]] std::optional<T> front_opt() const noexcept;
100 
104  [[nodiscard]] bool isEndOfWord() const noexcept;
105 
106  private:
107  std::vector<T> word;
108  size_t position;
109  };
110 
114  class WordOverflowException : public Utils::MessageException {
115  public:
119 
125  WordOverflowException(std::string message, size_t length, size_t position) :
126  Utils::MessageException(message),
127  length(length),
128  position(position)
129  {}
130 
131  protected:
132  virtual std::string makeFullMessage() const override {
133  std::ostringstream ss;
134  ss << this->msg << std::endl;
135  ss << " length: " << length << std::endl;
136  ss << " position: " << position << std::endl;
137  return ss.str();
138  }
139 
142  size_t length;
143 
146  size_t position;
147  };
148 
149  template<typename T>
150  constexpr Word<T>::Word() :
151  word(), position(0)
152  {}
153 
154  template<typename T>
155  constexpr Word<T>::Word(const std::vector<T>& word) :
156  word(word), position(0)
157  {}
158 
159  template<typename T>
160  constexpr Word<T>::Word(const Word<T> &other) :
161  word(other.word), position(other.position)
162  {}
163 
164  template<typename T>
165  constexpr Word<T>::Word(Word<T> &&other) :
166  word(std::move(other.word)), position(std::move(other.position))
167  {}
168 
169  template<typename T>
170  constexpr Word<T> &Word<T>::operator=(const Word<T>& other) {
171  if(this != &other) {
172  word = other.word;
173  position = other.position;
174  }
175  return *this;
176  }
177 
178  template<typename T>
179  constexpr Word<T> &Word<T>::operator=(Word<T>&& other) {
180  if(this != &other) {
181  word = std::move(other.word);
182  position = std::move(other.position);
183  }
184  return *this;
185  }
186 
187  template<typename T>
188  const T& Word<T>::read() {
189  if(position < word.size()) {
190  return word[position++];
191  }
192  throw WordOverflowException("End of word reached in Word<T>::read()", word.size(), position);
193  }
194 
195  template<typename T>
196  std::optional<T> Word<T>::read_opt() noexcept {
197  if(position < word.size()) {
198  return word[position++];
199  }
200  return std::nullopt;
201  }
202 
203  template<typename T>
204  const T& Word<T>::front() const {
205  if(position < word.size()) {
206  return word[position];
207  }
208  throw WordOverflowException("End of word reached in Word<T>::front()", word.size(), position);
209  }
210 
211  template<typename T>
212  std::optional<T> Word<T>::front_opt() const noexcept {
213  if(position < word.size()) {
214  return std::make_optional(word[position]);
215  }
216  return std::nullopt;
217  }
218 
219  template<typename T>
220  bool Word<T>::isEndOfWord() const noexcept {
221  return position == word.size();
222  }
223 
228  template <typename T>
229  class WordObserver : public MemoryObserver<Word<T>, WordObserver<T>> {
230  public:
239  constexpr WordObserver(Word<T>& word) : MemoryObserver<Word<T>, WordObserver<T>>(&word) {}
240 
244  [[nodiscard]] size_t getPosition() const noexcept {
245  return this->memory->position;
246  }
247 
251  [[nodiscard]] const std::vector<T>& getWord() const noexcept {
252  return this->memory->word;
253  }
254 
258  [[nodiscard]] size_t getSize() const noexcept {
259  return this->memory->word.size();
260  }
261  };
262 
267  template <typename T>
268  class WordModifier : public MemoryModifier<Word<T>, WordModifier<T>> {
269  public:
278  constexpr WordModifier(Word<T>& word) : MemoryModifier<Word<T>, WordModifier<T>>(&word) {}
279 
284  constexpr void setPosition(size_t position) {
285  if(position > this->memory->word.size()) {
286  throw WordOverflowException("Illegal position in WordModifier<T>::setPosition", this->memory->word.size(), position);
287  }
288  this->memory->position = position;
289  }
290 
296  constexpr T& operator[](size_t position) {
297  if(position >= this->memory->word.size()) {
298  throw WordOverflowException("Illegal position in WordModifier<T>::operator[]", this->memory->word.size(), position);
299  }
300  return this->memory->word[position];
301  }
302 
309  constexpr void setWord(const std::vector<T>& word) {
310  if(this->memory->position > word.size()) {
311  throw WordOverflowException("Illegal position in WordModifier<T>::setWord", this->memory->word.size(), this->memory->position);
312  }
313  this->memory->word = word;
314  }
315  };
316 }
TuringSim::Memory::Word::Word::Word
constexpr Word()
Create a new empty word.
Definition: word.h:150
TuringSim::Memory::Word::WordModifier::setWord
constexpr void setWord(const std::vector< T > &word)
Change the word.
Definition: word.h:309
TuringSim::Memory::Word::WordModifier::WordModifier
constexpr WordModifier(Word< T > &word)
Construct a modifier of the word in parameter.
Definition: word.h:278
TuringSim::Memory::Word::Word
Class to represent a word memory, as used by a FSA.
Definition: word.h:21
TuringSim::Memory::Word::WordOverflowException
The exception thrown when accessing a letter from an empty word.
Definition: word.h:114
TuringSim::Memory::Word::WordModifier
Modifier for the Word class.
Definition: word.h:268
TuringSim::Memory::Word::WordOverflowException::makeFullMessage
virtual std::string makeFullMessage() const override
Build the full error message. It should be overridden by derived class that adds data to the exceptio...
Definition: word.h:132
TuringSim::Memory::Word::Word::~Word
~Word()=default
Destruct the tape.
TuringSim::Memory::Word::WordOverflowException::length
size_t length
the length of the word.
Definition: word.h:142
TuringSim::Memory::Word::Word::Word
constexpr Word(Word< T > &&other)
Move a tape.
Definition: word.h:165
TuringSim::Memory::Word::WordObserver::getWord
const std::vector< T > & getWord() const noexcept
Get the underlying word vector.
Definition: word.h:251
TuringSim::Memory::Word::WordModifier::setPosition
constexpr void setPosition(size_t position)
Change the current position in the word.
Definition: word.h:284
TuringSim::Memory::Word::WordOverflowException::position
size_t position
the illegal requested position.
Definition: word.h:146
TuringSim::Memory::Word::Word::read_opt
std::optional< T > read_opt() noexcept
Return the next letter of the word, and increment the current position.
Definition: word.h:196
TuringSim::Memory::MemoryStructure
Base class for all memory structures.
Definition: memoryStructure.h:117
TuringSim::Memory::Word::WordObserver
Observer for the Word class.
Definition: word.h:229
TuringSim::Memory::Word::Word::front_opt
std::optional< T > front_opt() const noexcept
Return the next letter of the word.
Definition: word.h:212
TuringSim::Memory::Word::Word::operator=
constexpr Word< T > & operator=(Word< T > &&other)
Move a tape.
Definition: word.h:179
TuringSim::Memory::Word::WordObserver::getSize
size_t getSize() const noexcept
Get word size.
Definition: word.h:258
TuringSim::Memory::Word::Word::Word
constexpr Word(const Word< T > &other)
Copy a tape.
Definition: word.h:160
TuringSim::Memory::MemoryObserver
Base class for all memory observers.
Definition: memoryStructure.h:187
TuringSim::Memory::Word
Namespace for Word, the observer and the modifier.
TuringSim::Memory::Word::Word::read
const T & read()
Return the next letter of the word, and increment the current position.
Definition: word.h:188
TuringSim::Memory::MemoryModifier
Base class for all memory modifiers.
Definition: memoryStructure.h:307
TuringSim::Memory::Word::WordObserver::getPosition
size_t getPosition() const noexcept
Get the current position.
Definition: word.h:244
TuringSim::Memory::Word::WordOverflowException::WordOverflowException
WordOverflowException(std::string message, size_t length, size_t position)
Builds a EndOfWordException.
Definition: word.h:125
TuringSim::Memory::Word::Word::isEndOfWord
bool isEndOfWord() const noexcept
Test whether there are letter to read left.
Definition: word.h:220
TuringSim::Memory::Word::WordOverflowException::WordOverflowException
WordOverflowException()=delete
A EndOfWordException must contain interesting information.
TuringSim::Memory::Word::Word::Word
constexpr Word(const std::vector< T > &word)
Create a new word.
Definition: word.h:155
TuringSim::Memory::Word::WordObserver::WordObserver
constexpr WordObserver(Word< T > &word)
Construct an observer of the word in parameter.
Definition: word.h:239
TuringSim::Memory::Word::Word::operator=
constexpr Word< T > & operator=(const Word< T > &other)
Copy a tape.
Definition: word.h:170
TuringSim::Memory::Word::Word::front
const T & front() const
Return the next letter of the word.
Definition: word.h:204
TuringSim::Memory::Word::WordModifier::operator[]
constexpr T & operator[](size_t position)
Get a non-const reference to a letter in the word.
Definition: word.h:296