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 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany 00012 // 00013 // -- END LICENSE BLOCK ------------------------------------------------ 00014 00015 //---------------------------------------------------------------------- 00028 //---------------------------------------------------------------------- 00029 #ifndef ICL_CORE_SEQUENCE_NUMBER_H_INCLUDED 00030 #define ICL_CORE_SEQUENCE_NUMBER_H_INCLUDED 00031 00032 #include "icl_core/BaseTypes.h" 00033 00034 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 00035 # include "icl_core/Deprecate.h" 00036 #endif 00037 00038 namespace icl_core { 00039 00044 template <typename TBase, TBase max_value, TBase min_value = 0, TBase initial_value = min_value> 00045 class SequenceNumber 00046 { 00047 public: 00052 explicit SequenceNumber(TBase value = initial_value) 00053 : m_value(value) 00054 { 00055 } 00056 00060 SequenceNumber(const SequenceNumber& other) 00061 : m_value(other.m_value) 00062 { 00063 } 00064 00068 SequenceNumber& operator = (const SequenceNumber& other) 00069 { 00070 m_value = other.m_value; 00071 return *this; 00072 } 00073 00077 SequenceNumber& operator = (TBase value) 00078 { 00079 m_value = value; 00080 return *this; 00081 } 00082 00086 bool operator < (const SequenceNumber& other) const 00087 { 00088 if (m_value < other.m_value) 00089 { 00090 return (other.m_value - m_value) < max_value / 2; 00091 } 00092 else 00093 { 00094 return (m_value - other.m_value) > max_value / 2; 00095 } 00096 } 00097 00101 bool operator <= (const SequenceNumber& other) const 00102 { 00103 return (*this) < other || (*this) == other; 00104 } 00105 00109 bool operator > (const SequenceNumber& other) const 00110 { 00111 return (*this) != other && !((*this) < other); 00112 } 00113 00117 bool operator >= (const SequenceNumber& other) const 00118 { 00119 return !((*this) < other); 00120 } 00121 00125 bool operator == (const SequenceNumber& other) const 00126 { 00127 return m_value == other.m_value; 00128 } 00129 00133 bool operator != (const SequenceNumber& other) const 00134 { 00135 return !((*this) == other); 00136 } 00137 00141 SequenceNumber& operator ++ () 00142 { 00143 ++m_value; 00144 if (m_value >= max_value) 00145 { 00146 m_value = min_value; 00147 } 00148 return *this; 00149 } 00150 00154 SequenceNumber operator ++ (int) 00155 { 00156 SequenceNumber result = *this; 00157 ++(*this); 00158 return result; 00159 } 00160 00164 operator TBase () const 00165 { 00166 return m_value; 00167 } 00168 00172 static TBase maxValue() 00173 { 00174 return max_value; 00175 } 00176 00180 static TBase minValue() 00181 { 00182 return min_value; 00183 } 00184 00186 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 00187 00192 static ICL_CORE_VC_DEPRECATE_STYLE TBase MaxValue() ICL_CORE_GCC_DEPRECATE_STYLE; 00193 00198 static ICL_CORE_VC_DEPRECATE_STYLE TBase MinValue() ICL_CORE_GCC_DEPRECATE_STYLE; 00199 00200 #endif 00201 00202 00203 private: 00204 TBase m_value; 00205 }; 00206 00207 } 00208 00209 #endif