00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 00003 // -- BEGIN LICENSE BLOCK ---------------------------------------------- 00004 // This file is part of FZIs ic_workspace. 00005 // 00006 // This program is free software licensed under the LGPL 00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3). 00008 // You can find a copy of this license in LICENSE folder in the top 00009 // directory of the source code. 00010 // 00011 // © Copyright 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany 00012 // 00013 // -- END LICENSE BLOCK ------------------------------------------------ 00014 00015 //---------------------------------------------------------------------- 00022 //---------------------------------------------------------------------- 00023 #ifndef ICL_CORE_FINALIZABLE_H_INCLUDED 00024 #define ICL_CORE_FINALIZABLE_H_INCLUDED 00025 00026 #include <iostream> 00027 #include <stdexcept> 00028 00029 namespace icl_core { 00030 00041 template <typename T> 00042 class Finalizable 00043 { 00044 public: 00045 Finalizable(const T& value) 00046 : m_value(value), 00047 m_final(false) 00048 { } 00049 00053 Finalizable(const Finalizable& other) 00054 : m_value(other.m_value), 00055 m_final(other.m_final) 00056 { } 00057 00059 Finalizable& operator = (const Finalizable& other) 00060 { 00061 if (!m_final) 00062 { 00063 m_value = other.m_value; 00064 return *this; 00065 } 00066 else 00067 { 00068 throw std::logic_error("object is final"); 00069 } 00070 } 00071 00073 Finalizable& operator = (const T& value) 00074 { 00075 if (!m_final) 00076 { 00077 m_value = value; 00078 return *this; 00079 } 00080 else 00081 { 00082 throw std::logic_error("object is final"); 00083 } 00084 } 00085 00087 operator T () const { return m_value; } 00088 00090 bool isFinal() const { return m_final; } 00091 00093 void finalize() { m_final = true; } 00094 00096 std::ostream& operator << (std::ostream& os) 00097 { 00098 return os << m_value; 00099 } 00100 00101 private: 00105 T m_value; 00107 bool m_final; 00108 }; 00109 00110 } 00111 00112 #endif