Use an array not a vector for components

This commit is contained in:
Danny Robson 2012-07-30 16:32:32 +10:00
parent 20e8297768
commit 1c251699c3
2 changed files with 24 additions and 25 deletions

View File

@ -32,25 +32,26 @@ using namespace util;
version::version (unsigned int _major, version::version (unsigned int _major,
unsigned int _minor): unsigned int _minor):
m_values (2), m_size (2),
m_release (RELEASE_PRODUCTION) { m_release (RELEASE_PRODUCTION)
{
m_values[OFFSET_MAJOR] = _major; m_values[OFFSET_MAJOR] = _major;
m_values[OFFSET_MINOR] = _minor; m_values[OFFSET_MINOR] = _minor;
} }
version::version (const string& str): version::version (const string& str):
m_values (NUM_OFFSETS, 0), m_size (0),
m_release (RELEASE_PRODUCTION) { m_release (RELEASE_PRODUCTION)
m_values.clear (); {
parse (str); parse (str);
} }
version::version (const char *str): version::version (const char *str):
m_values (NUM_OFFSETS, 0), m_size (0),
m_release (RELEASE_PRODUCTION) { m_release (RELEASE_PRODUCTION)
m_values.clear (); {
parse (str); parse (str);
} }
@ -72,7 +73,7 @@ check_release (version::release_t r) {
void void
version::sanity (void) const { version::sanity (void) const {
check_release (m_release); check_release (m_release);
CHECK (!m_values.empty ()); CHECK (m_size > 0);
} }
@ -85,7 +86,7 @@ version::sanity (void) const {
{ current *= 10; { current *= 10;
current += (uintptr_t)(fc - (unsigned char)'0'); } current += (uintptr_t)(fc - (unsigned char)'0'); }
action finish action finish
{ m_values.push_back (current); } { m_values[m_size++] = current; }
number = (digit+) number = (digit+)
>clear >clear

View File

@ -17,11 +17,11 @@
* Copyright 2010 Danny Robson <danny@nerdcruft.net> * Copyright 2010 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __VERSION_HPP #ifndef __UTIL_VERSION_HPP
#define __VERSION_HPP #define __UTIL_VERSION_HPP
#include <array>
#include <string> #include <string>
#include <vector>
#include <iostream> #include <iostream>
@ -54,22 +54,20 @@ namespace util {
NUM_OFFSETS NUM_OFFSETS
}; };
std::vector <unsigned int> m_values; std::array<unsigned int, NUM_OFFSETS> m_values;
size_t m_size;
release_t m_release; release_t m_release;
void parse (const std::string&); void parse (const std::string&);
void parse (const char*); void parse (const char*);
public: public:
unsigned int major (void) const { return m_values[OFFSET_MAJOR]; }
unsigned int minor (void) const { return m_values[OFFSET_MINOR]; }
unsigned int point (void) const { return m_values[OFFSET_POINT]; }
unsigned int build (void) const { return m_values[OFFSET_BUILD]; }
unsigned int major (void) const size_t size (void) const { return m_size; }
{ return m_values[OFFSET_MAJOR]; }
unsigned int minor (void) const
{ return m_values[OFFSET_MINOR]; }
unsigned int point (void) const
{ return m_values[OFFSET_POINT]; }
unsigned int build (void) const
{ return m_values[OFFSET_BUILD]; }
bool operator < (const version& rhs) const; bool operator < (const version& rhs) const;
bool operator > (const version& rhs) const; bool operator > (const version& rhs) const;