Skip to content
Snippets Groups Projects
Commit 28d5ea5b authored by yanmorin's avatar yanmorin
Browse files

GNU Licensing
Add my copyright to it
parent 28e30020
No related branches found
No related tags found
No related merge requests found
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Yan Morin <yan.morin@savoirfairelinux.com> (cc++ mutex)
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
* Yan Morin <yan.morin@savoirfairelinux.com> (cc++ mutex)
*
* 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
......
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Yan Morin <yan.morin@savoirfairelinux.com> (cc++ mutex)
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
* Yan Morin <yan.morin@savoirfairelinux.com> (cc++ mutex)
*
* 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
......
#include <stdexcept>
#include <string>
#include <iostream>
#include <map>
class Request
{
public:
Request()
{
std::cout << "This is a Normal Request" << std::endl;
}
};
class SpecificRequest : public Request
{
public:
SpecificRequest()
{
std::cout << "This is a Specific Request" << std::endl;
}
};
class RequestCreatorBase
{
public:
virtual Request *create() = 0;
virtual RequestCreatorBase *clone() = 0;
};
template< typename T >
class RequestCreator : public RequestCreatorBase
{
public:
virtual Request *create()
{
return new T();
}
virtual RequestCreatorBase *clone()
{
return new RequestCreator< T >();
}
};
class RequestFactory
{
public:
Request *create(const std::string &requestname)
{
std::map< std::string, RequestCreatorBase * >::iterator pos = mRequests.find(requestname);
if(pos == mRequests.end()) {
throw std::runtime_error("there's no request of that name");
}
return pos->second->create();
}
template< typename T >
void registerRequest(const std::string &requestname)
{
std::map< std::string, RequestCreatorBase * >::iterator pos =
mRequests.find(requestname);
if(pos != mRequests.end()) {
delete pos->second; pos->second = NULL;
mRequests.erase(pos);
}
mRequests.insert(std::make_pair(requestname, new RequestCreator< T >()));
}
private:
std::map< std::string, RequestCreatorBase * > mRequests;
};
int main(int, char **)
{
RequestFactory factory;
factory.registerRequest< Request > ("requestsimple");
factory.registerRequest< SpecificRequest >("requestspecific");
std::cout << "First one" << std::endl;
delete factory.create("requestsimple");
std::cout << "Second one" << std::endl;
delete factory.create("requestspecific");
factory.registerRequest< SpecificRequest >("requestsimple");
std::cout << "Third one" << std::endl;
delete factory.create("requestsimple");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment