Skip to content

Commit

Permalink
Stop 'using namespace std'.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv committed Feb 17, 2019
1 parent c414893 commit e94529c
Show file tree
Hide file tree
Showing 96 changed files with 512 additions and 543 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Query numeric precision in a more sensible, standard way.
- Avoid "dead code" warning.
- Replace obsolete autoconf macros.
- Remove all "using namespace std".
6.3.1
- Windows compile fix (CALLBACK is a macro there).
- Work around Visual Studio 2017 not supporting ISO 646.
Expand Down
45 changes: 22 additions & 23 deletions test/test00.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;


Expand All @@ -11,17 +10,17 @@ using namespace pqxx;

namespace
{
void check(string ref, string val, string vdesc)
void check(std::string ref, std::string val, std::string vdesc)
{
PQXX_CHECK_EQUAL(val, ref, "String mismatch for " + vdesc);
}

template<typename T> inline void strconv(
string type,
std::string type,
const T &Obj,
string expected)
std::string expected)
{
const string Objstr(to_string(Obj));
const std::string Objstr(to_string(Obj));

check(expected, Objstr, type);
T NewObj;
Expand All @@ -30,13 +29,13 @@ template<typename T> inline void strconv(
}

// There's no from_string<const char *>()...
inline void strconv(string type, const char Obj[], string expected)
inline void strconv(std::string type, const char Obj[], std::string expected)
{
const string Objstr(to_string(Obj));
const std::string Objstr(to_string(Obj));
check(expected, Objstr, type);
}

const double not_a_number = numeric_limits<double>::quiet_NaN();
const double not_a_number = std::numeric_limits<double>::quiet_NaN();

struct intderef
{
Expand All @@ -59,7 +58,7 @@ void test_000()
"cursor_base::difference_type appears to be unsigned.");

const char weird[] = "foo\t\n\0bar";
const string weirdstr(weird, sizeof(weird)-1);
const std::string weirdstr(weird, sizeof(weird)-1);

// Test string conversions
strconv("const char[]", "", "");
Expand All @@ -75,12 +74,12 @@ void test_000()
long_max = std::numeric_limits<long>::max();
#endif

stringstream lminstr, lmaxstr, llminstr, llmaxstr, ullmaxstr;
lminstr.imbue(locale("C"));
lmaxstr.imbue(locale("C"));
llminstr.imbue(locale("C"));
llmaxstr.imbue(locale("C"));
ullmaxstr.imbue(locale("C"));
std::stringstream lminstr, lmaxstr, llminstr, llmaxstr, ullmaxstr;
lminstr.imbue(std::locale("C"));
lmaxstr.imbue(std::locale("C"));
llminstr.imbue(std::locale("C"));
llmaxstr.imbue(std::locale("C"));
ullmaxstr.imbue(std::locale("C"));

lminstr << long_min;
lmaxstr << long_max;
Expand All @@ -99,7 +98,7 @@ void test_000()
strconv("long", long_min, lminstr.str());
strconv("long", long_max, lmaxstr.str());
strconv("double", not_a_number, "nan");
strconv("string", string{}, "");
strconv("string", std::string{}, "");
strconv("string", weirdstr, weirdstr);
strconv("long long", 0LL, "0");
strconv("long long", llong_min, llminstr.str());
Expand All @@ -108,37 +107,37 @@ void test_000()
strconv("unsigned long long", ullong_max, ullmaxstr.str());

const char zerobuf[] = "0";
string zero;
std::string zero;
from_string(zerobuf, zero, sizeof(zerobuf)-1);
PQXX_CHECK_EQUAL(
zero,
zerobuf,
"Converting \"0\" with explicit length failed.");

const char nulbuf[] = "\0string\0with\0nuls\0";
const string nully(nulbuf, sizeof(nulbuf)-1);
string nully_parsed;
const std::string nully(nulbuf, sizeof(nulbuf)-1);
std::string nully_parsed;
from_string(nulbuf, nully_parsed, sizeof(nulbuf)-1);
PQXX_CHECK_EQUAL(nully_parsed.size(), nully.size(), "Nul truncates string.");
PQXX_CHECK_EQUAL(nully_parsed, nully, "String conversion breaks on nuls.");
from_string(nully.c_str(), nully_parsed, nully.size());
PQXX_CHECK_EQUAL(nully_parsed, nully, "Nul conversion breaks on strings.");

stringstream ss;
std::stringstream ss;
strconv("empty stringstream", ss, "");
ss << -3.1415;
strconv("stringstream", ss, ss.str());

// TODO: Test binarystring reversibility

const string pw = encrypt_password("foo", "bar");
const std::string pw = encrypt_password("foo", "bar");
PQXX_CHECK(not pw.empty(), "Encrypting a password returned no data.");
PQXX_CHECK_NOT_EQUAL(
pw,
encrypt_password("splat", "blub"),
"Password encryption is broken.");
PQXX_CHECK(
pw.find("bar") == string::npos,
pw.find("bar") == std::string::npos,
"Encrypted password contains original.");

// Test error handling for failed connections
Expand All @@ -157,7 +156,7 @@ void test_000()
"nullconnection(const char[]) is broken.");
}
{
string n;
std::string n;
nullconnection nc(n);
PQXX_CHECK_THROWS(
work w(nc),
Expand Down
12 changes: 7 additions & 5 deletions test/test01.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;


Expand All @@ -14,9 +13,10 @@ namespace
void test_001()
{
connection conn;
cout << "Connected to database." << endl
<< "Backend version: " << conn.server_version() << endl
<< "Protocol version: " << conn.protocol_version() << endl;
std::cout
<< "Connected to database." << std::endl
<< "Backend version: " << conn.server_version() << std::endl
<< "Protocol version: " << conn.protocol_version() << std::endl;

// Begin a transaction acting on our current connection. Give it a human-
// readable name so the library can include it in error messages.
Expand All @@ -34,7 +34,9 @@ void test_001()
// Dump row number and column 0 value to cout. Read the value using
// as(), which converts the field to the same type as the default value
// you give it (or returns the default value if the field is null).
cout << '\t' << to_string(c.num()) << '\t' << c[0].as(string{}) << endl;
std::cout
<< '\t' << to_string(c.num()) << '\t' << c[0].as(std::string{})
<< std::endl;
}

tx.commit();
Expand Down
5 changes: 2 additions & 3 deletions test/test02.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;


Expand All @@ -24,7 +23,7 @@ void test_002()
"Invalid connection string did not cause exception.");

// Set up connection to database
string ConnectString = "";
std::string ConnectString = "";
connection C{ConnectString};

// Start transaction within context of connection
Expand All @@ -51,7 +50,7 @@ void test_002()
R.column_table(pqxx::row::size_type(0)),
"Inconsistent answers from column_table()");

const string rcol = R.column_name(0);
const std::string rcol = R.column_name(0);
const oid crtable = R.column_table(rcol);
PQXX_CHECK_EQUAL(
crtable,
Expand Down
3 changes: 1 addition & 2 deletions test/test04.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;

// Example program for libpqxx. Send notification to self.
Expand All @@ -24,7 +23,7 @@ class TestListener final : public notification_receiver
explicit TestListener(connection_base &conn) :
notification_receiver(conn, "listen"), m_done(false) {}

virtual void operator()(const string &, int be_pid) override
virtual void operator()(const std::string &, int be_pid) override
{
m_done = true;
PQXX_CHECK_EQUAL(
Expand Down
11 changes: 5 additions & 6 deletions test/test07.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;


Expand Down Expand Up @@ -45,7 +44,7 @@ void test_007()

// Perform (an instantiation of) the UpdateYears transactor we've defined
// in the code above. This is where the work gets done.
map<int,int> conversions;
std::map<int,int> conversions;
perform(
[&conversions, &conn]()
{
Expand All @@ -60,10 +59,10 @@ void test_007()
rctype,
"Inconsistent result::column_type().");

const string rct = to_string(rctype);
const std::string rct = to_string(rctype);
PQXX_CHECK(rctype > 0, "Got strange type ID for column: " + rct);

const string rcol = R.column_name(0);
const std::string rcol = R.column_name(0);
PQXX_CHECK(not rcol.empty(), "Didn't get a name for column.");

const oid rcctype = R.column_type(rcol);
Expand Down Expand Up @@ -136,15 +135,15 @@ void test_007()
R = tx.exec0(query.c_str());
AffectedRows += R.affected_rows();
}
cout << AffectedRows << " rows updated." << endl;
std::cout << AffectedRows << " rows updated." << std::endl;
});

// Just for fun, report the exact conversions performed. Note that this
// list will be accurate even if other people were modifying the database
// at the same time; this property was established through use of the
// transactor framework.
for (auto &i: conversions)
cout << '\t' << i.first << "\t-> " << i.second << endl;
std::cout << '\t' << i.first << "\t-> " << i.second << std::endl;
}


Expand Down
13 changes: 6 additions & 7 deletions test/test10.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;


Expand All @@ -16,14 +15,14 @@ namespace
// Let's take a boring year that is not going to be in the "pqxxevents" table
const int BoringYear = 1977;

const string Table("pqxxevents");
const std::string Table("pqxxevents");


// Count events, and boring events, in table
pair<int,int> CountEvents(transaction_base &T)
std::pair<int,int> CountEvents(transaction_base &T)
{
const string EventsQuery = "SELECT count(*) FROM " + Table;
const string BoringQuery =
const std::string EventsQuery = "SELECT count(*) FROM " + Table;
const std::string BoringQuery =
EventsQuery + " WHERE year=" + to_string(BoringYear);
int EventsCount = 0,
BoringCount = 0;
Expand All @@ -34,7 +33,7 @@ pair<int,int> CountEvents(transaction_base &T)
R = T.exec1(BoringQuery);
R.front().to(BoringCount);

return make_pair(EventsCount, BoringCount);
return std::make_pair(EventsCount, BoringCount);
}


Expand All @@ -43,7 +42,7 @@ pair<int,int> CountEvents(transaction_base &T)
// performed correctly.
void Test(connection_base &C, bool ExplicitAbort)
{
pair<int,int> EventCounts;
std::pair<int,int> EventCounts;

// First run our doomed transaction. This will refuse to run if an event
// exists for our Boring Year.
Expand Down
17 changes: 8 additions & 9 deletions test/test11.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "test_helpers.hxx"

using namespace std;
using namespace pqxx;


Expand All @@ -15,15 +14,15 @@ void test_011()
{
connection conn;
work tx{conn};
const string Table = "pg_tables";
const std::string Table = "pg_tables";

result R( tx.exec("SELECT * FROM " + Table) );

// Print column names
for (pqxx::row::size_type c = 0; c < R.columns(); ++c)
{
string N = R.column_name(c);
cout << c << ":\t" << N << endl;
std::string N = R.column_name(c);
std::cout << c << ":\t" << N << std::endl;
PQXX_CHECK_EQUAL(R.column_number(N), c, "Inconsistent column numbers.");
}

Expand All @@ -33,7 +32,7 @@ void test_011()
PQXX_CHECK_EQUAL(R[0].rownumber(), 0u, "Row 0 has wrong number.");

if (R.size() < 2)
cout << "(Only one row in table.)" << endl;
std::cout << "(Only one row in table.)" << std::endl;
else
PQXX_CHECK_EQUAL(R[1].rownumber(), 1u, "Row 1 has wrong number.");

Expand All @@ -51,15 +50,15 @@ void test_011()

for (pqxx::row::size_type c = 0; c < R[0].size(); ++c)
{
string N = R.column_name(c);
std::string N = R.column_name(c);

PQXX_CHECK_EQUAL(
string{R[0].at(c).c_str()},
std::string{R[0].at(c).c_str()},
R[0].at(N).c_str(),
"Field by name != field by number.");

PQXX_CHECK_EQUAL(
string{R[0][c].c_str()},
std::string{R[0][c].c_str()},
R[0][N].c_str(),
"at() is inconsistent with operator[].");

Expand All @@ -73,7 +72,7 @@ void test_011()
}
else
{
cout << "(Table is empty.)" << endl;
std::cout << "(Table is empty.)" << std::endl;
}
}

Expand Down

0 comments on commit e94529c

Please sign in to comment.