Skip to content

Commit 4f32da1

Browse files
committed
Add my Utils.h file to define nullptr on C++98 and C++03 compilers
1 parent 3f3b174 commit 4f32da1

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ set(SQLITECPP_INC
122122
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
123123
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
124124
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h
125+
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Utils.h
125126
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/VariadicBind.h
126127
)
127128
source_group(inc FILES ${SQLITECPP_INC})

include/SQLiteCpp/Database.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* @ingroup SQLiteCpp
44
* @brief Management of a SQLite Database Connection.
55
*
6-
* Copyright (c) 2012-2016 Sebastien Rombauts ([email protected])
6+
* Copyright (c) 2012-2017 Sebastien Rombauts ([email protected])
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)
1010
*/
1111
#pragma once
1212

1313
#include <SQLiteCpp/Column.h>
14+
#include <SQLiteCpp/Utils.h> // definition of nullptr for C++98/C++03 compilers
1415

1516
#include <string.h>
1617

@@ -95,7 +96,7 @@ class Database
9596
Database(const char* apFilename,
9697
const int aFlags = SQLite::OPEN_READONLY,
9798
const int aBusyTimeoutMs = 0,
98-
const char* apVfs = NULL);
99+
const char* apVfs = nullptr);
99100

100101
/**
101102
* @brief Open the provided database UTF-8 filename.
@@ -309,10 +310,10 @@ class Database
309310
* @param[in] aNbArg Number of arguments in the function
310311
* @param[in] abDeterministic Optimize for deterministic functions (most are). A random number generator is not.
311312
* @param[in] apApp Arbitrary pointer of user data, accessible with sqlite3_user_data().
312-
* @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal NULL)
313-
* @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
314-
* @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
315-
* @param[in] apDestroy If not NULL, then it is the destructor for the application data pointer.
313+
* @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal nullptr)
314+
* @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc nullptr)
315+
* @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc nullptr)
316+
* @param[in] apDestroy If not nullptr, then it is the destructor for the application data pointer.
316317
*
317318
* @throw SQLite::Exception in case of error
318319
*/
@@ -337,10 +338,10 @@ class Database
337338
* @param[in] aNbArg Number of arguments in the function
338339
* @param[in] abDeterministic Optimize for deterministic functions (most are). A random number generator is not.
339340
* @param[in] apApp Arbitrary pointer of user data, accessible with sqlite3_user_data().
340-
* @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal NULL)
341-
* @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
342-
* @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc NULL)
343-
* @param[in] apDestroy If not NULL, then it is the destructor for the application data pointer.
341+
* @param[in] apFunc Pointer to a C-function to implement a scalar SQL function (apStep & apFinal nullptr)
342+
* @param[in] apStep Pointer to a C-function to implement an aggregate SQL function (apFunc nullptr)
343+
* @param[in] apFinal Pointer to a C-function to implement an aggregate SQL function (apFunc nullptr)
344+
* @param[in] apDestroy If not nullptr, then it is the destructor for the application data pointer.
344345
*
345346
* @throw SQLite::Exception in case of error
346347
*/
@@ -368,7 +369,7 @@ class Database
368369
* @note UTF-8 text encoding assumed.
369370
*
370371
* @param[in] apExtensionName Name of the shared library containing extension
371-
* @param[in] apEntryPointName Name of the entry point (NULL to let sqlite work it out)
372+
* @param[in] apEntryPointName Name of the entry point (nullptr to let sqlite work it out)
372373
*
373374
* @throw SQLite::Exception in case of error
374375
*/

include/SQLiteCpp/Utils.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @file Utils.h
3+
* @ingroup SQLiteCpp
4+
* @brief Shared utility macros and functions.
5+
*
6+
* Copyright (c) 2013-2017 Sebastien Rombauts ([email protected])
7+
*
8+
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
9+
* or copy at http://opensource.org/licenses/MIT)
10+
*/
11+
#pragma once
12+
13+
#include <cstddef>
14+
15+
/**
16+
* @brief A macro to disallow the copy constructor and operator= functions.
17+
*
18+
* This should be used in the private: declarations for a class
19+
*
20+
* @param[in] TypeName Class name to protect
21+
*/
22+
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
23+
TypeName(const TypeName&); \
24+
void operator=(const TypeName&)
25+
26+
#ifdef _MSC_VER
27+
#if _MSC_VER < 1600
28+
/// A macro to enable the use of the nullptr keyword (NULL on older MSVC compilers, as they do not accept "nullptr_t")
29+
#ifndef nullptr
30+
#define nullptr NULL
31+
#endif // nullptr
32+
#endif // _MSC_VER < 1600
33+
#else // _MSC_VER
34+
#if (__cplusplus < 201103L) && !defined(__GXX_EXPERIMENTAL_CXX0X__) // before C++11 on GCC4.7 and Visual Studio 2010
35+
#ifndef HAVE_NULLPTR
36+
#define HAVE_NULLPTR ///< A macro to avoid double definition of nullptr
37+
/**
38+
* @brief nullptr_t is the type of the null pointer literal, nullptr.
39+
*/
40+
class nullptr_t {
41+
public:
42+
template<typename T>
43+
inline operator T* () const { ///< convertible to any type of null non-member pointer...
44+
return 0;
45+
}
46+
47+
template<typename C, typename T>
48+
inline operator T C::* () const { ///< convertible to any type of null member pointer...
49+
return 0;
50+
}
51+
52+
private:
53+
void operator&() const; ///< Can't take address of nullptr NOLINT
54+
};
55+
56+
/**
57+
* @brief Better way to enable nullptr on older GCC/Clang compilers
58+
*/
59+
const nullptr_t nullptr = {};
60+
#endif // HAVE_NULLPTR
61+
#endif // (__cplusplus < 201103L) && !defined(__GXX_EXPERIMENTAL_CXX0X__)
62+
#endif // _MSC_VER
63+
64+
// A macro for snprintf support in Visual Studio
65+
#if _MSC_VER
66+
#define snprintf _snprintf
67+
#endif

src/Database.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @ingroup SQLiteCpp
44
* @brief Management of a SQLite Database Connection.
55
*
6-
* Copyright (c) 2012-2016 Sebastien Rombauts ([email protected])
6+
* Copyright (c) 2012-2017 Sebastien Rombauts ([email protected])
77
*
88
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
99
* or copy at http://opensource.org/licenses/MIT)
@@ -53,8 +53,8 @@ int getLibVersionNumber() noexcept // nothrow
5353
Database::Database(const char* apFilename,
5454
const int aFlags /* = SQLite::OPEN_READONLY*/,
5555
const int aBusyTimeoutMs /* = 0 */,
56-
const char* apVfs /* = NULL*/) :
57-
mpSQLite(NULL),
56+
const char* apVfs /* = nullptr*/) :
57+
mpSQLite(nullptr),
5858
mFilename(apFilename)
5959
{
6060
const int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, apVfs);
@@ -75,10 +75,10 @@ Database::Database(const std::string& aFilename,
7575
const int aFlags /* = SQLite::OPEN_READONLY*/,
7676
const int aBusyTimeoutMs /* = 0 */,
7777
const std::string& aVfs /* = "" */) :
78-
mpSQLite(NULL),
78+
mpSQLite(nullptr),
7979
mFilename(aFilename)
8080
{
81-
const int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? NULL : aVfs.c_str());
81+
const int ret = sqlite3_open_v2(aFilename.c_str(), &mpSQLite, aFlags, aVfs.empty() ? nullptr : aVfs.c_str());
8282
if (SQLITE_OK != ret)
8383
{
8484
const SQLite::Exception exception(mpSQLite, ret); // must create before closing
@@ -126,7 +126,7 @@ void Database::setBusyTimeout(const int aBusyTimeoutMs)
126126
// Shortcut to execute one or multiple SQL statements without results (UPDATE, INSERT, ALTER, COMMIT, CREATE...).
127127
int Database::exec(const char* apQueries)
128128
{
129-
const int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
129+
const int ret = sqlite3_exec(mpSQLite, apQueries, nullptr, nullptr, nullptr);
130130
check(ret);
131131

132132
// Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE only)
@@ -217,12 +217,12 @@ void Database::loadExtension(const char* apExtensionName, const char *apEntryPoi
217217

218218
throw std::runtime_error("sqlite extensions are disabled");
219219
#else
220-
#ifdef SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION // Since SQLite 3.13 (2016-05-18):
220+
#ifdef SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION // Since SQLite 3.13 (2017-05-18):
221221
// Security warning:
222222
// It is recommended that the SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION method be used to enable only this interface.
223223
// The use of the sqlite3_enable_load_extension() interface should be avoided to keep the SQL load_extension()
224224
// disabled and prevent SQL injections from giving attackers access to extension loading capabilities.
225-
int ret = sqlite3_db_config(mpSQLite, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, NULL);
225+
int ret = sqlite3_db_config(mpSQLite, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, nullptr);
226226
#else
227227
int ret = sqlite3_enable_load_extension(mpSQLite, 1);
228228
#endif

0 commit comments

Comments
 (0)