OSDN Git Service

utakataの標準となる例外クラス及びマクロを定義した。
[simplecms/utakata.git] / src / type.h
1 #ifndef _UTAKATA_SRC_TYPE_H_
2 #define _UTAKATA_SRC_TYPE_H_
3
4 #include <string>
5
6 namespace utakata {
7
8 namespace type {
9
10 class Type {
11   // IObject派生クラスに1:1で対応する型のハッシュとして機能
12   // するクラスです。このクラスはInterpreterからのみ生成される
13   // ため、各値の変更は不可能となっています。
14  public:
15   explicit Type(const std::string& name) : hash_(0), hash_string_(name) {
16     MakeHash();
17   }
18   Type(const Type& type);
19   virtual ~Type() {}
20
21   // 渡されたTypeをコピーします。
22   Type& operator=(const Type& type);
23
24   // 内部で生成されたハッシュ数を返します。
25   unsigned int hash() const {return hash_;}
26
27  private:
28
29   // 渡された文字列から、型単位で一意なhashを生成します。
30   void MakeHash();
31
32   // 渡されたtypeとそれぞれの値を交換します。
33   void Swap(Type& type);
34
35   // 生成されたハッシュ値
36   unsigned int hash_;
37   // ハッシュ値の生成元となる文字列
38   std::string hash_string_;
39 };
40
41 // Typeのhashが等しい場合、trueを返します。
42 bool operator==(const Type& lh, const Type& rh) {
43   return lh.hash() == rh.hash();
44 }
45
46 // Typeのhashが異なる場合、trueを返します。
47 bool operator!=(const Type& lh, const Type& rh) {
48   return !(lh.hash() == rh.hash());
49 }
50 };
51 };
52
53 #endif /* _UTAKATA_SRC_TYPE_H_ */