Finalizable.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_FINALIZABLE_H_INCLUDED
24 #define ICL_CORE_FINALIZABLE_H_INCLUDED
25 
26 #include <iostream>
27 #include <stdexcept>
28 
29 namespace icl_core {
30 
41 template <typename T>
43 {
44 public:
45  Finalizable(const T& value)
46  : m_value(value),
47  m_final(false)
48  { }
49 
53  Finalizable(const Finalizable& other)
54  : m_value(other.m_value),
55  m_final(other.m_final)
56  { }
57 
60  {
61  if (!m_final)
62  {
63  m_value = other.m_value;
64  return *this;
65  }
66  else
67  {
68  throw std::logic_error("object is final");
69  }
70  }
71 
73  Finalizable& operator = (const T& value)
74  {
75  if (!m_final)
76  {
77  m_value = value;
78  return *this;
79  }
80  else
81  {
82  throw std::logic_error("object is final");
83  }
84  }
85 
87  operator T () const { return m_value; }
88 
90  bool isFinal() const { return m_final; }
91 
93  void finalize() { m_final = true; }
94 
96  std::ostream& operator << (std::ostream& os)
97  {
98  return os << m_value;
99  }
100 
101 private:
107  bool m_final;
108 };
109 
110 }
111 
112 #endif
bool isFinal() const
Returns true if the object is final.
Definition: Finalizable.h:90
std::ostream & operator<<(std::ostream &os)
Output stream operator.
Definition: Finalizable.h:96
void finalize()
Finalizes the object, i.e. makes it unmodifiable.
Definition: Finalizable.h:93
bool m_final
Indicates whether m_value is final, i.e. no longer modifiable.
Definition: Finalizable.h:107
Finalizable(const T &value)
Definition: Finalizable.h:45
Finalizable(const Finalizable &other)
Definition: Finalizable.h:53
Finalizable & operator=(const Finalizable &other)
Assignment is only allowed as long as this is not final.
Definition: Finalizable.h:59


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58