Gene
7/12/2011 1:55:00 AM
On Jul 11, 3:01 pm, Kenneth Brody <kenbr...@spamcop.net> wrote:
> On 7/11/2011 12:01 PM, Kenneth Brody wrote:
> [...]
>
> Okay, "final" version, put up for comments/criticisms...
>
> Given the already-existing "MY_VERSION" which is the "hexified" version
> number (ie: 4.6 would be specified as "0x0406") and MY_REVNUM is the patch
> level. (Names changed to protect the innocent.)
>
> #define MY_VERSION_MAJOR (MY_VERSION / 256)
> #define MY_VERSION_MINOR (MY_VERSION % 256)
> #define MY_VERSION_PATCH (MY_REVNUM)
> #define IS_MY_VERSION(major,minor,patch) \
> ( \
> ( major > MY_VERSION_MAJOR ) \
> || \
> ( major == MY_VERSION_MAJOR && minor > MY_VERSION_MINOR ) \
> || \
> ( major == MY_VERSION_MAJOR && minor == MY_VERSION_MINOR && patch >=
> MY_VERSION_PATCH ) \
> )
>
> I can then use, for example:
>
> #if IS_MY_VERSION(5,0,0)
> ... code for 5.0.00 and later ...
> #elif IS_MY_VERSION(4,6,3)
> ... code for 4.6.03 through pre-5.0.00 ...
> #else
> ... pre-4.6.03 code ...
> #endif
Okay, but isn't it more readable to allow general comparisons of
version numbers? Your method implies the direction of comparison, so
at least it should be called IS_MY_VERSION_AT_LEAST().
Or you can just shift the patch level into the existing hex encoding
and use integer comparison as version comparison:
#define MAJOR_VERSION 4
#define MINOR_VERSION 6
#define PATCH_VERSION 0
// Encodings of version information.
#define ENCODE(A, B) (((A) << 4) | (B))
#define VERSION_ENCODING(Major, Minor) \
ENCODE(Major, Minor)
#define VERSION_ENCODING_WITH_PATCH(Major, Minor, Patch) \
ENCODE(VERSION_ENCODING(Major, Minor), Patch)
// Strings denoting version informatino.
#define STRINGIFY(X) #X
#define STRING_ENCODE(A, B) \
STRINGIFY(A) "." STRINGIFY(B)
#define VERSION_STRING(Major, Minor) \
STRING_ENCODE(Major, Minor)
#define VERSION_STRING_WITH_PATCH(Major, Minor, Patch)\
VERSION_STRING(Major, Minor) "." STRINGIFY(Patch)
// Current version encodings and strings.
#define MY_VERSION VERSION_ENCODING(MAJOR_VERSION, MINOR_VERSION)
#define MY_VERSION_STRING VERSION_STRING(MAJOR_VERSION, MINOR_VERSION)
#define MY_VERSION_WITH_PATCH \
VERSION_ENCODING_WITH_PATCH(MAJOR_VERSION, MINOR_VERSION,
PATCH_VERSION)
#define MY_VERSION_STRING_WITH_PATCH \
VERSION_STRING_WITH_PATCH(MAJOR_VERSION, MINOR_VERSION,
PATCH_VERSION)
// Check including patch level.
#if MY_VERSION_WITH_PATCH >= VERSION_ENCODING_WITH_PATCH(5, 0, 0)
.... yada yada
#endif
// Check with no patch level
#if MY_VERSION >= VERSION_ENCODING(6, 9)
.... yada yada
#endif
I've added version string expansions here for fun.