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"
|
2011-03-16 12:04:04 +01:00
|
|
|
#include "Timer.h"
|
2011-04-08 18:43:05 +02:00
|
|
|
#include "ProviderInterface.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 "../yvalve/gds_proto.h"
|
2011-01-17 13:17:24 +01:00
|
|
|
#include "../common/classes/init.h"
|
2011-03-31 15:44:22 +02:00
|
|
|
#include "../common/classes/auto.h"
|
|
|
|
#include "../common/classes/RefCounted.h"
|
2011-03-04 16:49:37 +01:00
|
|
|
#include "consts_pub.h"
|
|
|
|
|
2010-07-23 14:04:18 +02:00
|
|
|
namespace Firebird {
|
|
|
|
|
2011-04-07 19:16:00 +02:00
|
|
|
// If you need interface on stack, use template AutoPtr<YourInterface, AutoDisposable>
|
2011-01-14 18:31:40 +01:00
|
|
|
// as second parameter to store it.
|
2011-04-07 19:16:00 +02:00
|
|
|
class AutoDisposable
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-03-31 15:44:22 +02:00
|
|
|
static void clear(IDisposable* ptr)
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
2011-03-31 15:44:22 +02:00
|
|
|
ptr->dispose();
|
2010-07-23 14:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
// Implement standard interface and plugin functions
|
2011-04-08 17:18:50 +02:00
|
|
|
|
|
|
|
// Helps to implement disposable interfaces
|
2011-03-31 15:44:22 +02:00
|
|
|
template <class C, typename S = GlobalStorage>
|
|
|
|
class DisposeIface : public C, public S
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-03-31 15:44:22 +02:00
|
|
|
DisposeIface() { }
|
2011-01-14 18:31:40 +01:00
|
|
|
|
|
|
|
private:
|
2011-03-31 15:44:22 +02:00
|
|
|
DisposeIface(const DisposeIface&);
|
|
|
|
DisposeIface& operator=(const DisposeIface&);
|
|
|
|
};
|
2010-07-24 04:43:28 +02:00
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Helps to implement disposable interfaces on stack or static
|
2011-03-31 15:44:22 +02:00
|
|
|
template <class C, typename S = GlobalStorage>
|
2011-04-01 04:05:19 +02:00
|
|
|
class StackIface : public DisposeIface<C, S>
|
2011-03-31 15:44:22 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
public:
|
2011-03-31 15:44:22 +02:00
|
|
|
void FB_CARG dispose()
|
|
|
|
{ }
|
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-04-08 17:18:50 +02:00
|
|
|
// Helps to implement standard interfaces
|
2011-01-14 18:31:40 +01:00
|
|
|
template <class C, int V, typename S = GlobalStorage>
|
2011-03-31 15:44:22 +02:00
|
|
|
class StdIface : public C, public S
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-03-31 15:44:22 +02:00
|
|
|
StdIface() : refCounter(0) { }
|
|
|
|
|
2011-04-11 13:12:21 +02:00
|
|
|
int FB_CARG getVersion()
|
2011-03-31 15:44:22 +02:00
|
|
|
{
|
|
|
|
return V;
|
|
|
|
}
|
2011-01-14 18:31:40 +01:00
|
|
|
|
|
|
|
#ifdef DEV_BUILD
|
|
|
|
~StdIface()
|
|
|
|
{
|
2011-03-31 15:44:22 +02:00
|
|
|
fb_assert(refCounter.value() == 0);
|
2011-01-14 18:31:40 +01:00
|
|
|
}
|
|
|
|
#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
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Helps to implement plugins
|
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-04-11 13:12:21 +02:00
|
|
|
IInterface* owner;
|
2011-01-14 18:31:40 +01:00
|
|
|
|
|
|
|
public:
|
2011-04-11 13:12:21 +02:00
|
|
|
StdPlugin() : owner(NULL)
|
2011-01-14 18:31:40 +01:00
|
|
|
{ }
|
|
|
|
|
2011-04-11 13:12:21 +02:00
|
|
|
IInterface* FB_CARG getOwner()
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
2011-04-11 13:12:21 +02:00
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FB_CARG setOwner(IInterface* iface)
|
|
|
|
{
|
|
|
|
owner = iface;
|
2011-01-14 18:31:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Trivial factory
|
|
|
|
template <class P>
|
2011-04-07 19:16:00 +02:00
|
|
|
class SimpleFactoryBase : public StackIface<IPluginFactory>
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-04-07 19:16:00 +02:00
|
|
|
IPluginBase* FB_CARG createPlugin(IPluginConfig* factoryParameter)
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
2011-03-31 15:44:22 +02:00
|
|
|
P* p = new P(factoryParameter);
|
|
|
|
p->addRef();
|
|
|
|
return p;
|
2011-01-14 18:31:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Master interface access
|
|
|
|
class MasterInterfacePtr : public AutoPtr<IMaster, AutoDisposable>
|
2011-03-16 12:04:04 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-04-08 17:18:50 +02:00
|
|
|
MasterInterfacePtr() : AutoPtr<IMaster, AutoDisposable>(fb_get_master_interface())
|
2011-03-16 12:04:04 +01:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Generic plugins interface access
|
|
|
|
class PluginManagerInterfacePtr : public AutoPtr<IPluginManager, AutoDisposable>
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-04-08 17:18:50 +02:00
|
|
|
PluginManagerInterfacePtr() : AutoPtr<IPluginManager, AutoDisposable>(fb_get_master_interface()->getPluginManager())
|
2011-03-31 15:44:22 +02:00
|
|
|
{ }
|
2011-04-08 17:18:50 +02:00
|
|
|
PluginManagerInterfacePtr(IMaster* master) : AutoPtr<IPluginManager, AutoDisposable>(master->getPluginManager())
|
2011-03-31 15:44:22 +02:00
|
|
|
{ }
|
2010-07-23 14:04:18 +02:00
|
|
|
};
|
|
|
|
|
2011-03-04 16:49:37 +01:00
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Control timer interface access
|
|
|
|
class TimerInterfacePtr : public AutoPtr<ITimerControl, AutoDisposable>
|
2011-03-16 12:04:04 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-04-08 17:18:50 +02:00
|
|
|
TimerInterfacePtr() : AutoPtr<ITimerControl, AutoDisposable>(fb_get_master_interface()->getTimerControl())
|
2011-03-31 15:44:22 +02:00
|
|
|
{ }
|
2011-03-16 12:04:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-05-19 13:38:38 +02:00
|
|
|
// Distributed transactions coordinator access
|
|
|
|
class DtcInterfacePtr : public AutoPtr<IDtc, AutoDisposable>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DtcInterfacePtr() : AutoPtr<IDtc, AutoDisposable>(fb_get_master_interface()->getDtc())
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-03-04 16:49:37 +01:00
|
|
|
// When process exits, dynamically loaded modules (for us plugin modules)
|
|
|
|
// are unloaded first. As the result all global variables in plugin are already destroyed
|
|
|
|
// when yvalve is starting fb_shutdown(). This causes almost unavoidable segfault.
|
|
|
|
// To avoid it this class is added - it detects spontaneous (not by PluginManager)
|
2011-04-07 19:16:00 +02:00
|
|
|
// module unload and notifies PluginManager about this said fact.
|
2011-03-04 16:49:37 +01:00
|
|
|
class DummyStorage
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2011-04-07 19:16:00 +02:00
|
|
|
class UnloadDetectorHelper : public DisposeIface<IPluginModule, DummyStorage>
|
2011-03-04 16:49:37 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-04-02 06:16:48 +02:00
|
|
|
explicit UnloadDetectorHelper(MemoryPool&)
|
2011-04-07 19:16:00 +02:00
|
|
|
: flagOsUnload(true)
|
2011-03-04 16:49:37 +01:00
|
|
|
{ }
|
|
|
|
|
|
|
|
~UnloadDetectorHelper()
|
|
|
|
{
|
|
|
|
if (flagOsUnload)
|
|
|
|
{
|
2011-04-08 17:18:50 +02:00
|
|
|
PluginManagerInterfacePtr pi;
|
|
|
|
pi->unregisterModule(this);
|
2011-03-16 12:04:04 +01:00
|
|
|
|
2011-04-07 19:16:00 +02:00
|
|
|
flagOsUnload = false;
|
2011-03-04 16:49:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unloadStarted()
|
|
|
|
{
|
|
|
|
return !flagOsUnload;
|
|
|
|
}
|
|
|
|
|
2011-03-31 15:44:22 +02:00
|
|
|
void FB_CARG dispose()
|
|
|
|
{
|
|
|
|
// delete this; -don't do that!
|
|
|
|
}
|
|
|
|
|
2011-03-16 12:04:04 +01:00
|
|
|
private:
|
2011-03-04 16:49:37 +01:00
|
|
|
bool flagOsUnload;
|
2011-03-16 12:04:04 +01:00
|
|
|
|
|
|
|
void FB_CARG doClean()
|
|
|
|
{
|
|
|
|
flagOsUnload = false;
|
|
|
|
}
|
2011-03-04 16:49:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef GlobalPtr<UnloadDetectorHelper, InstanceControl::PRIORITY_DETECT_UNLOAD> UnloadDetector;
|
|
|
|
|
2011-04-08 18:43:05 +02:00
|
|
|
|
2011-04-13 14:41:40 +02:00
|
|
|
class InternalMessageBuffer : public FbMessage
|
2011-04-08 18:43:05 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-04-13 17:09:18 +02:00
|
|
|
InternalMessageBuffer(unsigned aBlrLength, const unsigned char* aBlr,
|
|
|
|
unsigned aBufferLength, unsigned char* aBuffer)
|
2011-04-08 18:43:05 +02:00
|
|
|
{
|
2011-04-13 14:41:40 +02:00
|
|
|
blrLength = aBlrLength;
|
|
|
|
blr = aBlr;
|
|
|
|
bufferLength = aBufferLength;
|
|
|
|
buffer = aBuffer;
|
2011-04-08 18:43:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-07-23 14:04:18 +02:00
|
|
|
} // namespace Firebird
|
|
|
|
|
|
|
|
|
|
|
|
#endif // FB_COMMON_CLASSES_IMPLEMENT_HELPER
|