From 90e5bee516554805707436ccf9cb2b2e7d2c207a Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 16 Nov 2011 11:51:31 +0200 Subject: Add auto_handle class template --- odb/mssql/auto-handle.cxx | 19 ++++++++++++ odb/mssql/auto-handle.hxx | 78 +++++++++++++++++++++++++++++++++++++++++++++++ odb/mssql/makefile | 1 + odb/mssql/mssql-fwd.hxx | 9 ++++-- 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 odb/mssql/auto-handle.cxx create mode 100644 odb/mssql/auto-handle.hxx diff --git a/odb/mssql/auto-handle.cxx b/odb/mssql/auto-handle.cxx new file mode 100644 index 0000000..733ec92 --- /dev/null +++ b/odb/mssql/auto-handle.cxx @@ -0,0 +1,19 @@ +// file : odb/mssql/auto-handle.cxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#include +#include + +namespace odb +{ + namespace mssql + { + void + free_handle (SQLHANDLE h, SQLSMALLINT htype) + { + SQLFreeHandle (htype, h); + } + } +} diff --git a/odb/mssql/auto-handle.hxx b/odb/mssql/auto-handle.hxx new file mode 100644 index 0000000..d545629 --- /dev/null +++ b/odb/mssql/auto-handle.hxx @@ -0,0 +1,78 @@ +// file : odb/mssql/auto-handle.hxx +// author : Constantin Michael +// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC +// license : ODB NCUEL; see accompanying LICENSE file + +#ifndef ODB_MSSQL_AUTO_HANDLE_HXX +#define ODB_MSSQL_AUTO_HANDLE_HXX + +#include + +#include +#include + +#include + +namespace odb +{ + namespace mssql + { + LIBODB_MSSQL_EXPORT void + free_handle (SQLHANDLE, SQLSMALLINT htype); + + template + class auto_handle + { + public: + auto_handle (SQLHANDLE h = 0) + : h_ (h) + { + } + + ~auto_handle () + { + if (h_ != 0) + free_handle (h_, htype); + } + + operator SQLHANDLE () const + { + return h_; + } + + SQLHANDLE + get () const + { + return h_; + } + + SQLHANDLE + release () + { + SQLHANDLE h (h_); + h_ = 0; + return h; + } + + void + reset (SQLHANDLE h = 0) + { + if (h_ != 0) + free_handle (h_, htype); + + h_ = h; + } + + private: + auto_handle (const auto_handle&); + auto_handle& operator= (const auto_handle&); + + private: + SQLHANDLE h_; + }; + } +} + +#include + +#endif // ODB_MSSQL_AUTO_HANDLE_HXX diff --git a/odb/mssql/makefile b/odb/mssql/makefile index 98749c1..00342f1 100644 --- a/odb/mssql/makefile +++ b/odb/mssql/makefile @@ -7,6 +7,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make cxx := \ +auto-handle.cxx \ exceptions.cxx diff --git a/odb/mssql/mssql-fwd.hxx b/odb/mssql/mssql-fwd.hxx index 434edb1..403e2a8 100644 --- a/odb/mssql/mssql-fwd.hxx +++ b/odb/mssql/mssql-fwd.hxx @@ -13,10 +13,15 @@ // in public headers. // #ifdef _WIN32 -typedef long SQLINTEGER; -typedef unsigned long SQLUINTEGER; +typedef long SQLINTEGER; +typedef unsigned long SQLUINTEGER; #endif +typedef short SQLSMALLINT; +typedef unsigned short SQLUSMALLINT; + +typedef void* SQLHANDLE; + #include #endif // ODB_MSSQL_MSSQL_FWD_HXX -- cgit v1.1