From 73f829d3051f5c6c02ce629991f1757d1586bf74 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 25 Nov 2013 17:21:46 +0200 Subject: [PATCH] Refactor version number extraction and checks --- source/extension.cpp | 48 ++++++++++++++++++++++++++++++-------------- source/extension.h | 6 ++++++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/source/extension.cpp b/source/extension.cpp index 56f96d4e..41b06b21 100644 --- a/source/extension.cpp +++ b/source/extension.cpp @@ -14,6 +14,31 @@ using namespace std; namespace Msp { namespace GL { +Version::Version() +{ + major = 0; + minor = 0; +} + +Version::Version(unsigned short a, unsigned short i) +{ + major = a; + minor = i; +} + +Version::Version(const string &s) +{ + vector parts = split(s, '.'); + major = lexical_cast(parts[0]); + minor = lexical_cast(parts[1]); +} + +bool Version::operator>=(const Version &other) const +{ + return major>other.major || (major==other.major && minor>=other.minor); +} + + Extension::Extension(const char *n, InitFunc f): name(n), init_func(f), @@ -66,28 +91,21 @@ bool is_supported(const string &ext) return extensions.count(ext); } -const Version &get_gl_version() +inline Version _get_gl_version() { - static Version version; - static bool init_done = false; - - if(!init_done) - { - string gl_ver = reinterpret_cast(glGetString(GL_VERSION)); - vector parts = split(gl_ver.substr(0, gl_ver.find(' ')), '.'); - version.major = lexical_cast(parts[0]); - version.minor = lexical_cast(parts[1]); - - init_done = true; - } + string gl_ver = reinterpret_cast(glGetString(GL_VERSION)); + return Version(gl_ver.substr(0, gl_ver.find(' '))); +} +const Version &get_gl_version() +{ + static Version version = _get_gl_version(); return version; } bool is_version_at_least(unsigned a, unsigned b) { - const Version &ver = get_gl_version(); - return (ver.major>a || (ver.major==a && ver.minor>=b)); + return get_gl_version()>=Version(a, b); } ExtFunc *get_proc_address(const string &name) diff --git a/source/extension.h b/source/extension.h index e6c2aca0..df6bd78d 100644 --- a/source/extension.h +++ b/source/extension.h @@ -10,6 +10,12 @@ struct Version { unsigned short major; unsigned short minor; + + Version(); + Version(unsigned short, unsigned short); + Version(const std::string &); + + bool operator>=(const Version &) const; }; -- 2.43.0