Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Rust:
- enum Message {
- Quit,
- ChangeColor(i32, i32, i32),
- Move { x: i32, y: i32 },
- Write(String),
- }
- C++:
- class MsgType {
- enum InMsgType {
- T_Quit,
- T_ChangeColor,
- T_Move,
- T_Write,
- };
- struct Quit { enum { type = T_Quit } };
- struct ChangeColor { enum { type = T_ChangeColor } };
- struct Move { enum { type = T_Move } };
- struct Write { enum { type = T_Write } };
- };
- struct ChangeColorData {
- int32_t a;
- int32_t b;
- int32_t c;
- };
- struct MoveData {
- int32_t x;
- int32_t y;
- };
- struct WriteData {
- std::string a;
- };
- class Message {
- private:
- MsgType::InMsgType TagType;
- std::variant<ChangeColorData, MoveData, WriteData> VarData;
- public:
- Message(MsgType::Quit) :
- TagType(MsgType::Quit::type) {}
- Message(MsgType::ChangeColor, ChangeColorData data) :
- TagType(MsgType::ChangeColor::type), VarData(data) {}
- Message(MsgType::Move, MoveData data) :
- TagType(MsgType::Move::type), VarData(data) {}
- Message(MsgType::Write, WriteData data) :
- TagType(MsgType::Write::type), VarData(data) {}
- ~Message() = default;
- MsgType::InMsgType getType() const {
- return TagType;
- }
- ChangeColorData getChangeColor() const {
- return std::get<ChangeColorData>(VarData);
- }
- MoveData getMoveData() const {
- return std::get<MoveData>(VarData);
- }
- WriteData getWriteData() const {
- return std::get<WriteData>(VarData);
- }
- };
- Message DoSth(bool cond) {
- if(cond)
- return { MsgType::Quit };
- else
- return { MsgType::Move, { 12, 3 } };
- }
Advertisement
Add Comment
Please, Sign In to add comment