views:

45

answers:

1

We're distributing a Cocoa framework with regular updates. We'll be updating the version numbers with each release. The Apple documentation appears to suggest that version numbers should be consecutive incrementing integers. We are distributing output in several formats, and the framework is only one of them. We would rather not have to maintain a separate numbering system just for our frameworks.

We don't really care about the precise format of the version numbers of the framework, so long as they change whenever the product changes, and behave in a correct, sensible and expected manner. I'm looking for a way of avoiding having to run a separate version number counter.

One suggestion is that for product version 12.34.56 we could simply remove the dots and say the framework version is 123456 (with appropriate zero padding).

  • Is there a constraint on the type of number that can be represented (uint? long?)
  • Does it have to be a number? Could it be a string?
  • Do the numbers have to be consecutive?
  • Is there a standard way of doing things in this situation?
+4  A: 

As I understand it, the reason for this requirement is so that you can have macro checks like:

#if FRAMEWORKNAME_VERSION >= 123456
    // some stuff
#else
    // some other stuff
#endif

The numbers do not need to be consecutive, and your suggested scheme is quite sensible:

#define MAKE_VERSION(MAJOR,MINOR,PATCH) ((MAJOR*10000)+(MINOR*100)+PATCH)

I would also suggest that in addition to defining a version, you also define constants for each version...

#define FRAMEWORKNAME_VERSION_1_0_0 MAKE_VERSION(1,0,0)
#define FRAMEWORKNAME_VERSION_1_0_1 MAKE_VERSION(1,0,1)

That way, you can check in multiple ways... either:

#if FRAMEWORKNAME_VERSION >= MAKE_VERSION(1,0,1)
    // 1.0.1 and later
#else
    // Before 1.0.1
#endif

Or:

#if defined(FRAMEWORKNAME_VERSION_1_0_1)
    // 1.0.1 and later
#else
    // Before 1.0.1
#endif

The key requirements that you should satisfy are:

  • The numbers are monotonically increasing
  • The numbers are predictable
  • The numbers are easily comparable

If you want to supply a string representation of your version in addition to the integer representation, by all means go ahead; however, I would strongly suggest that you have an integer representation available, as it simplifies comparison and allows the version to be checked in the preprocessor.

Michael Aaron Safyan
You can make a string representation of each one using the double-stringize trick: http://everything2.com/title/stringize+macro+macro+hack I'm not entirely sure how it works in C99-definition terms, though. I've asked a question about that: http://stackoverflow.com/questions/2751870/how-exactly-does-the-double-stringize-trick-work
Peter Hosey
@Peter, you could stringize it, but then it wouldn't have the decimal points.
Michael Aaron Safyan