include/corelib/version.hpp

Go to the documentation of this file.
00001 #ifndef CORELIB___VERSION__HPP
00002 #define CORELIB___VERSION__HPP
00003 
00004 /*  $Id: version.hpp 130819 2008-06-13 16:29:09Z gouriano $
00005  * ===========================================================================
00006  *
00007  *                            PUBLIC DOMAIN NOTICE
00008  *               National Center for Biotechnology Information
00009  *
00010  *  This software/database is a "United States Government Work" under the
00011  *  terms of the United States Copyright Act.  It was written as part of
00012  *  the author's official duties as a United States Government employee and
00013  *  thus cannot be copyrighted.  This software/database is freely available
00014  *  to the public for use. The National Library of Medicine and the U.S.
00015  *  Government have not placed any restriction on its use or reproduction.
00016  *
00017  *  Although all reasonable efforts have been taken to ensure the accuracy
00018  *  and reliability of the software and data, the NLM and the U.S.
00019  *  Government do not and cannot warrant the performance or results that
00020  *  may be obtained by using this software or data. The NLM and the U.S.
00021  *  Government disclaim all warranties, express or implied, including
00022  *  warranties of performance, merchantability or fitness for any particular
00023  *  purpose.
00024  *
00025  *  Please cite the author in any work or product based on this material.
00026  *
00027  * ===========================================================================
00028  *
00029  * Authors:  Denis Vakatov, Vladimir Ivanov, Anatoliy Kuznetsov
00030  *
00031  *
00032  */
00033 
00034 /// @file version.hpp
00035 /// Define CVersionInfo, a version info storage class.
00036 
00037 
00038 #include <corelib/ncbistd.hpp>
00039 #include <corelib/ncbiobj.hpp>
00040 
00041 
00042 
00043 BEGIN_NCBI_SCOPE
00044 
00045 /** @addtogroup Version
00046  *
00047  * @{
00048  */
00049 
00050 /////////////////////////////////////////////////////////////////////////////
00051 // CVersionInfo
00052 
00053 
00054 /////////////////////////////////////////////////////////////////////////////
00055 ///
00056 /// CVersionInfo --
00057 ///
00058 /// Define class for storing version information.
00059 
00060 class  CVersionInfo
00061 {
00062 public:
00063     /// Default constructor
00064     CVersionInfo(void) ;
00065 
00066     /// Constructor
00067     CVersionInfo(int  ver_major,
00068                  int  ver_minor,
00069                  int  patch_level = 0,
00070                  const string& name = kEmptyStr);
00071 
00072     /// @param version
00073     ///    version string in rcs format (like 1.2.4)
00074     ///
00075     CVersionInfo(const string& version,
00076                  const string& name = kEmptyStr);
00077 
00078     enum EVersionFlags {
00079         kAny = 0,
00080         kLatest
00081     };
00082     CVersionInfo(EVersionFlags flags);
00083 
00084     /// Constructor.
00085     CVersionInfo(const CVersionInfo& version);
00086     CVersionInfo& operator=(const CVersionInfo& version);
00087 
00088     /// Destructor.
00089     virtual ~CVersionInfo() {}
00090 
00091     /// Take version info from string
00092     void FromStr(const string& version);
00093 
00094     void SetVersion(int  ver_major,
00095                int  ver_minor,
00096                int  patch_level = 0);
00097 
00098     /// Print version information.
00099     ///
00100     /// Version information is printed in the following forms:
00101     /// - <ver_major>.<ver_minor>.<patch_level>
00102     /// - <ver_major>.<ver_minor>.<patch_level> (<name>)
00103     virtual string Print(void) const;
00104 
00105     /// Major version
00106     int GetMajor(void) const { return m_Major; }
00107     /// Minor version
00108     int GetMinor(void) const { return m_Minor; }
00109     /// Patch level
00110     int GetPatchLevel(void) const { return m_PatchLevel; }
00111 
00112     const string& GetName(void) const { return m_Name; }
00113 
00114     /// Version comparison result
00115     /// @sa Match
00116     enum EMatch {
00117         eNonCompatible,           ///< major, minor does not match
00118         eConditionallyCompatible, ///< patch level incompatibility
00119         eBackwardCompatible,      ///< patch level is newer
00120         eFullyCompatible          ///< exactly the same version
00121     };
00122 
00123     /// Check if version matches another version.
00124     /// @param version_info
00125     ///   Version Info to compare with
00126     EMatch Match(const CVersionInfo& version_info) const;
00127 
00128     /// Check if version is all zero (major, minor, patch)
00129     /// Convention is that all-zero version used in requests as 
00130     /// "get me anything". 
00131     /// @sa kAny
00132     bool IsAny() const 
00133         { return !(m_Major | m_Minor | m_PatchLevel); }
00134 
00135     /// Check if version is all -1 (major, minor, patch)
00136     /// Convention is that -1 version used in requests as 
00137     /// "get me the latest version". 
00138     /// @sa kLatest
00139     bool IsLatest() const 
00140        { return (m_Major == -1 && m_Minor == -1 && m_PatchLevel == -1); }
00141 
00142     /// Check if this version info is more contemporary version 
00143     /// than parameter cinfo (or the same version)
00144     ///
00145     /// @param cinfo
00146     ///    Version checked (all components must be <= than this)
00147     ///
00148     bool IsUpCompatible(const CVersionInfo &cinfo) const
00149     {
00150         return cinfo.m_Major <= m_Major && 
00151                cinfo.m_Minor <= m_Minor &&
00152                cinfo.m_PatchLevel <= m_PatchLevel;
00153     }
00154 
00155 protected:
00156     int          m_Major;       ///< Major number
00157     int          m_Minor;       ///< Minor number
00158     int          m_PatchLevel;  ///< Patch level
00159     string       m_Name;        ///< Name
00160 };
00161 
00162 
00163 class  CComponentVersionInfo : public CVersionInfo
00164 {
00165 public:
00166 
00167     /// Constructor
00168     CComponentVersionInfo( const string& component_name,
00169                            int  ver_major,
00170                            int  ver_minor,
00171                            int  patch_level = 0,
00172                            const string& ver_name = kEmptyStr);
00173 
00174     /// Constructor
00175     ///
00176     /// @param component_name
00177     ///    component name
00178     /// @param version
00179     ///    version string (eg, 1.2.4)
00180     /// @param ver_name
00181     ///    version name
00182     CComponentVersionInfo( const string& component_name,
00183                            const string& version,
00184                            const string& ver_name = kEmptyStr);
00185 
00186     /// Copy constructor.
00187     CComponentVersionInfo(const CComponentVersionInfo& version);
00188 
00189     /// Assignment.
00190     CComponentVersionInfo& operator=(const CComponentVersionInfo& version);
00191 
00192     /// Destructor.
00193     virtual ~CComponentVersionInfo() {}
00194 
00195     /// Get component name
00196     const string& GetComponentName(void) const
00197     {
00198         return m_ComponentName;
00199     }
00200 
00201     /// Print version information.
00202     virtual string Print(void) const;
00203 
00204 private:
00205     // default ctor
00206     CComponentVersionInfo(void);
00207     string m_ComponentName;
00208 };
00209 
00210 
00211 class  CVersion : public CObject
00212 {
00213 public:
00214 
00215     CVersion(void);
00216     
00217     CVersion(const CVersionInfo& version);
00218 
00219     /// Copy constructor.
00220     CVersion(const CVersion& version);
00221     /// Destructor.
00222     virtual ~CVersion(void)
00223     {
00224     }
00225     
00226     /// Set version information
00227     void SetVersionInfo( int  ver_major,
00228                          int  ver_minor,
00229                          int  patch_level = 0,
00230                          const string& ver_name = kEmptyStr);
00231     /// Set version information
00232     /// @note Takes the ownership over the passed VersionInfo object 
00233     void SetVersionInfo( CVersionInfo* version);
00234     /// Get version information
00235     const CVersionInfo& GetVersionInfo( ) const;
00236 
00237     /// Add component version information
00238     void AddComponentVersion( const string& component_name,
00239                               int           ver_major,
00240                               int           ver_minor,
00241                               int           patch_level = 0,
00242                               const string& ver_name = kEmptyStr);
00243     /// Add component version information
00244     /// @note Takes the ownership over the passed VersionInfo object 
00245     void AddComponentVersion( CComponentVersionInfo* component);
00246 
00247     static string GetPackageName(void);
00248     static CVersionInfo GetPackageVersion(void);
00249     static string GetPackageConfig(void);
00250 
00251     enum EPrintFlags {
00252         fVersionInfo    = 0x01,  ///< Print version info
00253         fComponents     = 0x02,  ///< Print components version info
00254         fPackageShort   = 0x04,  ///< Print package info, if available
00255         fPackageFull    = 0x08,  ///< Print package info, if available
00256         fPrintAll       = 0xFF   ///< Print all version data
00257     };
00258     typedef int TPrintFlags;  ///< Binary OR of EPrintFlags
00259     
00260     /// Print version data.
00261     string Print(const string& appname, TPrintFlags flags = fPrintAll) const;
00262 
00263 private:
00264     AutoPtr< CVersionInfo > m_VersionInfo;
00265     vector< AutoPtr< CComponentVersionInfo> > m_Components;
00266 };
00267 
00268 
00269 /// Return true if one version info is matches another better than
00270 /// the best variant.
00271 /// When condition satisfies, return true and the former best values 
00272 /// are getting updated
00273 /// @param info
00274 ///    Version info to search
00275 /// @param cinfo
00276 ///    Comparison candidate
00277 /// @param best_major
00278 ///    Best major version found (reference)
00279 /// @param best_minor
00280 ///    Best minor version found (reference)
00281 /// @param best_patch_level
00282 ///    Best patch levelfound (reference)
00283 bool  IsBetterVersion(const CVersionInfo& info, 
00284                                        const CVersionInfo& cinfo,
00285                                        int&  best_major, 
00286                                        int&  best_minor,
00287                                        int&  best_patch_level);
00288 
00289 inline
00290 bool operator==(const CVersionInfo& v1, const CVersionInfo& v2)
00291 {
00292     
00293     return v1.Match(v2) == CVersionInfo::eFullyCompatible;
00294 }
00295 
00296 inline
00297 bool operator<(const CVersionInfo& v1, const CVersionInfo& v2)
00298 {
00299     int best_major = -1;
00300     int best_minor = -1;
00301     int best_patch_level = -1;
00302     
00303     return IsBetterVersion(v1, v2, best_major, best_minor, best_patch_level);
00304 }
00305 
00306 inline
00307 ostream& operator << (ostream& strm, const CVersionInfo& v)
00308 {
00309     strm << v.GetMajor() << "." << v.GetMinor() << "." << v.GetPatchLevel();
00310     
00311     return strm;
00312 }
00313 
00314 /// Algorithm function to find version in the container
00315 ///
00316 /// Scans the provided iterator for version with the same major and
00317 /// minor version and the newest patch level.
00318 ///
00319 /// @param first
00320 ///    first iterator to start search 
00321 /// @param last
00322 ///    ending iterator (typically returned by end() function of an STL
00323 ///    container)
00324 /// @return 
00325 ///    iterator on the best version or last
00326 template<class It>
00327 It FindVersion(It first, It last, const CVersionInfo& info)
00328 {
00329     It  best_version = last;  // not found by default
00330     int best_major = -1;
00331     int best_minor = -1;
00332     int best_patch_level = -1;
00333 
00334     for ( ;first != last; ++first) {
00335         const CVersionInfo& vinfo = *first;
00336 
00337         if (IsBetterVersion(vinfo, info, 
00338                             best_major, best_minor, best_patch_level))
00339         {
00340             best_version = first;
00341         }
00342     }        
00343     
00344     return best_version;
00345 }
00346 
00347 
00348 /// Algorithm function to find version in the container
00349 ///
00350 /// Scans the provided container for version with the same major and
00351 /// minor version and the newest patch level.
00352 ///
00353 /// @param container
00354 ///    container object to search in 
00355 /// @return 
00356 ///    iterator on the best fit version (last if no version found)
00357 template<class TClass>
00358 typename TClass::const_iterator FindVersion(const TClass& cont, 
00359                                             const CVersionInfo& info)
00360 {
00361     typename TClass::const_iterator it = cont.begin();
00362     typename TClass::const_iterator it_end = cont.end();
00363     return FindVersion(it, it_end, info);
00364 }
00365 
00366 /// Parse string, extract version info and program name
00367 /// (case insensitive)
00368 ///
00369 /// Examples:
00370 ///   MyProgram 1.2.3
00371 ///   MyProgram version 1.2.3
00372 ///   MyProgram v. 1.2.3
00373 ///   MyProgram ver. 1.2.3
00374 ///   version 1.2.3
00375 ///
00376 
00377 void ParseVersionString(const string&  vstr, 
00378                         string*        program_name, 
00379                         CVersionInfo*  ver);
00380 
00381 /* @} */
00382 
00383 
00384 END_NCBI_SCOPE
00385 
00386 #endif // CORELIB___VERSION__HPP
00387 
00388 

Generated on Wed Sep 3 23:51:57 2008 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Thu Sep 04 03:23:50 2008 by modify_doxy.py rev. 117643