Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
read_only.h
Go to the documentation of this file.
1#ifndef READ_ONLY_H
2#define READ_ONLY_H
3
4#include <vector>
5#include <stdexcept>
6
12template <class T>
14{
15public:
16 read_only() : m_value{std::vector<T>()} {};
17 read_only(const T& value) : m_value{std::vector<T>(1, value)} {};
18
19 bool has_value() const { return !m_value.empty(); };
20
21 const T& get_value() const {
22 if(!this->has_value())
23 {
24 throw std::logic_error("this object does not contain a value!\n");
25 }
26 return m_value[0];
27 };
28
29 void operator=( const T& value ) { m_value[0] = value; };
30
31private:
32 std::vector<T> m_value;
33};
34
36void test_read_only();
37
38#endif // READ_ONLY_H
A copyable read-only value.
Definition read_only.h:14
const T & get_value() const
Definition read_only.h:21
read_only()
Definition read_only.h:16
bool has_value() const
Definition read_only.h:19
read_only(const T &value)
Definition read_only.h:17
void operator=(const T &value)
Definition read_only.h:29
void test_read_only()
Test our read_only class.
Definition read_only.cpp:40