2011-12-05 26 views
4

"version"というフィールド呼び出しを宣言する必要がありますが、使用するデータ型が不明です。C++でのソフトウェアバージョンの適切なデータ型は何ですか?

私の "version"は "11.04"のようなものです。私は良い候補者として「ダブル」を考えています。

+11

version.cpp

#include <string> #include <cstdlib> #include <iostream> class Version_Number{ public: //constructors Version_Number(); //<- constructs to 0.0.0 Version_Number(std::string const& version_number); //transformers void Major_Update(); void Minor_Update(); void Bug_Update(); //observers std::string str(); friend std::ostream& operator<<(std::ostream& os, const Version_Number& r); bool operator < (Version_Number const& other) const; bool operator > (Version_Number const& other) const; bool operator == (Version_Number const& other) const; bool operator != (Version_Number const& other) const; private: unsigned int a,b,c; }; 

私は自由のための次のバージョン:) – AraK

+1

を起こっとなっ丸めを想像していますが、これはマーケティングのバージョンまたはエンジニアリング版ですか?何人かのマーケティング担当者は数字の代わりにかわいいバージョン名を思いつくので、_mathはハード_です:( – MSalters

答えて

4

あなたは、非合理的な数に収束するクヌートスタイルのバージョンを使用することを計画している場合を除き、二重はおそらく悪い考えです。 intの配列、文字列の何が問題なのですか?浮動小数点数が十分に正確でないため、

3

doubleは恐ろしい候補になります。

私は、intのメンバーを持つクラスをお勧めします。少数のオペレータに過負荷をかけるだけです。

4

メジャーバージョン番号とマイナーバージョン番号の2つのintを使用します。代わりに、文字列を使用して:)多くの数字が倍精度で正確に表現できないため、doubleは良い候補のようには聞こえません。

2

私はarray<unsigned char, 4>程度を使うと思います。これにより、必要な場合には11.2.1.3のようなものが可能になりますが、それでも倍以上のスペースを使います。 バージョン番号の個々のコンポーネントがまだ255を超えているので、 のバージョン番号を持つ個々のコンポーネントは十分にまれであるため、各コンポーネントにcharを使用することは実際には限界になるとは思えません。明白な例外は、バージョン番号に毎日のビルド番号を埋め込むことにした場合です。

あなたが本当に汎用性が必要な場合は、あなたが何か行うことができます。これで一つだけのキャッチあります

typedef unsigned char component_t; 

array<component_t, 4> version; 

:それを読むために、あなたはcomponent_tが何であるかを知っておく必要があります。これを処理する明白な方法は、あなたが使用しているバージョン番号のバージョンを知らせる1バイトのバージョン番号を持つことです。そうすれば、いつでもcomponent_t(または使用可能なコンポーネントの数)を変更するだけで増やすことができます! :-)

+0

@ildjarn:さて、私は少し言い直しました。 –

2

intとlet 1000はバージョン1.0に対応しています。

1

いくつかのライブラリのようなトリックを使用します。

#define PKG_MAJOR (3) // example values... 
#define PKG_MINOR (7) 
#define PKG_MICRO (11) 

const unsigned long pkg_version = (PKG_MAJOR * 1000 + PKG_MINOR) * 1000 + PKG_MICRO; 

すなわち、pkg_versionは何unsigned intまたは、そのようなを使用する方法について3007011.

1

のですか?私はちょうどあなたがカーネルのバージョンa、メジャーバージョンbとマイナーバージョンcを持ってlinux kernel Makefile

#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) 

でこれを見てきました。 2つのバージョンの順序を簡単に比較できます。印刷可能な形式で印刷したい場合は、バージョン文字列を作成する関数を追加することができます。 "2.6.38"。 (もちろん、あなたが最大に制限されている256のサブバージョン... unsigned long longを使用します。?)

2

私はsemantic versioningを使用するので、私は次のユーザー定義型を作成しました:

バージョンを。HPP

#include <string> 
#include <cstdlib> 
#include <iostream> 

#include "version_number.hpp" 

    Version_Number::Version_Number(): a(0),b(0),c(0){} 

    Version_Number::Version_Number(std::string const& folder_name){ 

     std::string a_str,b_str,c_str; 

     auto it = folder_name.begin(); 
      while (*it != '.'){ 
       a_str+=*it; 
       ++it; 
      } 
      ++it; 
      while (*it != '.'){ 
       b_str+=*it; 
       ++it; 
      } 
      ++it; 
      while (it != folder_name.end()){ 
       c_str+=*it; 
       ++it; 
      } 

      a = std::atoi(a_str.c_str()); 
      b = std::atoi(b_str.c_str()); 
      c = std::atoi(c_str.c_str()); 
    } 

    void Version_Number::Major_Update(){ 
     ++a; 
     b = 0; 
     c = 0; 
     return; 
    } 
    void Version_Number::Minor_Update(){ 
     ++b; 
     c = 0; 
     return; 
    } 
    void Version_Number::Bug_Update(){ 
     ++c; 
     return; 
    } 

    std::string Version_Number::str(){ 
     std::string str; 

     str+= std::to_string(a); 
     str+='.'; 
     str+= std::to_string(b); 
     str+='.'; 
     str+= std::to_string(c); 

     return str; 
    } 
bool Version_Number::operator < (Version_Number const& other) const{ 
    if (a > other.a){return false;} 
    if (a < other.a){return true;} 
    if (b > other.b){return false;} 
    if (b < other.b){return true;} 
    if (c > other.c){return false;} 
    if (c < other.c){return true;} 
    return false; 
} 

bool Version_Number::operator > (Version_Number const& other) const{ 
    if (a < other.a){return false;} 
    if (a > other.a){return true;} 
    if (b < other.b){return false;} 
    if (b > other.b){return true;} 
    if (c < other.c){return false;} 
    if (c > other.c){return true;} 
    return false; 
} 

bool Version_Number::operator == (Version_Number const& other) const{ 
    if (a == other.a){ 
     if (b == other.b){ 
      if (c == other.c){ 
       return true; 
      } 
     } 
    } 

    return false; 
} 
bool Version_Number::operator != (Version_Number const& other) const{ 
    if (a == other.a){ 
     if (b == other.b){ 
      if (c == other.c){ 
       return false; 
      } 
     } 
    } 

    return true; 
} 

std::ostream& operator<<(std::ostream& os, const Version_Number& r){ 
    os << r.a << '.' << r.b << '.' << r.c; 
    return os; 
} 
関連する問題