2010-07-23 14:04:18 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Firebird interface.
|
|
|
|
* MODULE: ImplementHelper.h
|
|
|
|
* DESCRIPTION: Tools to help write plugins.
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Initial
|
|
|
|
* Developer's Public License Version 1.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed AS IS,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing rights
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Alex Peshkov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 Alex Peshkov <peshkoff at mail.ru>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FB_COMMON_CLASSES_IMPLEMENT_HELPER
|
|
|
|
#define FB_COMMON_CLASSES_IMPLEMENT_HELPER
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
#include "FirebirdPluginApi.h"
|
2010-07-23 14:04:18 +02:00
|
|
|
#include "../common/classes/alloc.h"
|
2011-01-14 18:31:40 +01:00
|
|
|
#include "gen/iberror.h"
|
|
|
|
#include "../common/config/config.h"
|
|
|
|
#include "../yvalve/gds_proto.h"
|
2011-01-17 13:17:24 +01:00
|
|
|
#include "../common/classes/init.h"
|
2010-07-23 14:04:18 +02:00
|
|
|
|
|
|
|
namespace Firebird {
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
// If you need interface on stack, use template AutoPtr<YourInterface, AutoInterface>
|
|
|
|
// as second parameter to store it.
|
2010-07-23 14:04:18 +02:00
|
|
|
class AutoInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void clear(Interface* ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
ptr->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
// Implement standard interface and plugin functions
|
|
|
|
template <class C, int V, typename S>
|
|
|
|
class Versioned : public C, public S
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-01-14 18:31:40 +01:00
|
|
|
int FB_CARG version()
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
return V;
|
2010-07-23 14:04:18 +02:00
|
|
|
}
|
2011-01-14 18:31:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class C, int V, typename S = GlobalStorage>
|
|
|
|
class StackIface : public Versioned<C, V, S>
|
|
|
|
{
|
|
|
|
#ifdef DEV_BUILD
|
|
|
|
private:
|
|
|
|
int counter;
|
2010-07-24 04:43:28 +02:00
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
public:
|
|
|
|
// StackIface is destroyed in same frame where created,
|
|
|
|
// therefore final release() call is not expected,
|
|
|
|
// therefore initialize check counter with 0, not 1.
|
|
|
|
StackIface() : counter(0) { }
|
|
|
|
|
|
|
|
~StackIface()
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
fb_assert(counter == 0);
|
2010-07-23 14:04:18 +02:00
|
|
|
}
|
2011-01-14 18:31:40 +01:00
|
|
|
#endif
|
2010-07-24 04:43:28 +02:00
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
public:
|
|
|
|
void FB_CARG addRef()
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
#ifdef DEV_BUILD
|
|
|
|
++counter;
|
|
|
|
#endif
|
2010-07-23 14:04:18 +02:00
|
|
|
}
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
int FB_CARG release()
|
2010-10-12 10:02:57 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
#ifdef DEV_BUILD
|
|
|
|
--counter;
|
|
|
|
#endif
|
|
|
|
return 1;
|
2010-10-12 10:02:57 +02:00
|
|
|
}
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
void* operator new(size_t, void* memory) throw()
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
return memory;
|
2010-07-23 14:04:18 +02:00
|
|
|
}
|
2011-01-14 18:31:40 +01:00
|
|
|
};
|
2010-07-24 04:43:28 +02:00
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
template <class C, int V, typename S = GlobalStorage>
|
|
|
|
class StdIface : public Versioned<C, V, S>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StdIface() : refCounter(1) { }
|
|
|
|
|
|
|
|
#ifdef DEV_BUILD
|
|
|
|
~StdIface()
|
|
|
|
{
|
|
|
|
// fb_assert(refCounter.value() == 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void FB_CARG addRef()
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
++refCounter;
|
2010-07-23 14:04:18 +02:00
|
|
|
}
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
protected:
|
|
|
|
AtomicCounter refCounter;
|
2011-01-19 08:15:00 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
StdIface(const StdIface&);
|
|
|
|
StdIface& operator=(const StdIface&);
|
2011-01-14 18:31:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <class C, int V, typename S = GlobalStorage>
|
|
|
|
class StdPlugin : public StdIface<C, V, S>
|
|
|
|
{
|
2010-07-23 14:04:18 +02:00
|
|
|
private:
|
2011-01-14 18:31:40 +01:00
|
|
|
Interface* own;
|
|
|
|
|
|
|
|
public:
|
|
|
|
StdPlugin() : own(NULL)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Interface* FB_CARG owner(Interface* iface)
|
|
|
|
{
|
|
|
|
Interface* rc = own;
|
|
|
|
own = iface;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Trivial factory
|
|
|
|
template <class P>
|
2011-01-17 13:17:24 +01:00
|
|
|
class SimpleFactoryBase : public StackIface<PluginsFactory, FB_PLUGINS_FACTORY_VERSION>
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Plugin* FB_CARG createPlugin(IFactoryParameter* factoryParameter)
|
|
|
|
{
|
|
|
|
return new P(factoryParameter);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-01-17 13:17:24 +01:00
|
|
|
template <class P>
|
|
|
|
class SimpleFactory : public Static<SimpleFactoryBase<P> >
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
|
|
|
|
class DefaultMissingEntrypoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void FB_CARG noEntrypoint()
|
|
|
|
{
|
|
|
|
Arg::Gds(isc_wish_list).raise();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Template to help with loop in the set of plugins
|
|
|
|
template <typename P, typename M = DefaultMissingEntrypoint>
|
|
|
|
class PluginsSet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PluginsSet(unsigned int interfaceType, unsigned int desiredVersion, const char* namesList = NULL)
|
|
|
|
: masterInterface(fb_get_master_interface()), pluginInterface(masterInterface->getPluginInterface()), missing(),
|
|
|
|
pluginSet(pluginInterface->getPlugins(interfaceType, namesList ? namesList : Config::getPlugins(interfaceType),
|
|
|
|
desiredVersion, &missing)),
|
|
|
|
currentPlugin(NULL)
|
|
|
|
{
|
|
|
|
getPlugin();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasData()
|
|
|
|
{
|
|
|
|
return currentPlugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* name()
|
|
|
|
{
|
|
|
|
return hasData() ? pluginSet->name() : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* module()
|
|
|
|
{
|
|
|
|
return hasData() ? pluginSet->module() : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
P* plugin()
|
|
|
|
{
|
|
|
|
return currentPlugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void next()
|
|
|
|
{
|
|
|
|
if (hasData())
|
|
|
|
{
|
|
|
|
pluginInterface->releasePlugin(currentPlugin);
|
|
|
|
currentPlugin = NULL;
|
|
|
|
pluginSet->next();
|
|
|
|
getPlugin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(const char* newName)
|
|
|
|
{
|
|
|
|
if (hasData())
|
|
|
|
{
|
|
|
|
pluginInterface->releasePlugin(currentPlugin);
|
|
|
|
currentPlugin = NULL;
|
|
|
|
}
|
|
|
|
pluginSet->set(newName);
|
|
|
|
getPlugin();
|
|
|
|
}
|
|
|
|
|
|
|
|
~PluginsSet()
|
|
|
|
{
|
|
|
|
if (hasData())
|
|
|
|
{
|
|
|
|
pluginInterface->releasePlugin(currentPlugin);
|
|
|
|
currentPlugin = NULL;
|
|
|
|
}
|
|
|
|
if (pluginSet)
|
|
|
|
pluginSet->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoPtr<IMaster, AutoInterface> masterInterface;
|
|
|
|
AutoPtr<IPlugin, AutoInterface> pluginInterface;
|
|
|
|
M missing;
|
|
|
|
// AutoPtr<IPluginSet, AutoInterface> pluginSet;
|
|
|
|
IPluginSet *pluginSet;
|
|
|
|
P* currentPlugin;
|
|
|
|
|
|
|
|
void getPlugin()
|
|
|
|
{
|
2011-01-16 03:16:15 +01:00
|
|
|
currentPlugin = (P*) pluginSet->plugin();
|
2011-01-14 18:31:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class PluginInterface : public AutoPtr<IPlugin, AutoInterface>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PluginInterface() : AutoPtr<IPlugin, AutoInterface>(NULL)
|
|
|
|
{
|
|
|
|
IMaster* mi = fb_get_master_interface();
|
|
|
|
reset(mi->getPluginInterface());
|
|
|
|
mi->release();
|
|
|
|
}
|
2010-07-23 14:04:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
|
|
|
|
|
|
|
#endif // FB_COMMON_CLASSES_IMPLEMENT_HELPER
|