ViennaGrid - The Vienna Grid Library
2.1.0
|
00001 #ifndef VIENNAGRID_STORAGE_ID_HPP 00002 #define VIENNAGRID_STORAGE_ID_HPP 00003 00004 /* ======================================================================= 00005 Copyright (c) 2011-2014, Institute for Microelectronics, 00006 Institute for Analysis and Scientific Computing, 00007 TU Wien. 00008 00009 ----------------- 00010 ViennaGrid - The Vienna Grid Library 00011 ----------------- 00012 00013 License: MIT (X11), see file LICENSE in the base directory 00014 ======================================================================= */ 00015 00016 #include <iostream> 00017 00022 namespace viennagrid 00023 { 00024 template<typename id_type> 00025 struct id_tag; 00026 00027 template<typename base_id_type> 00028 struct smart_id_tag; 00029 00030 namespace detail 00031 { 00032 template<typename value_type_, typename base_id_type_> 00033 class smart_id 00034 { 00035 public: 00036 typedef smart_id self_type; 00037 typedef value_type_ value_type; 00038 typedef base_id_type_ base_id_type; 00039 00040 typedef smart_id<const value_type, base_id_type> self_const_type; 00041 00042 smart_id() : internal_id(-1) {} 00043 explicit smart_id( base_id_type internal_id_ ) : internal_id(internal_id_) {} 00044 00045 base_id_type get() const { return internal_id; } 00046 void set( base_id_type internal_id_ ) { internal_id =internal_id_; } 00047 00048 bool operator== ( self_type rhs ) const { return internal_id == rhs.get(); } 00049 bool operator!= ( self_type rhs ) const { return !(*this == rhs); } 00050 bool operator< ( self_type rhs ) const { return internal_id < rhs.get(); } 00051 bool operator<= ( self_type rhs ) const { return internal_id <= rhs.get(); } 00052 bool operator> ( self_type rhs ) const { return internal_id > rhs.get(); } 00053 bool operator>= (self_type rhs ) const { return internal_id >= rhs.get(); } 00054 00055 bool operator== ( self_const_type rhs ) const { return internal_id == rhs.get(); } 00056 bool operator!= ( self_const_type rhs ) const { return !(*this == rhs); } 00057 bool operator< ( self_const_type rhs ) const { return internal_id < rhs.get(); } 00058 bool operator<= ( self_const_type rhs ) const { return internal_id <= rhs.get(); } 00059 bool operator> ( self_const_type rhs ) const { return internal_id > rhs.get(); } 00060 bool operator>= (self_const_type rhs ) const { return internal_id >= rhs.get(); } 00061 00062 self_type & operator++() { ++internal_id; return *this; } 00063 self_type operator++(int) { self_type tmp(*this); ++*this; return tmp; } 00064 00065 private: 00066 base_id_type internal_id; 00067 }; 00068 00069 00070 00071 template<typename value_type_, typename base_id_type_> 00072 class smart_id<const value_type_, base_id_type_> 00073 { 00074 public: 00075 typedef smart_id self_type; 00076 typedef value_type_ value_type; 00077 typedef base_id_type_ base_id_type; 00078 00079 typedef smart_id<value_type, base_id_type> self_non_const_type; 00080 00081 smart_id() : internal_id(-1) {} 00082 smart_id( smart_id<value_type_, base_id_type> const & id_ ) : internal_id(id_.get()) {} 00083 explicit smart_id( base_id_type internal_id_ ) : internal_id(internal_id_) {} 00084 00085 base_id_type get() const { return internal_id; } 00086 void set( base_id_type internal_id_ ) { internal_id =internal_id_; } 00087 00088 bool operator== ( self_type rhs ) const { return internal_id == rhs.get(); } 00089 bool operator!= ( self_type rhs ) const { return !(*this == rhs); } 00090 bool operator< ( self_type rhs ) const { return internal_id < rhs.get(); } 00091 bool operator<= ( self_type rhs ) const { return internal_id <= rhs.get(); } 00092 bool operator> ( self_type rhs ) const { return internal_id > rhs.get(); } 00093 bool operator>= (self_type rhs ) const { return internal_id >= rhs.get(); } 00094 00095 bool operator== ( self_non_const_type rhs ) const { return internal_id == rhs.get(); } 00096 bool operator!= ( self_non_const_type rhs ) const { return !(*this == rhs); } 00097 bool operator< ( self_non_const_type rhs ) const { return internal_id < rhs.get(); } 00098 bool operator<= ( self_non_const_type rhs ) const { return internal_id <= rhs.get(); } 00099 bool operator> ( self_non_const_type rhs ) const { return internal_id > rhs.get(); } 00100 bool operator>= (self_non_const_type rhs ) const { return internal_id >= rhs.get(); } 00101 00102 self_type & operator++() { ++internal_id; return *this; } 00103 self_type operator++(int) { self_type tmp(*this); ++*this; return tmp; } 00104 00105 private: 00106 base_id_type internal_id; 00107 }; 00108 00109 00110 template<typename value_type, typename base_id_type> 00111 bool operator<( smart_id<value_type, base_id_type> const & lhs, smart_id<value_type, base_id_type> const & rhs ) 00112 { return lhs.get() < rhs.get(); } 00113 00114 template<typename value_type, typename base_id_type> 00115 bool operator<( smart_id<const value_type, base_id_type> const & lhs, smart_id<value_type, base_id_type> const & rhs ) 00116 { return lhs.get() < rhs.get(); } 00117 00118 template<typename value_type, typename base_id_type> 00119 bool operator<( smart_id<value_type, base_id_type> const & lhs, smart_id<const value_type, base_id_type> const & rhs ) 00120 { return lhs.get() < rhs.get(); } 00121 00122 template<typename value_type, typename base_id_type> 00123 bool operator<( smart_id<const value_type, base_id_type> const & lhs, smart_id<const value_type, base_id_type> const & rhs ) 00124 { return lhs.get() < rhs.get(); } 00125 00126 00127 00128 template<typename value_type_, typename base_id_type> 00129 std::ostream & operator<< (std::ostream & os, smart_id<value_type_, base_id_type> id) 00130 { 00131 os << id.get(); 00132 return os; 00133 } 00134 00135 namespace result_of 00136 { 00137 template<typename value_type, typename id_tag> 00138 struct make_id; 00139 00140 template<typename value_type, typename id_type> 00141 struct make_id<value_type, id_tag<id_type> > 00142 { 00143 typedef id_type type; 00144 }; 00145 00146 template<typename value_type, typename base_id_type> 00147 struct make_id<value_type, smart_id_tag<base_id_type> > 00148 { 00149 typedef smart_id<value_type, base_id_type> type; 00150 }; 00151 00152 00153 template<typename id_type> 00154 struct const_id 00155 { 00156 typedef const id_type type; 00157 }; 00158 00159 template<typename value_type, typename id_type> 00160 struct const_id< smart_id<value_type, id_type> > 00161 { 00162 typedef smart_id<const value_type, id_type> type; 00163 }; 00164 } 00165 00166 00167 00168 template<typename id_type_> 00169 class id_handler 00170 { 00171 public: 00172 typedef id_type_ id_type; 00173 typedef typename result_of::const_id<id_type>::type const_id_type; 00174 00175 id_handler() {} 00176 id_handler(const id_handler & rhs) : id_(rhs.id_) {} 00177 00178 void id(id_type id) { id_ = id; } 00179 id_type id() const { return id_; } 00180 00181 private: 00182 id_type id_; 00183 }; 00184 00185 00186 namespace result_of 00187 { 00188 template<typename some_type> 00189 struct id 00190 { 00191 typedef typename some_type::id_type type; 00192 }; 00193 } 00194 00195 00196 00197 template<typename id_type_> 00198 class id_compare 00199 { 00200 public: 00201 typedef id_type_ id_type; 00202 typedef typename result_of::const_id<id_type>::type const_id_type; 00203 00204 id_compare(const_id_type id_) : id(id_) {} 00205 00206 template<typename type> 00207 bool operator() ( const type & to_compare ) 00208 { 00209 return to_compare.id() == id; 00210 } 00211 00212 private: 00213 const_id_type id; 00214 }; 00215 00216 00217 template<typename element_type, typename id_type> 00218 void set_id( element_type & element, id_type id ) 00219 { 00220 element.id(id); 00221 } 00222 00223 template<typename id_type> void set_id( bool &, id_type ) {} 00224 template<typename id_type> void set_id( char &, id_type ) {} 00225 template<typename id_type> void set_id( unsigned char &, id_type ) {} 00226 template<typename id_type> void set_id( short &, id_type ) {} 00227 template<typename id_type> void set_id( unsigned short &, id_type ) {} 00228 template<typename id_type> void set_id( int &, id_type ) {} 00229 template<typename id_type> void set_id( unsigned int &, id_type ) {} 00230 template<typename id_type> void set_id( long &, id_type ) {} 00231 template<typename id_type> void set_id( unsigned long &, id_type ) {} 00232 template<typename id_type> void set_id( float &, id_type ) {} 00233 template<typename id_type> void set_id( double &, id_type ) {} 00234 00235 00236 } 00237 00238 } 00239 00240 #endif