From 42c93996398eab00c343b221c6d514d715e76a63 Mon Sep 17 00:00:00 2001 From: Paul Reeves Date: Wed, 25 Jan 2023 14:53:02 +0100 Subject: [PATCH] Separate templates out into their own header --- src/MyFirstUDRKit.h | 161 +---------------------------------- src/UdrCppTemplates.h | 189 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 158 deletions(-) create mode 100644 src/UdrCppTemplates.h diff --git a/src/MyFirstUDRKit.h b/src/MyFirstUDRKit.h index 384afa6..7ee1ecb 100644 --- a/src/MyFirstUDRKit.h +++ b/src/MyFirstUDRKit.h @@ -8,172 +8,17 @@ #include "ibase.h" #include "firebird/UdrCppEngine.h" +#include "UdrCppTemplates.h" #include #include - -//// DOC NOTE This is the basic stuff from UdrCppExample.h - seems to be declared in all UDR libraries - -namespace -{ - template - class AutoReleaseClear - { - public: - static void clear(T* ptr) - { - if (ptr) - ptr->release(); - } - }; - - template - class AutoDisposeClear - { - public: - static void clear(T* ptr) - { - if (ptr) - ptr->dispose(); - } - }; - - template - class AutoDeleteClear - { - public: - static void clear(T* ptr) - { - delete ptr; - } - }; - - template - class AutoArrayDeleteClear - { - public: - static void clear(T* ptr) - { - delete [] ptr; - } - }; - - template - class AutoImpl - { - public: - AutoImpl(T* aPtr = NULL) - : ptr(aPtr) - { - } - - ~AutoImpl() - { - Clear::clear(ptr); - } - - AutoImpl& 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(AutoImpl&); - void operator =(AutoImpl&); - - private: - T* ptr; - }; - - template class AutoDispose : public AutoImpl > - { - public: - AutoDispose(T* ptr = NULL) - : AutoImpl >(ptr) - { - } - }; - - template class AutoRelease : public AutoImpl > - { - public: - AutoRelease(T* ptr = NULL) - : AutoImpl >(ptr) - { - } - }; - - template class AutoDelete : public AutoImpl > - { - public: - AutoDelete(T* ptr = NULL) - : AutoImpl >(ptr) - { - } - }; - - template class AutoArrayDelete : public AutoImpl > - { - public: - AutoArrayDelete(T* ptr = NULL) - : AutoImpl >(ptr) - { - } - }; -} - - // This should be used in only one of the UDR library files. // Build must export firebird_udr_plugin function. -// + FB_UDR_IMPLEMENT_ENTRY_POINT -#endif // MY_FIRST_UDR_KIT_H +#endif diff --git a/src/UdrCppTemplates.h b/src/UdrCppTemplates.h new file mode 100644 index 0000000..85ce3dd --- /dev/null +++ b/src/UdrCppTemplates.h @@ -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 + * 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 +//#include + +namespace +{ + template + class AutoReleaseClear + { + public: + static void clear(T* ptr) + { + if (ptr) + ptr->release(); + } + }; + + template + class AutoDisposeClear + { + public: + static void clear(T* ptr) + { + if (ptr) + ptr->dispose(); + } + }; + + template + class AutoDeleteClear + { + public: + static void clear(T* ptr) + { + delete ptr; + } + }; + + template + class AutoArrayDeleteClear + { + public: + static void clear(T* ptr) + { + delete [] ptr; + } + }; + + template + class AutoImpl + { + public: + AutoImpl(T* aPtr = NULL) + : ptr(aPtr) + { + } + + ~AutoImpl() + { + Clear::clear(ptr); + } + + AutoImpl& 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(AutoImpl&); + void operator =(AutoImpl&); + + private: + T* ptr; + }; + + template class AutoDispose : public AutoImpl > + { + public: + AutoDispose(T* ptr = NULL) + : AutoImpl >(ptr) + { + } + }; + + template class AutoRelease : public AutoImpl > + { + public: + AutoRelease(T* ptr = NULL) + : AutoImpl >(ptr) + { + } + }; + + template class AutoDelete : public AutoImpl > + { + public: + AutoDelete(T* ptr = NULL) + : AutoImpl >(ptr) + { + } + }; + + template class AutoArrayDelete : public AutoImpl > + { + public: + AutoArrayDelete(T* ptr = NULL) + : AutoImpl >(ptr) + { + } + }; +} + + + +#endif // MY_FIRST_UDR_KIT_H