Advertisement
NLinker

Concept of conversion

May 30th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.07 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <Types.h>
  4. #include <BwString.h>
  5. #include <BWAPI/Position.h>
  6. #include <BWAPI/UnitType.h>
  7. #include <BWAPI/Event.h>
  8. #include <BWAPI/EventType.h>
  9.  
  10. //____________________________________________//
  11. // Forward conversion :: BW -> RS
  12. // Reverse conversion :: RS -> BW
  13. // BW - types that come from BWAPI
  14. // RS - types that come from outside, e.g. Rust
  15. // for value types it looks like this: Cast<BWAPI::Position, Position>
  16. // for opaque ref types looks like this:
  17.  
  18. template<class BW, class RS>
  19. struct Cast {
  20.     // type members to infer Cast type instance from CastFwd and CastRev
  21.     typedef RS RsType;
  22.     typedef BW BwType;
  23.     // disallow to call non-specialized version
  24.     static RS from_bw(const BW& bw) = delete;
  25.     static BW to_bw(const RS& rs) = delete;
  26. };
  27.  
  28. template <class FromType>
  29. struct CastFwd { typedef Cast<FromType, FromType> Type; };
  30.  
  31. template <class ToType>
  32. struct CastRev { typedef Cast<ToType, ToType> Type; };
  33.  
  34. //_______________________________________________________//
  35. // shortcut functions to make Cast usage more lightweight
  36. // cast_from_bw :: BW -> RS
  37. // cast_to_bw :: RS -> BW
  38.  
  39. template<class BW>
  40. typename CastFwd<BW>::Type::RsType cast_from_bw(const BW& bw) {
  41.     return CastFwd<BW>::Type::from_bw(bw);
  42. };
  43.  
  44. template<class RS>
  45. typename CastRev<RS>::Type::BwType cast_to_bw(const RS& rs) {
  46.     return CastRev<RS>::Type::to_bw(rs);
  47. };
  48.  
  49. //_______________________________//
  50. // Cast specializations go here  //
  51.  
  52. template<>
  53. struct Cast<BWAPI::Position, Position> {
  54.     typedef BWAPI::Position BwType;
  55.     typedef Position        RsType;
  56.  
  57.     static RsType from_bw(const BwType& bw) {
  58.         return Position {bw.x, bw.y};
  59.     }
  60.     static BwType to_bw(const RsType& rs) {
  61.         return BWAPI::Position {rs.x, rs.y};
  62.     }
  63. };
  64. template<>
  65. struct CastFwd<BWAPI::Position> {
  66.     typedef Cast<BWAPI::Position, Position> Type;
  67. };
  68. template<>
  69. struct CastRev<Position> {
  70.     typedef Cast<BWAPI::Position, Position> Type;
  71. };
  72. //----------------------------------------------------
  73.  
  74. template<>
  75. struct Cast<BWAPI::UnitType, UnitType> {
  76.     typedef BWAPI::UnitType BwType;
  77.     typedef UnitType        RsType;
  78.  
  79.     inline static RsType from_bw(const BwType& bw) {
  80.         return UnitType {bw.getID()};
  81.     }
  82.     inline static BwType to_bw(const RsType& rs) {
  83.         return BWAPI::UnitType {rs.id};
  84.     }
  85. };
  86. template<>
  87. struct CastFwd<BWAPI::UnitType> {
  88.     typedef Cast<BWAPI::UnitType, UnitType> Type;
  89. };
  90. template<>
  91. struct CastRev<UnitType> {
  92.     typedef Cast<BWAPI::UnitType, UnitType> Type;
  93. };
  94. //----------------------------------------------------
  95.  
  96. template<>
  97. struct Cast<BWAPI::EventType::Enum, EventType> {
  98.     typedef BWAPI::EventType::Enum BwType;
  99.     typedef EventType              RsType;
  100.  
  101.     inline static RsType from_bw(const BwType& bw) {
  102.         return EventType { (int) bw };
  103.     }
  104.     inline static BwType to_bw(const RsType& rs) {
  105.         return BWAPI::EventType::Enum(rs.id);
  106.     }
  107. };
  108. template<>
  109. struct CastFwd<BWAPI::EventType::Enum> {
  110.     typedef Cast<BWAPI::EventType::Enum, EventType> Type;
  111. };
  112. template<>
  113. struct CastRev<EventType> {
  114.     typedef Cast<BWAPI::EventType::Enum, EventType> Type;
  115. };
  116. //----------------------------------------------------
  117.  
  118. template<>
  119. struct Cast<std::string, BwString*> {
  120.     typedef std::string BwType;
  121.     typedef BwString*   RsType;
  122.  
  123.     static RsType from_bw(const BwType& bw) {
  124.         // TODO who is going to release the memory?
  125.         return BwString_new(bw.c_str(), (int) bw.length());
  126.     }
  127.     static BwType to_bw(const RsType& rs) {
  128.         return std::string(BwString_data(rs));
  129.     }
  130. };
  131. template<>
  132. struct CastFwd<std::string> {
  133.     typedef Cast<std::string, BwString*> Type;
  134. };
  135. template<>
  136. struct CastRev<BwString*> {
  137.     typedef Cast<std::string, BwString*> Type;
  138. };
  139. //----------------------------------------------------
  140.  
  141. template<>
  142. struct Cast<BWAPI::Unit, Unit*> {
  143.     typedef BWAPI::Unit BwType;
  144.     typedef Unit*       RsType;
  145.     static RsType from_bw(const BwType& bw) {
  146.         return reinterpret_cast<Unit*>(const_cast<BWAPI::Unit>(bw));
  147.     }
  148.     static BwType to_bw(const RsType rs) {
  149.         return reinterpret_cast<BWAPI::Unit>(const_cast<Unit*>(rs));
  150.     }
  151. };
  152. template<>
  153. struct CastFwd<BWAPI::Unit> {
  154.     typedef Cast<BWAPI::Unit, Unit*> Type;
  155. };
  156. template<>
  157. struct CastRev<Unit*> {
  158.     typedef Cast<BWAPI::Unit, Unit*> Type;
  159. };
  160. //----------------------------------------------------
  161.  
  162. template<>
  163. struct Cast<BWAPI::Player, Player*> {
  164.     typedef BWAPI::Player BwType;
  165.     typedef Player*       RsType;
  166.     static RsType from_bw(const BwType& bw) {
  167.         return reinterpret_cast<Player*>(const_cast<BWAPI::Player>(bw));
  168.     }
  169.     static BwType to_bw(const RsType& rs) {
  170.         return reinterpret_cast<BWAPI::Player>(const_cast<Player*>(rs));
  171.     }
  172. };
  173. template<>
  174. struct CastFwd<BWAPI::Player> {
  175.     typedef Cast<BWAPI::Player, Player*> Type;
  176. };
  177. template<>
  178. struct CastRev<Player*> {
  179.     typedef Cast<BWAPI::Player, Player*> Type;
  180. };
  181. //----------------------------------------------------
  182.  
  183. template<>
  184. struct Cast<BWAPI::Event, Event> {
  185.     typedef BWAPI::Event BwType;
  186.     typedef Event        RsType;
  187.  
  188.     static RsType from_bw(const BwType& bw) {
  189.         return Event {
  190.             cast_from_bw(bw.getPosition()),
  191.             cast_from_bw(bw.getText()),
  192.             cast_from_bw(bw.getUnit()),
  193.             cast_from_bw(bw.getPlayer()),
  194.             cast_from_bw(bw.getType()),
  195.             bw.isWinner()
  196.         };
  197.     }
  198.     static BwType to_bw(const RsType& rs) {
  199.         BWAPI::Event bw;
  200.         bw.setPosition(cast_to_bw(rs.position));
  201.         bw.setText(cast_to_bw(rs.text).c_str());
  202.         bw.setUnit(cast_to_bw(rs.unit));
  203.         bw.setPlayer(cast_to_bw(rs.player));
  204.         bw.setType(cast_to_bw(rs.type));
  205.         bw.setWinner(rs.winner);
  206.         return bw;
  207.     }
  208. };
  209. template<>
  210. struct CastFwd<BWAPI::Event> {
  211.     typedef Cast<BWAPI::Event, Event> Type;
  212. };
  213. template<>
  214. struct CastRev<Event> {
  215.     typedef Cast<BWAPI::Event, Event> Type;
  216. };
  217. //----------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement