00001 #ifndef CORELIB___VERSION__HPP
00002 #define CORELIB___VERSION__HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <corelib/ncbistd.hpp>
00039 #include <corelib/ncbiobj.hpp>
00040
00041
00042
00043 BEGIN_NCBI_SCOPE
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 class CVersionInfo
00061 {
00062 public:
00063
00064 CVersionInfo(void) ;
00065
00066
00067 CVersionInfo(int ver_major,
00068 int ver_minor,
00069 int patch_level = 0,
00070 const string& name = kEmptyStr);
00071
00072
00073
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
00085 CVersionInfo(const CVersionInfo& version);
00086 CVersionInfo& operator=(const CVersionInfo& version);
00087
00088
00089 virtual ~CVersionInfo() {}
00090
00091
00092 void FromStr(const string& version);
00093
00094 void SetVersion(int ver_major,
00095 int ver_minor,
00096 int patch_level = 0);
00097
00098
00099
00100
00101
00102
00103 virtual string Print(void) const;
00104
00105
00106 int GetMajor(void) const { return m_Major; }
00107
00108 int GetMinor(void) const { return m_Minor; }
00109
00110 int GetPatchLevel(void) const { return m_PatchLevel; }
00111
00112 const string& GetName(void) const { return m_Name; }
00113
00114
00115
00116 enum EMatch {
00117 eNonCompatible,
00118 eConditionallyCompatible,
00119 eBackwardCompatible,
00120 eFullyCompatible
00121 };
00122
00123
00124
00125
00126 EMatch Match(const CVersionInfo& version_info) const;
00127
00128
00129
00130
00131
00132 bool IsAny() const
00133 { return !(m_Major | m_Minor | m_PatchLevel); }
00134
00135
00136
00137
00138
00139 bool IsLatest() const
00140 { return (m_Major == -1 && m_Minor == -1 && m_PatchLevel == -1); }
00141
00142
00143
00144
00145
00146
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;
00157 int m_Minor;
00158 int m_PatchLevel;
00159 string m_Name;
00160 };
00161
00162
00163 class CComponentVersionInfo : public CVersionInfo
00164 {
00165 public:
00166
00167
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
00175
00176
00177
00178
00179
00180
00181
00182 CComponentVersionInfo( const string& component_name,
00183 const string& version,
00184 const string& ver_name = kEmptyStr);
00185
00186
00187 CComponentVersionInfo(const CComponentVersionInfo& version);
00188
00189
00190 CComponentVersionInfo& operator=(const CComponentVersionInfo& version);
00191
00192
00193 virtual ~CComponentVersionInfo() {}
00194
00195
00196 const string& GetComponentName(void) const
00197 {
00198 return m_ComponentName;
00199 }
00200
00201
00202 virtual string Print(void) const;
00203
00204 private:
00205
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
00220 CVersion(const CVersion& version);
00221
00222 virtual ~CVersion(void)
00223 {
00224 }
00225
00226
00227 void SetVersionInfo( int ver_major,
00228 int ver_minor,
00229 int patch_level = 0,
00230 const string& ver_name = kEmptyStr);
00231
00232
00233 void SetVersionInfo( CVersionInfo* version);
00234
00235 const CVersionInfo& GetVersionInfo( ) const;
00236
00237
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
00244
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,
00253 fComponents = 0x02,
00254 fPackageShort = 0x04,
00255 fPackageFull = 0x08,
00256 fPrintAll = 0xFF
00257 };
00258 typedef int TPrintFlags;
00259
00260
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
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
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
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 template<class It>
00327 It FindVersion(It first, It last, const CVersionInfo& info)
00328 {
00329 It best_version = last;
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
00349
00350
00351
00352
00353
00354
00355
00356
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
00367
00368
00369
00370
00371
00372
00373
00374
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