00001 // OSB library ************************************************ -*- C++ -*- 00006 /* 00007 AUTHOR(S): Stephan Broennimann (vb) 00008 00009 RCS information 00010 $Name: OSB_060808 $ 00011 $Revision: 1.8 $ 00012 00013 License 00014 OSB rating and billing library for communication networks 00015 Copyright (C) 2004, 2005, 2006 OSB systems 00016 00017 This file may be distributed and/or modify under the terms of the 00018 GNU General Public License (GPL) as published by the Free Software 00019 Foundation which is provided in the file LICENSE.GPL included in the 00020 packaging of this file. 00021 00022 The file is distributed in the hope that it will be useful, but WITHOUT 00023 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00024 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00025 for more details. 00026 00027 Holders of a OSB Commercial License may use this file under the terms 00028 and conditions of this commercial license. 00029 */ 00030 #ifndef _STATUSCHANGE_H_ 00031 #define _STATUSCHANGE_H_ 00032 00033 // ************************************************************************ 00034 // included header files 00035 // + standard includes 00036 #include <set> 00037 00038 // + libraries 00039 00040 // + local headers 00041 #include "status.h" 00042 #include "period.h" 00043 00044 // + class declarations 00045 namespace OSB_DB { 00046 class StatusChangeGw; 00047 } 00048 00049 namespace OSB_LIB { 00050 class StatusRequest; 00051 } 00052 00053 // ************************************************************************ 00054 // namespace extensions 00055 namespace OSB_LIB { 00056 00057 // ************************************************************************ 00058 // forward declarations 00059 00060 // ************************************************************************ 00061 // type definitions 00062 00063 // ************************************************************************ 00064 // class definitions 00071 class StatusChange { 00073 friend class OSB_DB::StatusChangeGw; 00074 00075 public: 00077 typedef Id<StatusChange> Oid; 00078 00079 // defined below 00080 class GeValidFrom; 00081 00082 public: 00090 explicit StatusChange( 00091 const Oid& oid = Oid() 00092 ); 00093 00094 public: 00096 const Oid oid() const; 00097 00103 void adjustNewStatus( 00104 const Status& prefStatus 00105 ); 00106 00108 GeValidFrom geValidFrom() const; 00109 00121 bool read( 00122 const OSB_DB::Session& session 00123 ); 00124 00131 std::ostream& write(std::ostream& os) const; 00132 00133 private: 00135 Oid oid_; 00136 public: 00138 Id<StatusRequest> requestId_; 00140 StatusValue oldStatus_; 00142 StatusValue newStatus_; 00144 StatusReason::Oid reasonId_; 00149 DateTime validFrom_; 00150 }; // class StatusChange 00151 00160 class StatusChange::GeValidFrom { 00161 public: 00163 GeValidFrom(const DateTime& rhs); 00165 bool operator()(const StatusChange& lhs); 00166 private: 00168 const DateTime rhs_; 00169 }; // class StatusRequest::GeValidFrom 00170 00180 std::ostream& operator<<(std::ostream& os, 00181 const StatusChange& sc); 00182 00193 class StatusChanges { 00195 friend class OSB_DB::StatusChangeGw; 00196 public: 00198 struct Cmp { 00206 bool operator()( 00207 const StatusChange& lhs, 00208 const StatusChange& rhs 00209 ) const; 00210 }; 00211 public: 00213 typedef std::set<StatusChange, Cmp> Cont; 00215 typedef Cont::const_iterator ConstIter; 00217 typedef Cont::const_reverse_iterator ConstRiter; 00218 00219 public: 00223 StatusChanges(); 00224 00226 StatusChanges( 00227 const Period& period 00228 ); 00229 00230 public: 00232 const Period& period() const; 00233 00235 bool empty() const; 00237 size_t size() const; 00238 00240 ConstIter begin() const; 00242 ConstIter end() const; 00244 ConstRiter rbegin() const; 00246 ConstRiter rend() const; 00247 00249 const Cont& changes() const; 00250 00252 template<typename Pred> 00253 size_t countIf(Pred pred) const; 00254 00262 void setPeriod( 00263 const Period& period 00264 ); 00265 00269 void clear(); 00270 00276 void add( 00277 const StatusChanges& rhs 00278 ); 00279 00281 void swap(StatusChanges& rhs) throw(); 00282 00283 private: 00285 Period period_; 00295 Cont changes_; 00296 }; // class StatusChanges 00297 } // namespace OSB_LIB 00298 00299 // ************************************************************************ 00300 // inline definitions 00301 namespace OSB_LIB { 00302 00303 // ******************************************************************** 00304 // StatusChange 00305 inline StatusChange::StatusChange( 00306 const Oid& oid 00307 ) : oid_(oid), oldStatus_(svNone), newStatus_(svNone) 00308 { 00309 // empty 00310 } 00311 00312 inline const StatusChange::Oid StatusChange::oid() const 00313 { 00314 return oid_; 00315 } 00316 00317 inline void StatusChange::adjustNewStatus( 00318 const Status& pref 00319 ) 00320 { 00321 newStatus_ = mkChildStatus(newStatus_, pref.status()); 00322 } 00323 00324 inline bool StatusChange::GeValidFrom::operator()( 00325 const StatusChange& lhs 00326 ) 00327 { 00328 return lhs.validFrom_ >= rhs_; 00329 } 00330 00331 // ******************************************************************** 00332 // StatusChanges 00333 inline StatusChanges::StatusChanges() 00334 { 00335 // empty 00336 } 00337 00338 inline StatusChanges::StatusChanges( 00339 const Period& period 00340 ) : period_(period) 00341 { 00342 // empty 00343 } 00344 00345 inline const Period& StatusChanges::period() const 00346 { 00347 return period_; 00348 } 00349 00350 inline bool StatusChanges::empty() const 00351 { 00352 return changes_.empty(); 00353 } 00354 00355 inline size_t StatusChanges::size() const 00356 { 00357 return changes_.size(); 00358 } 00359 00360 inline const StatusChanges::Cont& StatusChanges::changes() const 00361 { 00362 return changes_; 00363 } 00364 00365 inline StatusChanges::ConstIter StatusChanges::begin() const 00366 { 00367 return changes_.begin(); 00368 } 00369 00370 inline StatusChanges::ConstIter StatusChanges::end() const 00371 { 00372 return changes_.end(); 00373 } 00374 00375 inline StatusChanges::ConstRiter StatusChanges::rbegin() const 00376 { 00377 return changes_.rbegin(); 00378 } 00379 00380 inline StatusChanges::ConstRiter StatusChanges::rend() const 00381 { 00382 return changes_.rend(); 00383 } 00384 00385 template<typename Pred> 00386 inline size_t StatusChanges::countIf(Pred pred) const 00387 { 00388 return std::count_if(begin(), end(), pred); 00389 } 00390 00391 inline bool StatusChanges::Cmp::operator()( 00392 const StatusChange& lhs, 00393 const StatusChange& rhs 00394 ) const 00395 { 00396 if (lhs.validFrom_ < rhs.validFrom_) return true; 00397 if (lhs.validFrom_ > rhs.validFrom_) return false; 00398 return lhs.oid() < rhs.oid(); 00399 } 00400 } // namespace OSB_LIB 00401 #endif // #ifndef _STATUSCHANGE_H_