C++ Code: A lightweight Version Comparison class

This class is used for comparing version info in the form of "Major.Minor.Revision.Build".


// VersionInfo.h - Declararion of CVersionInfo 

class CVersionInfo
{
public:
    CVersionInfo(wstring s);
    CVersionInfo(unsigned int major,
    unsigned int minor,
    unsigned int revision,
    unsigned int build);

public: 
    wstring to_string() const;
    bool operator< (const CVersionInfo& vi);
    bool operator> (const CVersionInfo& vi);
    bool operator== (const CVersionInfo& vi);

private:
    unsigned int m_major;
    unsigned int m_minor;
    unsigned int m_revision;
    unsigned int m_build;
};


// VersionInfo.cpp - implementations of CVersionInfo 

CVersionInfo::CVersionInfo(unsigned int major,
        unsigned int minor, 
        unsigned int revision,
        unsigned int build)
        :m_major(major)
        ,m_minor(minor)
        ,m_revision(revision)
        ,m_build(build)
{} 

CVersionInfo::CVersionInfo(std::wstring s)
        :m_major(0)
        ,m_minor(0)
        ,m_revision(0)
        ,m_build(0)
{
    vector<wstring> vS;
    wstringstream ss;
    boost::algorithm::split(vS, s, boost::algorithm::is_any_of(L".")); 

    switch(vS.size())
    {
        case 4:
            ss.str(vS[3]); ss>>std::hex>>m_build; ss.clear();
        case 3:
            ss.str(vS[2]); ss>>std::hex>>m_revision; ss.clear();
        case 2:
            ss.str(vS[1]); ss>>std::hex>>m_minor; ss.clear();
        case 1:
            ss.str(vS[0]); ss>>std::hex>>m_major; ss.clear();
        default:
            ;
    }
} 

bool CVersionInfo::operator< (const CVersionInfo& vi)
{
    if(m_major != vi.m_major) return m_major < vi.m_major; 
    if(m_minor != vi.m_minor) return m_minor < vi.m_minor; 
    if(m_revision != vi.m_revision) return m_revision < vi.m_revision; 
    if(m_build != vi.m_build) return m_build < vi.m_build; 

    return false;
} 

bool CVersionInfo::operator> (const CVersionInfo& vi)
{
    if(m_major != vi.m_major) return m_major > vi.m_major; 
    if(m_minor != vi.m_minor) return m_minor > vi.m_minor; 
    if(m_revision != vi.m_revision) return m_revision > vi.m_revision; 
    if(m_build != vi.m_build) return m_build > vi.m_build; 

    return false;
} 

bool CVersionInfo::operator== (const CVersionInfo& vi)
{
    if((m_major == vi.m_major) 
        && (m_minor == vi.m_minor)
        && (vi.m_revision == vi.m_build)) 
        return true; 

    return false;
} 

wstring CVersionInfo::to_string() const
{
    wstringstream ss; 

    ss << m_major
       << L"."
       << m_minor
       << L"."
       << m_revision
       << L"."
       << m_build; 

    return ss.str();
}

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: