mapper_factory.h

00001 
00002 /***************************************************************************
00003  *  mapper_factory.h - Factory for Player proxy to Fawkes interface mappers
00004  *
00005  *  Created: Tue Sep 30 00:28:01 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #ifndef __PLUGINS_PLAYER_MAPPER_FACTORY_H_
00024 #define __PLUGINS_PLAYER_MAPPER_FACTORY_H_
00025 
00026 #include "mapper.h"
00027 
00028 namespace fawkes {
00029   class Interface;
00030   class ObjectPositionInterface;
00031 }
00032 
00033 namespace PlayerCc {
00034   class ClientProxy;
00035   class Position2dProxy;
00036 }
00037 
00038 class PlayerMapperFactory
00039 {
00040  public:
00041   static PlayerProxyFawkesInterfaceMapper *create_mapper(std::string varname,
00042                                                          fawkes::Interface *interface,
00043                                                          PlayerCc::ClientProxy *proxy);
00044                                                          
00045 
00046  private:
00047   PlayerMapperFactory() {}
00048 
00049   template <class FawkesInterfaceType, class PlayerProxyType, class MapperType>
00050     static PlayerProxyFawkesInterfaceMapper *  try_create(std::string varname,
00051                                                           fawkes::Interface *interface,
00052                                                           PlayerCc::ClientProxy *proxy);
00053 };
00054 
00055 /** Try to create a mapper instance.
00056  * Tries to dynamically cast the Fawkes interface and Player proxy to the
00057  * desired types, and if that succeeds instantiates a new mapper of the given type
00058  * giving the casted instances as parameters.
00059  * @param varname variable name
00060  * @param interface Fawkes interface instance
00061  * @param proxy Player proxy instance
00062  * @return NULL if a dynamic cast failed, a mapper instance for the given interface
00063  * and proxy otherwise
00064  */
00065 template <class FawkesInterfaceType, class PlayerProxyType, class MapperType>
00066 PlayerProxyFawkesInterfaceMapper *
00067 PlayerMapperFactory::try_create(std::string varname,
00068                                 fawkes::Interface *interface,
00069                                 PlayerCc::ClientProxy *proxy)
00070 {
00071   FawkesInterfaceType *fi;
00072   if ( (fi = dynamic_cast<FawkesInterfaceType *>(interface)) != NULL ) {
00073     PlayerProxyType *pp;
00074     if ( (pp = dynamic_cast<PlayerProxyType *>(proxy)) != NULL ) {
00075       return new MapperType(varname, fi, pp);
00076     } else {
00077       return NULL;
00078     }
00079   } else {
00080     return NULL;
00081   }
00082 }
00083 
00084 
00085 #endif