osbutils.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00006 /*
00007   AUTHOR(S): Andreas Huggel (ahu)
00008              Stephan Broennimann (vb)
00009 
00010   RCS information
00011    $Name: OSB_060808 $
00012    $Revision: 1.58 $
00013 
00014   License
00015    OSB rating and billing library for communication networks
00016    Copyright (C) 2004, 2005, 2006  OSB systems
00017 
00018    This file may be distributed and/or modify under the terms of the
00019    GNU General Public License (GPL) as published by the Free Software
00020    Foundation which is provided in the file LICENSE.GPL included in the
00021    packaging of this file.
00022 
00023    The file is distributed in the hope that it will be useful, but WITHOUT
00024    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00025    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00026    for more details.
00027 
00028    Holders of a OSB Commercial License may use this file under the terms
00029    and conditions of this commercial license.
00030  */
00031 #ifndef _OSBUTILS_H_
00032 #define _OSBUTILS_H_
00033 
00034 // *********************************************************************
00035 // included header files
00036 // + standard includes
00037 #include<algorithm>
00038 #include<functional>
00039 #include<iosfwd>
00040 #include<string>
00041 #include<utility>
00042 
00043 // + local headers
00044 #include "chrono.h"
00045 #include "osbid.h"
00046 
00047 // *********************************************************************
00048 // namespace extensions
00049 namespace OSB_LIB {
00050 
00051 // *********************************************************************
00052 // type definitions
00053 
00054 // *********************************************************************
00055 // class declarations
00056 
00067     class EditStatus {
00068     public:
00070         enum Status {
00072             esUnchanged = 0x00,
00074             esModified  = 0x01,
00076             esCreated   = 0x02,
00078             esDeleted   = 0x04
00079         };
00080 
00082         EditStatus(Status status = esUnchanged);
00083 
00099         bool setRead() const;
00100 
00116         bool setCreated();
00117 
00132         bool setModified();
00133 
00150         bool changesDiscarded();
00151 
00160         bool setDeleted();
00161 
00170         bool setUndeleted();
00171 
00173         Status status() const;
00174 
00175     private:
00177         Status toStatus(int value) const;
00178 
00180         int status_;
00181     };
00182 
00183     // Identifier and a date
00184     template<typename I>
00185     struct IdDate{
00186         IdDate() : id(ID_NOT_SET) {}
00187         IdDate(long i, const Date& d) : id(i), dt(d) {}
00188         IdDate(long i, const std::string& d_str) : id(i), dt(d_str) {}
00189         bool notSet() const { return ID_NOT_SET == id; }
00190         bool isSet() const { return !notSet(); }
00191         bool operator==(const IdDate& rhs) const
00192             { return id == rhs.id && dt == rhs.dt; }
00193         bool operator<(const IdDate& rhs) const
00194             { return id == rhs.id ? dt < rhs.dt : id < rhs.id; }
00195 
00196         long id;
00197         Date dt;
00198     };
00199 
00200     // Identifier and a time period.
00201     // If only one date is given, the type represents a 'reference date'
00202     // and the comparison operators allow to search for the id/period
00203     // that contains a given reference date (eg, keys in STL containers)
00204     // Important: Two periods must not overlap!
00205     // ToDo: should use the type-safe id from above
00206     template<typename I>
00207     struct IdPeriod {
00208         IdPeriod() : id(ID_NOT_SET) {}
00209         IdPeriod(long i, const Date& d) : id(i), from(d), to(d) {}
00210         IdPeriod(long i, const std::string& d_str)
00211             : id(i), from(d_str), to(d_str) {}
00212         IdPeriod(long i, const Date& f, const Date& t)
00213             : id(i), from(std::min(f, t)), to(std::max(f, t)) {}
00214         IdPeriod(long i, const std::string& f_str,
00215                  const std::string& t_str)
00216             : id(i), from(std::min(Date(f_str), Date(t_str))),
00217               to(std::max(Date(f_str), Date(t_str))) {}
00218         inline bool notSet() const { return ID_NOT_SET == id; }
00219         bool isSet() const { return !notSet(); }
00220         inline bool operator==(const IdPeriod& rhs) const;
00221         inline bool operator!=(const IdPeriod& rhs) const;
00222         inline bool operator< (const IdPeriod& rhs) const;
00223         inline bool operator> (const IdPeriod& rhs) const;
00224         // ToDo: I'm not sure if <= and >= make sense
00225         inline bool operator<=(const IdPeriod& rhs) const;
00226         inline bool operator>=(const IdPeriod& rhs) const;
00227 
00228         long id;
00229         Date from;
00230         Date to;
00231     };
00232 
00233     // output operator for IdPeriod
00234     template<typename I>
00235     std::ostream& operator<<(std::ostream& o, IdPeriod<I> Id)
00236     {
00237         return o << Id.id << ','
00238                  << Id.from.str() << ','
00239                  << Id.to.str();
00240     }
00241 
00242     // Strict weak ordering for two objects by object identifiers.
00243     // Requires method oid()
00244     template<class T>
00245     struct LessOid : public std::binary_function<T, T, bool> {
00246         bool operator() (const T& x, const T& y) const
00247             { return x.oid() < y.oid(); }
00248     };
00249 
00250     // Specialization for pointers
00251     template<class T>
00252     struct LessOid<T*> : public std::binary_function<T*, T*, bool> {
00253         bool operator() (const T* x, const T* y) const
00254             { return x->oid() < y->oid(); }
00255     };
00256 
00257 // *********************************************************************
00258 // inline definitions
00259 
00260     // provided that the two ids are equal, then
00261     // + a period and a ref date are equal if the period contains
00262     //   the ref date
00263     // + two periods or ref dates are equal if they have the
00264     //   same boundaries
00265     template<typename I>
00266     inline bool IdPeriod<I>::operator==(const IdPeriod& rhs) const
00267     {
00268         if (id != rhs.id) return false;
00269         if (from == to && rhs.from != rhs.to) {
00270             return from >= rhs.from && from < rhs.to;
00271         }
00272         if (rhs.from == rhs.to && from != to) {
00273             return rhs.from >= from && rhs.from < to;
00274         }
00275         return from == rhs.from && to == rhs.to;
00276     }
00277 
00278     // provided that the two ids are equal, then
00279     // + a period is less than another period if its from date is less
00280     // + a ref date is less than a period if its from date is less
00281     // + a ref date is less than a ref date if its to/from dates are less
00282     // + a period is less than a ref date if its to date is less or equal
00283     template<typename I>
00284     inline bool IdPeriod<I>::operator<(const IdPeriod& rhs) const
00285     {
00286         if (id != rhs.id) return id < rhs.id;
00287         if (from != to && rhs.from == rhs.to) return to <= rhs.from;
00288         return from < rhs.from;
00289     }
00290 
00291     template<typename I>
00292     inline bool IdPeriod<I>::operator!=(const IdPeriod& rhs) const
00293     {
00294         return !(*this == rhs);
00295     }
00296 
00297     template<typename I>
00298     inline bool IdPeriod<I>::operator>(const IdPeriod& rhs) const
00299     {
00300         return rhs < *this;
00301     }
00302 }                                       // namespace OSB_LIB
00303 #endif                                  // #ifndef _OSBUTILS_H_

Generated on Sat Sep 2 14:06:33 2006 for OSB Library by  doxygen 1.4.7