Refactoring of built-in services registration system.

This commit is contained in:
Vincent Richard 2005-10-19 17:12:20 +00:00
parent 6f278de337
commit 1e94a4a70f
7 changed files with 118 additions and 80 deletions

View File

@ -207,6 +207,7 @@ libvmime_messaging_sources = [
'net/service.cpp', 'net/service.hpp',
'net/serviceFactory.cpp', 'net/serviceFactory.hpp',
'net/serviceInfos.cpp', 'net/serviceInfos.hpp',
'net/serviceRegistration.inl',
'net/session.cpp', 'net/session.hpp',
'net/socket.hpp',
'net/store.hpp',

View File

@ -21,14 +21,14 @@
// the GNU General Public License cover the whole combination.
//
// Include registration helpers
#include "src/net/serviceRegistration.inl"
#ifndef VMIME_BUILDING_DOC
#define REGISTER_SERVICE(p_class, p_name, p_type) \
vmime::net::service::initializer <vmime::net::p_class> \
p_name(#p_name, vmime::net::service::p_type)
#if VMIME_BUILTIN_MESSAGING_PROTO_POP3
#include "vmime/net/pop3/POP3Store.hpp"
REGISTER_SERVICE(pop3::POP3Store, pop3, TYPE_STORE);

View File

@ -122,5 +122,11 @@ const std::vector <ref <const serviceFactory::registeredService> > serviceFactor
}
void serviceFactory::registerService(ref <registeredService> reg)
{
m_services.push_back(reg);
}
} // net
} // vmime

View File

@ -0,0 +1,98 @@
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
//
// 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library. Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//
#include "vmime/net/serviceFactory.hpp"
#ifndef VMIME_BUILDING_DOC
namespace vmime {
namespace net {
template <class S>
class registeredServiceImpl : public serviceFactory::registeredService
{
public:
registeredServiceImpl(const string& name, const int type)
: m_type(type), m_name(name), m_servInfos(S::getInfosInstance())
{
}
ref <service> create
(ref <session> sess,
ref <security::authenticator> auth) const
{
return vmime::create <S>(sess, auth);
}
const serviceInfos& getInfos() const
{
return (m_servInfos);
}
const string& getName() const
{
return (m_name);
}
const int getType() const
{
return (m_type);
}
private:
const int m_type;
const string m_name;
const serviceInfos& m_servInfos;
};
// Basic service registerer
template <class S>
class serviceRegisterer
{
public:
serviceRegisterer(const string& protocol, const service::Type type)
{
serviceFactory::getInstance()->registerService
(vmime::create <vmime::net::registeredServiceImpl <S> >(protocol, type));
}
};
} // net
} // vmime
#define REGISTER_SERVICE(p_class, p_name, p_type) \
vmime::net::serviceRegisterer <vmime::net::p_class> \
p_name(#p_name, vmime::net::service::p_type)
#endif // VMIME_BUILDING_DOC

View File

@ -30,7 +30,6 @@
#include "vmime/net/session.hpp"
#include "vmime/net/serviceFactory.hpp"
#include "vmime/net/serviceInfos.hpp"
#include "vmime/net/socket.hpp"
@ -192,21 +191,6 @@ public:
m_session->getProperties()[getInfos().getPropertyPrefix() + name] = value;
}
#ifndef VMIME_BUILDING_DOC
// Basic service registerer
template <class S>
class initializer
{
public:
initializer(const string& protocol, const Type type)
{
serviceFactory::getInstance()->
template registerServiceByProtocol <S>(protocol, type);
}
};
#endif // VMIME_BUILDING_DOC
private:
ref <session> m_session;

View File

@ -33,6 +33,7 @@
#include "vmime/utility/stringUtils.hpp"
#include "vmime/utility/url.hpp"
#include "vmime/net/service.hpp"
#include "vmime/net/serviceInfos.hpp"
#include "vmime/net/timeoutHandler.hpp"
@ -46,7 +47,6 @@ namespace net {
class session;
class service;
/** A factory to create 'service' objects for a specified protocol.
@ -83,67 +83,12 @@ public:
virtual const serviceInfos& getInfos() const = 0;
};
private:
template <class S>
class registeredServiceImpl : public registeredService
{
friend class serviceFactory;
friend class vmime::creator;
protected:
registeredServiceImpl(const string& name, const int type)
: m_type(type), m_name(name), m_servInfos(S::getInfosInstance())
{
}
public:
ref <service> create
(ref <session> sess,
ref <security::authenticator> auth) const
{
return vmime::create <S>(sess, auth);
}
const serviceInfos& getInfos() const
{
return (m_servInfos);
}
const string& getName() const
{
return (m_name);
}
const int getType() const
{
return (m_type);
}
private:
const int m_type;
const string m_name;
const serviceInfos& m_servInfos;
};
std::vector <ref <registeredService> > m_services;
public:
/** Register a new service by its protocol name.
*
* @param protocol protocol name
* @param type service type
* @param reg service registration infos
*/
template <class S>
void registerServiceByProtocol(const string& protocol, const int type)
{
const string name = utility::stringUtils::toLower(protocol);
m_services.push_back(vmime::create <registeredServiceImpl <S> >(name, type));
}
void registerService(ref <registeredService> reg);
/** Create a new service instance from a protocol name.
*
@ -201,6 +146,10 @@ public:
* @return list of registered services
*/
const std::vector <ref <const registeredService> > getServiceList() const;
private:
std::vector <ref <registeredService> > m_services;
};

View File

@ -111,7 +111,7 @@
#if VMIME_HAVE_MESSAGING_FEATURES
#include "vmime/net/socket.hpp"
#include "vmime/net/service.hpp"
#include "vmime/net/serviceFactory.hpp"
#include "vmime/net/store.hpp"
#include "vmime/net/transport.hpp"