Archived

This topic is now archived and is closed to further replies.

original vesoljc

class Version

Recommended Posts

i''m doing this simple class for tracking versions. base format of the version is like this: MAJOR.MINOR.BUILD anyway, i''m still struggling with const correct stuff and references and need some advice/check up header
  
#ifndef _AXP_VERSION_H_
#define _AXP_VERSION_H_

// SYSTEM INCLUDES

// PROJECT INCLUDES

#include "..\axpBase\axpBase.hpp"
// LOCAL INCLUDES


namespace axp
{

	class   Version
	{

	public:
	// LIFECYCLE

								Version				();
								Version				(const axpUInt16&,
													 const axpUInt16&,
													 const axpUInt16&);
								Version				(const Version&);
		 virtual                ~Version			();

	// OPERATORS

		axpBool					operator==			(const Version&) const;
		axpBool					operator<=			(const Version&) const;
		axpBool					operator>=			(const Version&) const;
		axpBool					operator<			(const Version&) const;
		axpBool					operator>			(const Version&) const;


	// OPERATIONS

	// ACCESS

		inline	axpVoid			Major				(const axpUInt16&);
		inline	axpVoid			Minor				(const axpUInt16&);
		inline	axpVoid			Build				(const axpUInt16&);

	// INQUIRY

		 inline axpUInt16&		Major				();
		 inline axpUInt16&		Minor				();
		 inline axpUInt16&		Build				();
		 inline	axpString		String				();

	// VARIABLES


	protected:
	// LIFECYCLE

	// OPERATORS

	// OPERATIONS

	// ACCESS

	// INQUIRY

	// VARIABLES


	private:
	// LIFECYCLE

	// OPERATORS

	// OPERATIONS


		inline	axpUInt64		VersionValue		() const;

	// ACCESS

	// INQUIRY

	// VARIABLES

		axpUInt16				mMajor;
		axpUInt16				mMinor;
		axpUInt16				mBuild;
	};

	#include "axpVersion.inl"

} // namespace axp


#endif // _AXP_VERSION_H_

  
inline
  
//////////////////// PUBLIC /////////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================


inline	axpVoid	Version::Major(const axpUInt16& lMajor)
{
	mMajor			= lMajor;
}

inline	axpVoid	Version::Minor(const axpUInt16& lMinor)
{
	mMinor			= lMinor;
}

inline	axpVoid	Version::Build(const axpUInt16& lBuild)
{
	mBuild			= lBuild;
}

//================= INQUIRY =================================================



inline axpUInt16& Version::Major() 
{
	return(mMajor);
}

inline axpUInt16& Version::Minor() 
{
	return(mMinor);
}

inline axpUInt16& Version::Build()
{
	return(mBuild);
}

inline	axpString	Version::String()
{
	axpString		lString;
	axpChar			lBuffer[8];

	_itoa(mMajor,lBuffer,10);
	lString.append(lBuffer);
	lString.append(".");
	_itoa(mMinor,lBuffer,10);
	lString.append(lBuffer);
	lString.append(".");
	_itoa(mBuild,lBuffer,10);
	lString.append(lBuffer);
	return(lString);
}


/////////////////// PROTECTED ///////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================

//================= INQUIRY =================================================


/////////////////// PRIVATE /////////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================


inline axpUInt64 Version::VersionValue() const
{
	return(mBuild + mMinor * 100000 + mMajor * 100000000000);
}

//================= ACCESS ==================================================

//================= INQUIRY =================================================  
source
  
#include "axpVersion.hpp"

using namespace axp;

//////////////////// PUBLIC /////////////////////////////////////////////////

//================= LIFECYCLE ===============================================


Version::Version()
{
	mMajor			= 0;
	mMinor			= 0;
	mBuild			= 0;
}

Version::Version(const axpUInt16& lMajor, 
				 const axpUInt16& lMinor,
				 const axpUInt16& lBuild)
{
	mMajor			= lMajor;
	mMinor			= lMinor;
	mBuild			= lBuild;
}

Version::Version(const Version& lLeft)
{
	mMajor			= lLeft.mMajor;
	mMinor			= lLeft.mMinor;
	mBuild			= lLeft.mBuild;
}

Version::~Version()
{

}

//================= OPERATORS ===============================================


axpBool	Version::operator == (const Version& lRight) const
{
	return( VersionValue() == lRight.VersionValue() );
}

axpBool	Version::operator <= (const Version& lRight) const
{
	return( VersionValue() <= lRight.VersionValue() );
}

axpBool	Version::operator >= (const Version& lRight) const
{
	return( VersionValue() >= lRight.VersionValue() );
}

axpBool	Version::operator < (const Version& lRight) const
{
	return( VersionValue() < lRight.VersionValue() );
}

axpBool	Version::operator > (const Version& lRight) const
{
	return( VersionValue() > lRight.VersionValue() );
}

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================

//================= INQUIRY =================================================


/////////////////// PROTECTED ///////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================

//================= INQUIRY =================================================


/////////////////// PRIVATE /////////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================

//================= INQUIRY =================================================

//================= INQUIRY =================================================


  
so... what am i doing wrong, what correct and what could be improved???

Share this post


Link to post
Share on other sites
I would do

const axpUInt16& Version::Major() const;
...



Why do you need axpVoid instead of simply void?

Share this post


Link to post
Share on other sites
yes all methods that doesnt affect the class members should be const.

[edited by - pag on May 27, 2003 12:52:04 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sprintf(buf,"%i.%i.%i",major,minor,build);

it looks alot nicer

Share this post


Link to post
Share on other sites
update


header

  
#ifndef _AXP_VERSION_H_
#define _AXP_VERSION_H_

// SYSTEM INCLUDES

// PROJECT INCLUDES

#include "..\axpBase\axpBase.hpp"
// LOCAL INCLUDES


namespace axp
{

class Version
{

public:
// LIFECYCLE

inline Version ();
inline Version (const axpUInt16&,
const axpUInt16&,
const axpUInt16&);
inline Version (const Version&);
inline virtual ~Version ();

// OPERATORS

inline axpBool operator== (const Version&) const;
inline axpBool operator<= (const Version&) const;
inline axpBool operator>= (const Version&) const;
inline axpBool operator< (const Version&) const;
inline axpBool operator> (const Version&) const;


// OPERATIONS

// ACCESS

inline axpVoid Major (const axpUInt16&);
inline axpVoid Minor (const axpUInt16&);
inline axpVoid Build (const axpUInt16&);

// INQUIRY

inline const axpUInt16& Major () const;
inline const axpUInt16& Minor () const;
inline const axpUInt16& Build () const;
inline const axpString String () const;

// VARIABLES


protected:
// LIFECYCLE

// OPERATORS

// OPERATIONS

// ACCESS

// INQUIRY

// VARIABLES


private:
// LIFECYCLE

// OPERATORS

// OPERATIONS


inline const axpUInt64 VersionValue () const;

// ACCESS

// INQUIRY

// VARIABLES

axpUInt16 mMajor;
axpUInt16 mMinor;
axpUInt16 mBuild;
};

#include "axpVersion.inl"

} // namespace axp


#endif // _AXP_VERSION_H_



inline

  
//////////////////// PUBLIC /////////////////////////////////////////////////

//================= LIFECYCLE ===============================================


inline Version::Version()
{
mMajor = 0;
mMinor = 0;
mBuild = 0;
}

inline Version::Version(const axpUInt16& lMajor,
const axpUInt16& lMinor,
const axpUInt16& lBuild)
{
mMajor = lMajor;
mMinor = lMinor;
mBuild = lBuild;
}

inline Version::Version(const Version& lRight)
{
mMajor = lRight.mMajor;
mMinor = lRight.mMinor;
mBuild = lRight.mBuild;
}

inline Version::~Version()
{

}

//================= OPERATORS ===============================================


inline axpBool Version::operator == (const Version& lRight) const
{
return( VersionValue() == lRight.VersionValue() );
}

inline axpBool Version::operator <= (const Version& lRight) const
{
return( VersionValue() <= lRight.VersionValue() );
}

inline axpBool Version::operator >= (const Version& lRight) const
{
return( VersionValue() >= lRight.VersionValue() );
}

inline axpBool Version::operator < (const Version& lRight) const
{
return( VersionValue() < lRight.VersionValue() );
}

inline axpBool Version::operator > (const Version& lRight) const
{
return( VersionValue() > lRight.VersionValue() );
}

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================


inline axpVoid Version::Major(const axpUInt16& lMajor)
{
mMajor = lMajor;
}

inline axpVoid Version::Minor(const axpUInt16& lMinor)
{
mMinor = lMinor;
}

inline axpVoid Version::Build(const axpUInt16& lBuild)
{
mBuild = lBuild;
}

//================= INQUIRY =================================================



inline const axpUInt16& Version::Major() const
{
return(mMajor);
}

inline const axpUInt16& Version::Minor() const
{
return(mMinor);
}

inline const axpUInt16& Version::Build() const
{
return(mBuild);
}

inline const axpString Version::String() const
{
axpString lString;
axpChar lBuffer[30];

sprintf(lBuffer,"%d.%d.%d",mMajor,mMinor,mBuild);

lString.append(lBuffer);
return(lString);
}


/////////////////// PROTECTED ///////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================

//================= ACCESS ==================================================

//================= INQUIRY =================================================


/////////////////// PRIVATE /////////////////////////////////////////////////

//================= LIFECYCLE ===============================================

//================= OPERATORS ===============================================

//================= OPERATIONS ==============================================


inline const axpUInt64 Version::VersionValue() const
{
return(mBuild + mMinor * 100000 + mMajor * 100000000000);
}

//================= ACCESS ==================================================

//================= INQUIRY =================================================



anything else?

would it be better to make a member variable smthing like
axpString mString;
and rebuild this one on every call to String() and return it as an reference?

Share this post


Link to post
Share on other sites
I don''t think that return a reference for String() be a good idea.

Functions for upgrade/reverse(major.minor.build) version would be interesting.

Maybe a class VersionDistance to calculate differences between versions etc... ???????

Share this post


Link to post
Share on other sites