mrDIMAS

Untitled

Dec 17th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. *******************
  2. .h
  3. class DamageClass {
  4. public:
  5.   enum Type { Unknown, Physical, Chemical, Electrical, Fire, Count };
  6.  
  7.   static unordered_map<string, Type> Names;
  8. private:
  9.   Type mValue{Unknown};
  10.  
  11. public:
  12.   DamageClass() {
  13.   }
  14.   DamageClass(const string &str) {
  15.     auto iter = Names.find(str);
  16.     if (iter == Names.end()) {
  17.       throw runtime_error("Invalid damage class: " + str);
  18.     }
  19.     mValue = iter->second;
  20.   }
  21.   DamageClass(const Type &type) : mValue(type) {
  22.   }
  23.   bool operator==(const Type &other) const {
  24.     return mValue == other;
  25.   }
  26.   bool operator==(const DamageClass &other) const {
  27.     return mValue == other.mValue;
  28.   }
  29.   operator Type() const {
  30.     return mValue;
  31.   }
  32.   void Serialize(Serializer &sav) {
  33.     sav &mValue;
  34.   }
  35. };
  36.  
  37. *****************
  38. .cpp
  39.  
  40. #define ENTRY(x) {#x, x}
  41.  
  42. unordered_map<string, DamageClass::Type> DamageClass::Names = {
  43.   ENTRY(Unknown),
  44.   ENTRY(Physical),
  45.   ENTRY(Chemical),
  46.   ENTRY(Electrical),
  47.   ENTRY(Fire),
  48.   ENTRY(Count)
  49. };
Advertisement
Add Comment
Please, Sign In to add comment