summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Glöckner <cgloeckner@freenet.de>2016-03-29 17:51:37 +0200
committerAndré Nusser <andre.nusser@googlemail.com>2016-03-31 17:49:56 +0200
commit9b5dae389051c8c9ea96c1f79b487169d19b46d8 (patch)
tree16ad2a4ab696e4f08048065dc977069d1351aa1c /src
parent68dc4b912f0a950a9db4987432c559dd1b0a0ebf (diff)
Refactored class VersionStr
Diffstat (limited to 'src')
-rw-r--r--src/versionstr.cc153
-rw-r--r--src/versionstr.h114
2 files changed, 148 insertions, 119 deletions
diff --git a/src/versionstr.cc b/src/versionstr.cc
index 48764ba..604d7c1 100644
--- a/src/versionstr.cc
+++ b/src/versionstr.cc
@@ -31,7 +31,8 @@
#include <stdlib.h>
#include <stdio.h>
-// Workaround - major, minor and patch are defined as macros when using _GNU_SOURCES
+// Workaround - major, minor and patch are defined as macros when using
+// _GNU_SOURCES
#ifdef major
#undef major
#endif
@@ -42,110 +43,138 @@
#undef patch
#endif
-VersionStr::VersionStr(std::string v) throw(const char *)
+VersionStr::VersionStr(const std::string& v) throw(const char*)
{
- memset(version, 0, sizeof(version));
- set(v);
+ memset(version, 0, sizeof(version));
+ set(v);
}
VersionStr::VersionStr(size_t major, size_t minor, size_t patch)
{
- version[0] = major;
- version[1] = minor;
- version[2] = patch;
+ version[0] = major;
+ version[1] = minor;
+ version[2] = patch;
}
-void VersionStr::set(std::string v) throw(const char *)
+void VersionStr::set(const std::string& v) throw(const char*)
{
- std::string num;
- size_t idx = 0;
- for(size_t i = 0; i < v.length(); i++) {
- if(v[i] == '.') {
- if(idx > 2) throw "Version string is too long.";
- version[idx] = atoi(num.c_str());
- idx++;
- num = "";
- } else if(v[i] >= '0' && v[i] <= '9') {
- num.append(1, v[i]);
- } else {
- throw "Version string contains illegal character.";
- }
- }
- if(idx > 2) throw "Version string is too long.";
- version[idx] = atoi(num.c_str());
+ std::string num;
+ size_t idx = 0;
+ for(size_t i = 0; i < v.length(); i++)
+ {
+ if(v[i] == '.')
+ {
+ if(idx > 2)
+ {
+ throw "Version string is too long.";
+ }
+ version[idx] = atoi(num.c_str());
+ idx++;
+ num = "";
+ }
+ else if(v[i] >= '0' && v[i] <= '9')
+ {
+ num.append(1, v[i]);
+ }
+ else
+ {
+ throw "Version string contains illegal character.";
+ }
+ }
+ if(idx > 2)
+ {
+ throw "Version string is too long.";
+ }
+ version[idx] = atoi(num.c_str());
}
VersionStr::operator std::string() const
{
- std::string v;
- char buf[64];
- if(patch()) {
- sprintf(buf, "%d.%d.%d", (int)major(), (int)minor(), (int)patch());
- } else {
- sprintf(buf, "%d.%d", (int)major(), (int)minor());
- }
- v = buf;
- return v;
+ std::string v;
+ char buf[64];
+ if(patch())
+ {
+ sprintf(buf, "%d.%d.%d", (int)major(), (int)minor(), (int)patch());
+ }
+ else
+ {
+ sprintf(buf, "%d.%d", (int)major(), (int)minor());
+ }
+ v = buf;
+ return v;
}
-
-void VersionStr::operator=(std::string v) throw(const char *)
+
+void VersionStr::operator=(const std::string& v) throw(const char*)
{
- set(v);
+ set(v);
}
// return a - b simplified as -1, 0 or 1
-static int vdiff(const VersionStr &a, const VersionStr &b)
+static int vdiff(const VersionStr& a, const VersionStr& b)
{
- if(a.major() < b.major()) return -1;
- if(a.major() > b.major()) return 1;
- if(a.minor() < b.minor()) return -1;
- if(a.minor() > b.minor()) return 1;
- if(a.patch() < b.patch()) return -1;
- if(a.patch() > b.patch()) return 1;
- return 0;
+ if(a.major() < b.major())
+ {
+ return -1;
+ }
+ if(a.major() > b.major())
+ {
+ return 1;
+ }
+ if(a.minor() < b.minor())
+ {
+ return -1;
+ }
+ if(a.minor() > b.minor())
+ {
+ return 1;
+ }
+ if(a.patch() < b.patch())
+ {
+ return -1;
+ }
+ if(a.patch() > b.patch())
+ {
+ return 1;
+ }
+ return 0;
}
-bool VersionStr::operator<(const VersionStr &other) const
+bool VersionStr::operator<(const VersionStr& other) const
{
- if(vdiff(*this, other) == -1) return true;
- return false;
+ return vdiff(*this, other) == -1;
}
-bool VersionStr::operator>(const VersionStr &other) const
+bool VersionStr::operator>(const VersionStr& other) const
{
- if(vdiff(*this, other) == 1) return true;
- return false;
+ return vdiff(*this, other) == 1;
}
-bool VersionStr::operator==(const VersionStr &other) const
+bool VersionStr::operator==(const VersionStr& other) const
{
- if(vdiff(*this, other) == 0) return true;
- return false;
+ return vdiff(*this, other) == 0;
}
-bool VersionStr::operator<=(const VersionStr &other) const
+bool VersionStr::operator<=(const VersionStr& other) const
{
- if(vdiff(*this, other) != 1) return true;
- return false;
+ return vdiff(*this, other) != 1;
}
-bool VersionStr::operator>=(const VersionStr &other) const
+bool VersionStr::operator>=(const VersionStr& other) const
{
- if(vdiff(*this, other) != -1) return true;
- return false;
+ return vdiff(*this, other) != -1;
}
size_t VersionStr::major() const
{
- return version[0];
+ return version[0];
}
size_t VersionStr::minor() const
{
- return version[1];
+ return version[1];
}
size_t VersionStr::patch() const
{
- return version[2];
+ return version[2];
}
diff --git a/src/versionstr.h b/src/versionstr.h
index 9cdd056..f168332 100644
--- a/src/versionstr.h
+++ b/src/versionstr.h
@@ -25,12 +25,12 @@
* along with DrumGizmo; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#ifndef __PRACRO_VERSIONSTR_H__
-#define __PRACRO_VERSIONSTR_H__
+#pragma once
#include <string>
-// Workaround - major, minor and patch are defined as macros when using _GNU_SOURCES
+// Workaround - major, minor and patch are defined as macros when using
+// _GNU_SOURCES
#ifdef major
#undef major
#endif
@@ -43,70 +43,70 @@
/**
* VersionStr class.
- * It hold a version number and is capable of correct sorting, as well as string
+ * It hold a version number and is capable of correct sorting, as well as string
* conversion both ways.
*/
-class VersionStr {
+class VersionStr
+{
public:
- /**
- * Constructor.
- * Throws an exeption if the string does not parse.
- * @param v A std::string containing a version string on the form a.b or a.b.c
- */
- VersionStr(std::string v) throw(const char *);
+ /**
+ * Constructor.
+ * Throws an exeption if the string does not parse.
+ * @param v A std::string containing a version string on the form a.b or
+ * a.b.c
+ */
+ VersionStr(const std::string& v) throw(const char*);
- /**
- * Constructor.
- * @param major A size_t containing the major version number.
- * @param minor A size_t containing the minor version number.
- * @param patch A size_t containing the patch level.
- */
- VersionStr(size_t major = 0, size_t minor = 0, size_t patch = 0);
+ /**
+ * Constructor.
+ * @param major A size_t containing the major version number.
+ * @param minor A size_t containing the minor version number.
+ * @param patch A size_t containing the patch level.
+ */
+ VersionStr(size_t major = 0, size_t minor = 0, size_t patch = 0);
- /**
- * Typecast to std::string operator.
- * It simply converts the version numbers into a string of the form major.minor
- * (if patch i 0) or major.minor.patch
- */
- operator std::string() const;
+ /**
+ * Typecast to std::string operator.
+ * It simply converts the version numbers into a string of the form
+ * major.minor
+ * (if patch i 0) or major.minor.patch
+ */
+ operator std::string() const;
- /**
- * Assignment from std::string operator.
- * Same as in the VersionStr(std::string v) constructor.
- * Throws an exeption if the string does not parse.
- */
- void operator=(std::string v) throw(const char *);
+ /**
+ * Assignment from std::string operator.
+ * Same as in the VersionStr(std::string v) constructor.
+ * Throws an exeption if the string does not parse.
+ */
+ void operator=(const std::string& v) throw(const char*);
- /**
- * Comparison operator.
- * The version objects are sorted according to their major, minor and patch
- * level numbers.
- */
- bool operator<(const VersionStr &other) const;
- bool operator==(const VersionStr &other) const;
- bool operator>(const VersionStr &other) const;
- bool operator>=(const VersionStr &other) const;
- bool operator<=(const VersionStr &other) const;
+ /**
+ * Comparison operator.
+ * The version objects are sorted according to their major, minor and patch
+ * level numbers.
+ */
+ bool operator<(const VersionStr& other) const;
+ bool operator==(const VersionStr& other) const;
+ bool operator>(const VersionStr& other) const;
+ bool operator>=(const VersionStr& other) const;
+ bool operator<=(const VersionStr& other) const;
+ /**
+ * @return Major version number.
+ */
+ size_t major() const;
- /**
- * @return Major version number.
- */
- size_t major() const;
+ /**
+ * @return Minor version number.
+ */
+ size_t minor() const;
- /**
- * @return Minor version number.
- */
- size_t minor() const;
-
- /**
- * @return Patch level.
- */
- size_t patch() const;
+ /**
+ * @return Patch level.
+ */
+ size_t patch() const;
private:
- void set(std::string v) throw(const char *);
- size_t version[3];
+ void set(const std::string& v) throw(const char*);
+ size_t version[3];
};
-
-#endif/*__PRACRO_VERSIONSTR_H__*/