Separate templates out into their own header

This commit is contained in:
Paul Reeves 2023-01-25 14:53:02 +01:00
parent 39c337536d
commit 42c9399639
2 changed files with 192 additions and 158 deletions

View File

@ -8,172 +8,17 @@
#include "ibase.h" #include "ibase.h"
#include "firebird/UdrCppEngine.h" #include "firebird/UdrCppEngine.h"
#include "UdrCppTemplates.h"
#include <cassert> #include <cassert>
#include <cstdio> #include <cstdio>
//// DOC NOTE This is the basic stuff from UdrCppExample.h - seems to be declared in all UDR libraries
namespace
{
template <typename T>
class AutoReleaseClear
{
public:
static void clear(T* ptr)
{
if (ptr)
ptr->release();
}
};
template <typename T>
class AutoDisposeClear
{
public:
static void clear(T* ptr)
{
if (ptr)
ptr->dispose();
}
};
template <typename T>
class AutoDeleteClear
{
public:
static void clear(T* ptr)
{
delete ptr;
}
};
template <typename T>
class AutoArrayDeleteClear
{
public:
static void clear(T* ptr)
{
delete [] ptr;
}
};
template <typename T, typename Clear>
class AutoImpl
{
public:
AutoImpl<T, Clear>(T* aPtr = NULL)
: ptr(aPtr)
{
}
~AutoImpl()
{
Clear::clear(ptr);
}
AutoImpl<T, Clear>& operator =(T* aPtr)
{
Clear::clear(ptr);
ptr = aPtr;
return *this;
}
operator T*()
{
return ptr;
}
operator const T*() const
{
return ptr;
}
bool operator !() const
{
return !ptr;
}
bool hasData() const
{
return ptr != NULL;
}
T* operator ->()
{
return ptr;
}
T* release()
{
T* tmp = ptr;
ptr = NULL;
return tmp;
}
void reset(T* aPtr = NULL)
{
if (aPtr != ptr)
{
Clear::clear(ptr);
ptr = aPtr;
}
}
private:
// not implemented
AutoImpl<T, Clear>(AutoImpl<T, Clear>&);
void operator =(AutoImpl<T, Clear>&);
private:
T* ptr;
};
template <typename T> class AutoDispose : public AutoImpl<T, AutoDisposeClear<T> >
{
public:
AutoDispose(T* ptr = NULL)
: AutoImpl<T, AutoDisposeClear<T> >(ptr)
{
}
};
template <typename T> class AutoRelease : public AutoImpl<T, AutoReleaseClear<T> >
{
public:
AutoRelease(T* ptr = NULL)
: AutoImpl<T, AutoReleaseClear<T> >(ptr)
{
}
};
template <typename T> class AutoDelete : public AutoImpl<T, AutoDeleteClear<T> >
{
public:
AutoDelete(T* ptr = NULL)
: AutoImpl<T, AutoDeleteClear<T> >(ptr)
{
}
};
template <typename T> class AutoArrayDelete : public AutoImpl<T, AutoArrayDeleteClear<T> >
{
public:
AutoArrayDelete(T* ptr = NULL)
: AutoImpl<T, AutoArrayDeleteClear<T> >(ptr)
{
}
};
}
// This should be used in only one of the UDR library files. // This should be used in only one of the UDR library files.
// Build must export firebird_udr_plugin function. // Build must export firebird_udr_plugin function.
//
FB_UDR_IMPLEMENT_ENTRY_POINT FB_UDR_IMPLEMENT_ENTRY_POINT
#endif // MY_FIRST_UDR_KIT_H #endif

189
src/UdrCppTemplates.h Normal file
View File

@ -0,0 +1,189 @@
/*
* 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 Adriano dos Santos Fernandes
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2015 Adriano dos Santos Fernandes <adrianosf@gmail.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
//// DOC NOTE This is the basic stuff from UdrCppExample.h - seems to be declared in all UDR libraries
#ifndef UDR_CPP_TEMPLATES_H
#define UDR_CPP_TEMPLATES_H
#define FB_UDR_STATUS_TYPE ::Firebird::ThrowStatusWrapper
#include "ibase.h"
#include "firebird/UdrCppEngine.h"
//#include <cassert>
//#include <cstdio>
namespace
{
template <typename T>
class AutoReleaseClear
{
public:
static void clear(T* ptr)
{
if (ptr)
ptr->release();
}
};
template <typename T>
class AutoDisposeClear
{
public:
static void clear(T* ptr)
{
if (ptr)
ptr->dispose();
}
};
template <typename T>
class AutoDeleteClear
{
public:
static void clear(T* ptr)
{
delete ptr;
}
};
template <typename T>
class AutoArrayDeleteClear
{
public:
static void clear(T* ptr)
{
delete [] ptr;
}
};
template <typename T, typename Clear>
class AutoImpl
{
public:
AutoImpl<T, Clear>(T* aPtr = NULL)
: ptr(aPtr)
{
}
~AutoImpl()
{
Clear::clear(ptr);
}
AutoImpl<T, Clear>& operator =(T* aPtr)
{
Clear::clear(ptr);
ptr = aPtr;
return *this;
}
operator T*()
{
return ptr;
}
operator const T*() const
{
return ptr;
}
bool operator !() const
{
return !ptr;
}
bool hasData() const
{
return ptr != NULL;
}
T* operator ->()
{
return ptr;
}
T* release()
{
T* tmp = ptr;
ptr = NULL;
return tmp;
}
void reset(T* aPtr = NULL)
{
if (aPtr != ptr)
{
Clear::clear(ptr);
ptr = aPtr;
}
}
private:
// not implemented
AutoImpl<T, Clear>(AutoImpl<T, Clear>&);
void operator =(AutoImpl<T, Clear>&);
private:
T* ptr;
};
template <typename T> class AutoDispose : public AutoImpl<T, AutoDisposeClear<T> >
{
public:
AutoDispose(T* ptr = NULL)
: AutoImpl<T, AutoDisposeClear<T> >(ptr)
{
}
};
template <typename T> class AutoRelease : public AutoImpl<T, AutoReleaseClear<T> >
{
public:
AutoRelease(T* ptr = NULL)
: AutoImpl<T, AutoReleaseClear<T> >(ptr)
{
}
};
template <typename T> class AutoDelete : public AutoImpl<T, AutoDeleteClear<T> >
{
public:
AutoDelete(T* ptr = NULL)
: AutoImpl<T, AutoDeleteClear<T> >(ptr)
{
}
};
template <typename T> class AutoArrayDelete : public AutoImpl<T, AutoArrayDeleteClear<T> >
{
public:
AutoArrayDelete(T* ptr = NULL)
: AutoImpl<T, AutoArrayDeleteClear<T> >(ptr)
{
}
};
}
#endif // MY_FIRST_UDR_KIT_H