Commit 5530f57a authored by jpbl's avatar jpbl
Browse files

added the EventFactory

parent 31069e92
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef SFLPHONEGUI_OBJECTFACTORY_H
#define SFLPHONEGUI_OBJECTFACTORY_H
#include <list>
#include <map>
#include <string>
/**
* This is the base class that we will use to
* create an object from the "create" function.
*/
template< typename Base >
class EventCreatorBase
{
public:
virtual ~EventCreatorBase(){}
virtual Base *create(const std::string &code,
const std::list< std::string > &args) = 0;
virtual EventCreatorBase *clone() = 0;
};
/**
* This is the actual class that will create
* the request. It will return a Request
*/
template< typename Base, typename Actual >
class EventCreator : public EventCreatorBase< Base >
{
public:
virtual Actual *create(const std::string &code,
const std::list< std::string > &args);
virtual EventCreatorBase< Base > *clone();
};
/**
* This class is used to create object related to
* a string. However, thoses objects will be created
* with the default constructor.
*/
template< typename Base >
class EventFactory
{
public:
/**
* Ask for a new object linked to the string.
*/
Base *create(const std::string &code,
const std::list< std::string > &args);
/**
* Register the string to return a Actual type.
*/
template< typename Actual >
void registerEvent(const std::string &code);
private:
std::map< std::string, EventCreatorBase< Base > * > mEventCreators;
};
#include "EventFactory.inl"
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef SFLPHONEGUI_OBJECTFACTORY_INL
#define SFLPHONEGUI_OBJECTFACTORY_INL
#include <stdexcept>
template< typename Base, typename Actual >
Actual *
EventCreator< Base, Actual >::create(const std::string &code,
const std::list< std::string > &args)
{
return new Actual(code, args);
}
template< typename Base, typename Actual >
EventCreatorBase< Base > *
EventCreator< Base, Actual >::clone()
{
return new EventCreator< Base, Actual >();
}
template< typename Base >
Base *
EventFactory< Base >::create(const std::string &code,
const std::list< std::string > &args)
{
typename std::map< std::string, EventCreatorBase< Base > * >::iterator pos = mEventCreators.find(code);
if(pos == mEventCreators.end()) {
throw std::runtime_error("The code \"" + code + "\" has no creator registered.");
}
return pos->second->create(code, args);
}
template< typename Base >
template< typename Actual >
void
EventFactory< Base >::registerEvent(const std::string &code)
{
if(mEventCreators.find(code) != mEventCreators.end()) {
delete mEventCreators[code];
}
mEventCreators[code] = new EventCreator< Base, Actual >();
}
#endif
......@@ -6,9 +6,33 @@
#include "Request.hpp"
class CallRequest : public CallRelatedRequest
class EventRequest : public Request
{
virtual ~EventRequest(){}
/**
* This function will be called when the request
* receive its answer, if the request didn't successfully
* ended. When we have an error on an EventRequest, we should
* quit the program.
*/
virtual void onError(const std::string &code, const std::string &message);
/**
* This function will be called when the request
* receive an answer, but there's other answers to come.
* This will be dispatched to the valid event.
*/
virtual void onEntry(const std::string &code, const std::string &message);
/**
* This function will be called when the request
* receive its answer, if the request successfully
* ended. The event handling is gone, so we should
* quit.
*/
virtual void onSuccess(const std::string &code, const std::string &message);
};
#endif
......@@ -12,6 +12,8 @@ INCLUDEPATH += .
HEADERS += Account.hpp \
AnswerReceiver.hpp \
Call.hpp \
EventFactory.hpp \
EventFactory.inl \
globals.h \
JPushButton.hpp \
ObjectFactory.hpp \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment