Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <Types.h>
- #include <BwString.h>
- #include <BWAPI/Position.h>
- #include <BWAPI/UnitType.h>
- #include <BWAPI/Event.h>
- #include <BWAPI/EventType.h>
- //____________________________________________//
- // Forward conversion :: BW -> RS
- // Reverse conversion :: RS -> BW
- // BW - types that come from BWAPI
- // RS - types that come from outside, e.g. Rust
- // for value types it looks like this: Cast<BWAPI::Position, Position>
- // for opaque ref types looks like this:
- template<class BW, class RS>
- struct Cast {
- // type members to infer Cast type instance from CastFwd and CastRev
- typedef RS RsType;
- typedef BW BwType;
- // disallow to call non-specialized version
- static RS from_bw(const BW& bw) = delete;
- static BW to_bw(const RS& rs) = delete;
- };
- template <class FromType>
- struct CastFwd { typedef Cast<FromType, FromType> Type; };
- template <class ToType>
- struct CastRev { typedef Cast<ToType, ToType> Type; };
- //_______________________________________________________//
- // shortcut functions to make Cast usage more lightweight
- // cast_from_bw :: BW -> RS
- // cast_to_bw :: RS -> BW
- template<class BW>
- typename CastFwd<BW>::Type::RsType cast_from_bw(const BW& bw) {
- return CastFwd<BW>::Type::from_bw(bw);
- };
- template<class RS>
- typename CastRev<RS>::Type::BwType cast_to_bw(const RS& rs) {
- return CastRev<RS>::Type::to_bw(rs);
- };
- //_______________________________//
- // Cast specializations go here //
- template<>
- struct Cast<BWAPI::Position, Position> {
- typedef BWAPI::Position BwType;
- typedef Position RsType;
- static RsType from_bw(const BwType& bw) {
- return Position {bw.x, bw.y};
- }
- static BwType to_bw(const RsType& rs) {
- return BWAPI::Position {rs.x, rs.y};
- }
- };
- template<>
- struct CastFwd<BWAPI::Position> {
- typedef Cast<BWAPI::Position, Position> Type;
- };
- template<>
- struct CastRev<Position> {
- typedef Cast<BWAPI::Position, Position> Type;
- };
- //----------------------------------------------------
- template<>
- struct Cast<BWAPI::UnitType, UnitType> {
- typedef BWAPI::UnitType BwType;
- typedef UnitType RsType;
- inline static RsType from_bw(const BwType& bw) {
- return UnitType {bw.getID()};
- }
- inline static BwType to_bw(const RsType& rs) {
- return BWAPI::UnitType {rs.id};
- }
- };
- template<>
- struct CastFwd<BWAPI::UnitType> {
- typedef Cast<BWAPI::UnitType, UnitType> Type;
- };
- template<>
- struct CastRev<UnitType> {
- typedef Cast<BWAPI::UnitType, UnitType> Type;
- };
- //----------------------------------------------------
- template<>
- struct Cast<BWAPI::EventType::Enum, EventType> {
- typedef BWAPI::EventType::Enum BwType;
- typedef EventType RsType;
- inline static RsType from_bw(const BwType& bw) {
- return EventType { (int) bw };
- }
- inline static BwType to_bw(const RsType& rs) {
- return BWAPI::EventType::Enum(rs.id);
- }
- };
- template<>
- struct CastFwd<BWAPI::EventType::Enum> {
- typedef Cast<BWAPI::EventType::Enum, EventType> Type;
- };
- template<>
- struct CastRev<EventType> {
- typedef Cast<BWAPI::EventType::Enum, EventType> Type;
- };
- //----------------------------------------------------
- template<>
- struct Cast<std::string, BwString*> {
- typedef std::string BwType;
- typedef BwString* RsType;
- static RsType from_bw(const BwType& bw) {
- // TODO who is going to release the memory?
- return BwString_new(bw.c_str(), (int) bw.length());
- }
- static BwType to_bw(const RsType& rs) {
- return std::string(BwString_data(rs));
- }
- };
- template<>
- struct CastFwd<std::string> {
- typedef Cast<std::string, BwString*> Type;
- };
- template<>
- struct CastRev<BwString*> {
- typedef Cast<std::string, BwString*> Type;
- };
- //----------------------------------------------------
- template<>
- struct Cast<BWAPI::Unit, Unit*> {
- typedef BWAPI::Unit BwType;
- typedef Unit* RsType;
- static RsType from_bw(const BwType& bw) {
- return reinterpret_cast<Unit*>(const_cast<BWAPI::Unit>(bw));
- }
- static BwType to_bw(const RsType rs) {
- return reinterpret_cast<BWAPI::Unit>(const_cast<Unit*>(rs));
- }
- };
- template<>
- struct CastFwd<BWAPI::Unit> {
- typedef Cast<BWAPI::Unit, Unit*> Type;
- };
- template<>
- struct CastRev<Unit*> {
- typedef Cast<BWAPI::Unit, Unit*> Type;
- };
- //----------------------------------------------------
- template<>
- struct Cast<BWAPI::Player, Player*> {
- typedef BWAPI::Player BwType;
- typedef Player* RsType;
- static RsType from_bw(const BwType& bw) {
- return reinterpret_cast<Player*>(const_cast<BWAPI::Player>(bw));
- }
- static BwType to_bw(const RsType& rs) {
- return reinterpret_cast<BWAPI::Player>(const_cast<Player*>(rs));
- }
- };
- template<>
- struct CastFwd<BWAPI::Player> {
- typedef Cast<BWAPI::Player, Player*> Type;
- };
- template<>
- struct CastRev<Player*> {
- typedef Cast<BWAPI::Player, Player*> Type;
- };
- //----------------------------------------------------
- template<>
- struct Cast<BWAPI::Event, Event> {
- typedef BWAPI::Event BwType;
- typedef Event RsType;
- static RsType from_bw(const BwType& bw) {
- return Event {
- cast_from_bw(bw.getPosition()),
- cast_from_bw(bw.getText()),
- cast_from_bw(bw.getUnit()),
- cast_from_bw(bw.getPlayer()),
- cast_from_bw(bw.getType()),
- bw.isWinner()
- };
- }
- static BwType to_bw(const RsType& rs) {
- BWAPI::Event bw;
- bw.setPosition(cast_to_bw(rs.position));
- bw.setText(cast_to_bw(rs.text).c_str());
- bw.setUnit(cast_to_bw(rs.unit));
- bw.setPlayer(cast_to_bw(rs.player));
- bw.setType(cast_to_bw(rs.type));
- bw.setWinner(rs.winner);
- return bw;
- }
- };
- template<>
- struct CastFwd<BWAPI::Event> {
- typedef Cast<BWAPI::Event, Event> Type;
- };
- template<>
- struct CastRev<Event> {
- typedef Cast<BWAPI::Event, Event> Type;
- };
- //----------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement