Guest User

Approach

a guest
Sep 4th, 2015
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. Rust:
  2.  
  3. enum Message {
  4.     Quit,
  5.     ChangeColor(i32, i32, i32),
  6.     Move { x: i32, y: i32 },
  7.     Write(String),
  8. }
  9.  
  10. C++:
  11.  
  12. class MsgType {
  13.   enum InMsgType {
  14.     T_Quit,
  15.     T_ChangeColor,
  16.     T_Move,
  17.     T_Write,
  18.   };
  19.   struct Quit { enum { type = T_Quit } };
  20.   struct ChangeColor { enum { type = T_ChangeColor } };
  21.   struct Move { enum { type = T_Move } };
  22.   struct Write { enum { type = T_Write } };
  23. };
  24.  
  25. struct ChangeColorData {
  26.   int32_t a;
  27.   int32_t b;
  28.   int32_t c;
  29. };
  30.  
  31. struct MoveData {
  32.   int32_t x;
  33.   int32_t y;
  34. };
  35.  
  36. struct WriteData {
  37.   std::string a;
  38. };
  39.  
  40. class Message {
  41. private:
  42.   MsgType::InMsgType TagType;
  43.   std::variant<ChangeColorData, MoveData, WriteData> VarData;
  44.  
  45. public:
  46.   Message(MsgType::Quit) :
  47.     TagType(MsgType::Quit::type) {}
  48.   Message(MsgType::ChangeColor, ChangeColorData data) :
  49.     TagType(MsgType::ChangeColor::type), VarData(data) {}
  50.   Message(MsgType::Move, MoveData data) :
  51.     TagType(MsgType::Move::type), VarData(data) {}
  52.   Message(MsgType::Write, WriteData data) :
  53.     TagType(MsgType::Write::type), VarData(data) {}
  54.  
  55.   ~Message() = default;
  56.  
  57.   MsgType::InMsgType getType() const {
  58.     return TagType;
  59.   }
  60.  
  61.   ChangeColorData getChangeColor() const {
  62.     return std::get<ChangeColorData>(VarData);
  63.   }
  64.  
  65.   MoveData getMoveData() const {
  66.     return std::get<MoveData>(VarData);
  67.   }
  68.  
  69.   WriteData getWriteData() const {
  70.     return std::get<WriteData>(VarData);
  71.   }
  72. };
  73.  
  74. Message DoSth(bool cond) {
  75.   if(cond)
  76.     return { MsgType::Quit };
  77.   else
  78.     return { MsgType::Move, { 12, 3 } };
  79. }
Advertisement
Add Comment
Please, Sign In to add comment