Fix debian/rules to build in BUILD/ dir.
[tagua/yd.git] / src / serverreference.h
blobc6ea55be2caaa27cc558114c79bb840a3360ccba
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef CHESSREFERENCE_H
12 #define CHESSREFERENCE_H
14 #include <boost/shared_ptr.hpp>
15 #include "serverinfo.h"
17 template <typename Pos>
18 struct ComparePosition {
19 static bool apply(const Pos& chessPos, const Pos& pos) {
20 return static_cast<const ChessPosition&>(chessPos) == pos;
24 /**
25 * @brief Common base for ServerReference specializations.
27 template <typename Pos>
28 struct ServerReferenceBase {
29 typedef Pos Position;
31 class InfoVisitor : public boost::static_visitor<bool> {
32 const Position& m_position;
33 public:
34 InfoVisitor(const Position& position)
35 : m_position(position) { }
36 bool operator()(const ChessServerInfo<Pos>& info) const {
37 return ComparePosition<Pos>::apply(
38 *info.position,
39 m_position);
41 bool operator()(const PoolServerInfo<Pos>&) const {
42 return true; // FIXME
46 /**
47 * Compare a position with a @a ChessPosition. Used to check locally
48 * computed positions against server provided positions.
50 static bool checkPosition(const Position& pos, const ChessPosition& serverPos) {
51 return serverPos == pos;
54 /**
55 * Retrieve a position shared pointer from a @a ChessPosition
56 * pointer. Used to retrieve variant specific position
57 * information from @a style12.position
59 static boost::shared_ptr<Position> getPosition(
60 const boost::shared_ptr<ChessPosition>& pos) {
61 return boost::shared_ptr<Position>(
62 new Position(*pos));
66 /**
67 * Compare a position with partial information contained in another position.
69 static bool checkPartialPosition(const Position& pos, const typename ServerInfo<Position>::type& server) {
70 return boost::apply_visitor(InfoVisitor(pos), server);
74 /**
75 * @brief ServerReference is used whenever information coming from
76 * the server has to be compared with variant specific structures.
78 template <typename Pos>
79 struct ServerReference : public ServerReferenceBase<Pos> { };
81 template <>
82 struct ServerReference<ChessPosition>
83 : public ServerReferenceBase<ChessPosition> {
84 static boost::shared_ptr<ChessPosition> getPosition(
85 const boost::shared_ptr<ChessPosition>& pos) {
86 return pos;
90 #endif // CHESSREFERENCE_H