Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Version.h
1 /*
2  * $Id$
3  * Version class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2005-08, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_UTIL_VERSION_H
23 #define ELM_UTIL_VERSION_H
24 
25 #include <elm/assert.h>
26 #include <elm/io.h>
27 #undef major
28 #undef minor
29 
30 namespace elm {
31 
32 class Version {
33 public:
34  static const Version ZERO;
35 
36  // Constructors
37  inline Version(int major = 0, int minor = 0, int release = 0);
38  inline Version(const Version& version);
39  inline Version(const char *text) { *this = string(text); }
40  inline Version(const cstring text) { *this = string(text); }
41  inline Version(const string& text) { *this = text; }
42  inline Version nextRelease(void) const;
43  inline Version nextMinor(void) const;
44  inline Version nextMajor(void) const;
45 
46  // Accessors
47  inline int major(void) const;
48  inline int minor(void) const;
49  inline int release(void) const;
50  inline bool accepts(const Version& version) const;
51  inline int compare(const Version& version) const;
52 
53  // Operators
54  inline operator bool(void) const;
55  inline Version& operator=(const Version& version);
56  inline Version& operator=(const char *text) { return *this = string(text); }
57  inline Version& operator=(const cstring text) { return *this = string(text); }
58  Version& operator=(const string& text);
59  inline bool operator==(const Version& version) const;
60  inline bool operator!=(const Version& version) const;
61  inline bool operator>(const Version& version) const;
62  inline bool operator>=(const Version& version) const;
63  inline bool operator<(const Version& version) const;
64  inline bool operator<=(const Version& version) const;
65 private:
66  unsigned char _major;
67  unsigned char _minor;
68  unsigned short _release;
69 };
70 
71 io::Output& operator<<(io::Output& out, const Version& version);
72 
73 // Inlines
74 inline Version::Version(int major, int minor, int release)
75 : _major(major), _minor(minor), _release(release) {
76  ASSERTP(major >= 0, "major number must be positive");
77  ASSERTP(minor >= 0, "minor number must be positive");
78  ASSERTP(release >= 0, "release number be positive");
79 }
80 
81 inline Version::Version(const Version& version)
82 : _major(version._major), _minor(version._minor), _release(version._release) {
83 }
84 
85 inline Version Version::nextRelease(void) const {
86  return Version(_major, _minor, _release + 1);
87 }
88 
89 inline Version Version::nextMinor(void) const {
90  return Version(_major, _minor + 1, 0);
91 }
92 
93 inline Version Version::nextMajor(void) const {
94  return Version(_major + 1, 0, 0);
95 }
96 
97 inline int Version::major(void) const {
98  return _major;
99 }
100 
101 inline int Version::minor(void) const {
102  return _minor;
103 }
104 
105 inline int Version::release(void) const {
106  return _release;
107 }
108 
109 inline bool Version::accepts(const Version& version) const {
110  return _major != version._major || _minor <= version._minor;
111 }
112 
113 inline int Version::compare(const Version& version) const {
114  int res = _major - version._major;
115  if(res)
116  res = _minor - version._minor;
117  return res;
118 }
119 
120 inline Version::operator bool(void) const {
121  return _major || _minor || _release;
122 }
123 
124 inline Version& Version::operator=(const Version& version) {
125  _major = version._major;
126  _minor = version._minor;
127  _release = version._release;
128  return *this;
129 }
130 
131 inline bool Version::operator==(const Version& version) const {
132  return compare(version) == 0;
133 }
134 
135 inline bool Version::operator!=(const Version& version) const {
136  return compare(version) != 0;
137 }
138 
139 inline bool Version::operator>(const Version& version) const {
140  return compare(version) > 0;
141 }
142 
143 inline bool Version::operator>=(const Version& version) const {
144  return compare(version) >= 0;
145 }
146 
147 inline bool Version::operator<(const Version& version) const {
148  return compare(version) < 0;
149 }
150 
151 inline bool Version::operator<=(const Version& version) const {
152  return compare(version) <= 0;
153 }
154 
155 }; // elm
156 
157 #endif // ELM_UTIL_VERSION_H
elm::Version::operator==
bool operator==(const Version &version) const
Definition: Version.h:131
elm::Version::nextMinor
Version nextMinor(void) const
Definition: Version.h:89
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::Version::nextMajor
Version nextMajor(void) const
Definition: Version.h:93
elm::Version::compare
int compare(const Version &version) const
Definition: Version.h:113
elm::Version::operator<
bool operator<(const Version &version) const
Definition: Version.h:147
elm::operator<<
AutoString & operator<<(CString str, const T &value)
Definition: AutoString.h:75
elm::Version::operator<=
bool operator<=(const Version &version) const
Definition: Version.h:151
elm::Version::ZERO
static const Version ZERO
Definition: Version.h:34
elm::Version::operator=
Version & operator=(const char *text)
Definition: Version.h:56
elm::Version::minor
int minor(void) const
Definition: Version.h:101
elm::string
String string
Definition: String.h:149
elm::Version::operator>
bool operator>(const Version &version) const
Definition: Version.h:139
bool
elm::CString
Definition: CString.h:17
elm::Version
Definition: Version.h:32
elm
Definition: adapter.h:26
elm::Version::release
int release(void) const
Definition: Version.h:105
elm::Version::Version
Version(const string &text)
Definition: Version.h:41
elm::Version::Version
Version(int major=0, int minor=0, int release=0)
Definition: Version.h:74
elm::Version::major
int major(void) const
Definition: Version.h:97
elm::Version::nextRelease
Version nextRelease(void) const
Definition: Version.h:85
elm::Version::accepts
bool accepts(const Version &version) const
Definition: Version.h:109
elm::Version::Version
Version(const char *text)
Definition: Version.h:39
elm::Version::operator>=
bool operator>=(const Version &version) const
Definition: Version.h:143
elm::Version::Version
Version(const cstring text)
Definition: Version.h:40
elm::Version::operator=
Version & operator=(const cstring text)
Definition: Version.h:57
elm::Version::operator!=
bool operator!=(const Version &version) const
Definition: Version.h:135
elm::Version::operator=
Version & operator=(const Version &version)
Definition: Version.h:124