2010-07-23 14:04:18 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Firebird interface.
|
|
|
|
* MODULE: ImplementHelper.h
|
2011-05-19 18:24:46 +02:00
|
|
|
* DESCRIPTION: Tools to help create interfaces.
|
2010-07-23 14:04:18 +02:00
|
|
|
*
|
|
|
|
* 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-06-02 17:57:08 +02:00
|
|
|
#include "firebird/Plugin.h"
|
|
|
|
#include "firebird/Timer.h"
|
|
|
|
#include "firebird/Provider.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.
|
2012-03-30 16:15:08 +02:00
|
|
|
typedef SimpleDispose<IDisposable> AutoDisposable;
|
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
|
|
|
|
2011-05-19 18:24:46 +02:00
|
|
|
// Helps to implement generic versioned interfaces
|
|
|
|
template <class C, int V>
|
|
|
|
class VersionedIface : public C
|
2010-07-23 14:04:18 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
VersionedIface() { }
|
|
|
|
|
|
|
|
int FB_CARG getVersion()
|
|
|
|
{
|
|
|
|
return V;
|
|
|
|
}
|
2011-01-14 18:31:40 +01:00
|
|
|
|
2011-06-13 13:08:26 +02:00
|
|
|
IPluginModule* FB_CARG getModule();
|
2011-06-10 14:53:51 +02:00
|
|
|
|
2011-01-14 18:31:40 +01:00
|
|
|
private:
|
2011-05-19 18:24:46 +02:00
|
|
|
VersionedIface(const VersionedIface&);
|
|
|
|
VersionedIface& operator=(const VersionedIface&);
|
2011-03-31 15:44:22 +02:00
|
|
|
};
|
2010-07-24 04:43:28 +02:00
|
|
|
|
2011-05-19 18:24:46 +02:00
|
|
|
|
|
|
|
// Helps to implement versioned interfaces on stack or static
|
|
|
|
template <class C, int V>
|
|
|
|
class AutoIface : public VersionedIface<C, V>
|
2011-03-31 15:44:22 +02:00
|
|
|
{
|
2011-01-14 18:31:40 +01:00
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
AutoIface() { }
|
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-05-19 18:24:46 +02:00
|
|
|
// Helps to implement disposable interfaces
|
|
|
|
template <class C, int V>
|
|
|
|
class DisposeIface : public VersionedIface<C, V>, public GlobalStorage
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
DisposeIface() { }
|
|
|
|
};
|
2011-03-31 15:44:22 +02:00
|
|
|
|
2011-05-19 18:24:46 +02:00
|
|
|
// Helps to implement standard interfaces
|
|
|
|
template <class C, int V>
|
|
|
|
class RefCntIface : public VersionedIface<C, V>, public GlobalStorage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefCntIface() : refCounter(0) { }
|
2011-01-14 18:31:40 +01:00
|
|
|
|
|
|
|
#ifdef DEV_BUILD
|
2011-05-19 18:24:46 +02:00
|
|
|
~RefCntIface()
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
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-04-08 17:18:50 +02:00
|
|
|
// Helps to implement plugins
|
2011-05-19 18:24:46 +02:00
|
|
|
template <class C, int V>
|
|
|
|
class StdPlugin : public RefCntIface<C, V>
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
2010-07-23 14:04:18 +02:00
|
|
|
private:
|
2011-05-19 18:24:46 +02:00
|
|
|
IRefCounted* 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-05-19 18:24:46 +02:00
|
|
|
IRefCounted* FB_CARG getOwner()
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
2011-04-11 13:12:21 +02:00
|
|
|
return owner;
|
|
|
|
}
|
|
|
|
|
2011-05-19 18:24:46 +02:00
|
|
|
void FB_CARG setOwner(IRefCounted* iface)
|
2011-04-11 13:12:21 +02:00
|
|
|
{
|
|
|
|
owner = iface;
|
2011-01-14 18:31:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Trivial factory
|
|
|
|
template <class P>
|
2011-05-19 18:24:46 +02:00
|
|
|
class SimpleFactoryBase : public AutoIface<IPluginFactory, FB_PLUGIN_FACTORY_VERSION>
|
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
|
|
|
|
2012-03-26 13:34:52 +02:00
|
|
|
// Ensure access to cached pointer to master interface
|
|
|
|
class CachedMasterInterface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void set(IMaster* master);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static IMaster* getMasterInterface();
|
|
|
|
};
|
|
|
|
|
2011-05-21 20:56:02 +02:00
|
|
|
// Base for interface type independent accessors
|
2011-05-19 18:24:46 +02:00
|
|
|
template <typename C>
|
2012-03-26 13:34:52 +02:00
|
|
|
class AccessAutoInterface : public CachedMasterInterface
|
2011-05-19 18:24:46 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-06-24 08:34:16 +02:00
|
|
|
explicit AccessAutoInterface(C* aPtr)
|
2011-05-19 18:24:46 +02:00
|
|
|
: ptr(aPtr)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
operator C*()
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
C* operator->()
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
C* ptr;
|
|
|
|
};
|
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Master interface access
|
2011-05-19 18:24:46 +02:00
|
|
|
class MasterInterfacePtr : public AccessAutoInterface<IMaster>
|
2011-03-16 12:04:04 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
MasterInterfacePtr()
|
2012-03-26 13:34:52 +02:00
|
|
|
: AccessAutoInterface<IMaster>(getMasterInterface())
|
2011-03-16 12:04:04 +01:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-04-08 17:18:50 +02:00
|
|
|
// Generic plugins interface access
|
2011-05-19 18:24:46 +02:00
|
|
|
class PluginManagerInterfacePtr : public AccessAutoInterface<IPluginManager>
|
2011-01-14 18:31:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
PluginManagerInterfacePtr()
|
2012-03-26 13:34:52 +02:00
|
|
|
: AccessAutoInterface<IPluginManager>(getMasterInterface()->getPluginManager())
|
2011-03-31 15:44:22 +02:00
|
|
|
{ }
|
2012-03-26 13:34:52 +02:00
|
|
|
/* explicit PluginManagerInterfacePtr(IMaster* master)
|
2011-05-19 18:24:46 +02:00
|
|
|
: AccessAutoInterface<IPluginManager>(master->getPluginManager())
|
2012-03-26 13:34:52 +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
|
2011-05-19 18:24:46 +02:00
|
|
|
class TimerInterfacePtr : public AccessAutoInterface<ITimerControl>
|
2011-03-16 12:04:04 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
TimerInterfacePtr()
|
2012-03-26 13:34:52 +02:00
|
|
|
: AccessAutoInterface<ITimerControl>(getMasterInterface()->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
|
2011-05-19 18:24:46 +02:00
|
|
|
class DtcInterfacePtr : public AccessAutoInterface<IDtc>
|
2011-05-19 13:38:38 +02:00
|
|
|
{
|
|
|
|
public:
|
2011-05-19 18:24:46 +02:00
|
|
|
DtcInterfacePtr()
|
2012-03-26 13:34:52 +02:00
|
|
|
: AccessAutoInterface<IDtc>(getMasterInterface()->getDtc())
|
2011-05-19 13:38:38 +02:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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-05-19 18:24:46 +02:00
|
|
|
class UnloadDetectorHelper : public VersionedIface<IPluginModule, FB_PLUGIN_MODULE_VERSION>
|
2011-03-04 16:49:37 +01:00
|
|
|
{
|
|
|
|
public:
|
2011-05-27 13:31:29 +02:00
|
|
|
typedef void VoidNoParam();
|
|
|
|
|
2011-04-02 06:16:48 +02:00
|
|
|
explicit UnloadDetectorHelper(MemoryPool&)
|
2011-06-14 13:34:26 +02:00
|
|
|
: cleanup(NULL), flagOsUnload(false)
|
2011-03-04 16:49:37 +01:00
|
|
|
{ }
|
|
|
|
|
2011-06-14 13:34:26 +02:00
|
|
|
void registerMe()
|
|
|
|
{
|
|
|
|
PluginManagerInterfacePtr()->registerModule(this);
|
|
|
|
flagOsUnload = true;
|
|
|
|
}
|
|
|
|
|
2011-03-04 16:49:37 +01:00
|
|
|
~UnloadDetectorHelper()
|
|
|
|
{
|
|
|
|
if (flagOsUnload)
|
|
|
|
{
|
2011-06-14 13:34:26 +02:00
|
|
|
PluginManagerInterfacePtr()->unregisterModule(this);
|
2011-05-27 13:31:29 +02:00
|
|
|
doClean();
|
2011-03-04 16:49:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool unloadStarted()
|
|
|
|
{
|
|
|
|
return !flagOsUnload;
|
|
|
|
}
|
|
|
|
|
2011-05-27 13:31:29 +02:00
|
|
|
void setCleanup(VoidNoParam* function)
|
|
|
|
{
|
|
|
|
cleanup = function;
|
|
|
|
}
|
|
|
|
|
2011-03-16 12:04:04 +01:00
|
|
|
private:
|
2011-05-27 13:31:29 +02:00
|
|
|
VoidNoParam* cleanup;
|
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-05-28 04:05:45 +02:00
|
|
|
|
2011-05-27 13:31:29 +02:00
|
|
|
if (cleanup)
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
cleanup = NULL;
|
|
|
|
}
|
2011-03-16 12:04:04 +01:00
|
|
|
}
|
2011-03-04 16:49:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef GlobalPtr<UnloadDetectorHelper, InstanceControl::PRIORITY_DETECT_UNLOAD> UnloadDetector;
|
2011-06-10 14:53:51 +02:00
|
|
|
extern UnloadDetector myModule;
|
|
|
|
|
|
|
|
template <class C, int V> IPluginModule* VersionedIface<C, V>::getModule()
|
|
|
|
{
|
|
|
|
return &myModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Default replacement for missing virtual functions
|
|
|
|
class DefaultMissingEntrypoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void FB_CARG noEntrypoint()
|
|
|
|
{
|
|
|
|
Arg::Gds(isc_wish_list).raise();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helps to create update information
|
|
|
|
template <typename M = DefaultMissingEntrypoint>
|
|
|
|
class MakeUpgradeInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MakeUpgradeInfo()
|
|
|
|
{
|
|
|
|
ui.missingFunctionClass = &missing;
|
|
|
|
ui.clientModule = &myModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator UpgradeInfo*()
|
|
|
|
{
|
|
|
|
return &ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
M missing;
|
|
|
|
struct UpgradeInfo ui;
|
|
|
|
};
|
2011-03-04 16:49:37 +01:00
|
|
|
|
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
|