OSDN Git Service

Ignore spin wait requests with no designated referrer.
[mingw/mingw-get.git] / src / vercmp.h
1 #ifndef VERCMP_H
2 /*
3  * vercmp.h
4  *
5  * $Id$
6  *
7  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
8  * Copyright (C) 2009, 2010, 2011, MinGW Project
9  *
10  *
11  * Public interface for the package version comparator module, as
12  * implemented in the "C++" language file "vercmp.cpp".
13  *
14  *
15  * This is free software.  Permission is granted to copy, modify and
16  * redistribute this software, under the provisions of the GNU General
17  * Public License, Version 3, (or, at your option, any later version),
18  * as published by the Free Software Foundation; see the file COPYING
19  * for licensing details.
20  *
21  * Note, in particular, that this software is provided "as is", in the
22  * hope that it may prove useful, but WITHOUT WARRANTY OF ANY KIND; not
23  * even an implied WARRANTY OF MERCHANTABILITY, nor of FITNESS FOR ANY
24  * PARTICULAR PURPOSE.  Under no circumstances will the author, or the
25  * MinGW Project, accept liability for any damages, however caused,
26  * arising from the use of this software.
27  *
28  */
29 #define VERCMP_H  1
30
31 #include <stdlib.h>
32
33 enum
34 { /* The constituent elements of a package version number,
35    * and build serial number, in sequential order as they appear
36    * within a correctly formed package tarball name.
37    */
38   VERSION_MAJOR = 0,
39   VERSION_MINOR,
40   VERSION_PATCHLEVEL,
41   SNAPSHOT_DATE,
42   SERIAL_NUMBER,
43
44   /* Not a real element of the version/serial number sequence,
45    * but this final entry in the enumeration provides a convenient
46    * counter for the number of real elements listed above.
47    */
48   VERSION_ELEMENT_COUNT
49 };
50
51 struct version_t
52 {
53   /* Internal structure, used to capture an individual element
54    * of a decomposed version number, or build serial number.
55    */
56   unsigned long     value;
57   const char*       suffix;
58 };
59
60 #ifdef __cplusplus
61 /*
62  * Define the properties of the "C++" class API...
63  */
64 class pkgVersionInfo
65 {
66   /* A class for capture and manipulation of package version numbers
67    * in decomposed "major.minor.patch-datetamp-sequence" form.
68    */
69   public:
70     inline pkgVersionInfo( const char* version = "", const char* build = NULL )
71     {
72       /* Constructor...
73        * This expects either one or two "char *" arguments:
74        * the first is the package version number, in "major.minor.patch"
75        * format; the second is build serial number in "datestamp-sequence"
76        * format.  If the second is omitted, the build serial number may
77        * be appended to the first, in the full format as above.
78        *
79        * Note that we inline the constructor itself, but we then delegate
80        * its entire implementation to the Parse() method, to facilitate...
81        */
82       Parse( version, build );
83     }
84     inline void Reset( const char* version = "", const char* build = NULL )
85     {
86       /* ...implementation of this "reconstructor" method, which permits
87        * us to reassign alternative content to an existing instance of the
88        * class, (after first clearing out all previous content).
89        */
90       FreeAll(); Parse( version, build );
91     }
92
93     /* Destructor...
94      * This must release all heap memory allocated for parsed class data.
95      */
96     inline ~pkgVersionInfo(){ FreeAll(); }
97
98     /* Package version comparison operators.
99      */
100     bool operator<( const pkgVersionInfo& );
101     bool operator<=( const pkgVersionInfo& );
102     bool operator==( const pkgVersionInfo& );
103     bool operator!=( const pkgVersionInfo& );
104     bool operator>=( const pkgVersionInfo& );
105     bool operator>( const pkgVersionInfo& );
106
107   private:
108     /* The decomposed version/serial number elements.
109      */
110     char *version_string, *build_string;
111     struct version_t version_elements[VERSION_ELEMENT_COUNT];
112
113     /* The separated implementation for the constructor,
114      * shared by the Reset() "reconstructor" method.
115      */
116     void Parse( const char*, const char* );
117
118     /* An internal comparison helper function.
119      */
120     long Compare( const pkgVersionInfo&, int );
121
122     inline void FreeEntry( void *mem )
123     {
124       /* Helper method to release each of the two blocks of heap
125        * memory, which are allocated to store the class data...
126        */
127       if( mem != NULL ) free( mem );
128     }
129     inline void FreeAll()
130     {
131       /* ...used by this composite helper, which releases both of
132        * these allocated memory blocks with a single call.
133        */
134       FreeEntry( version_string ); FreeEntry( build_string );
135     }
136 };
137
138 #endif /* __cplusplus */
139 #endif /* VERCMP_H: $RCSfile$: end of file */