Template oddness with libpqxx

O

Owen Jacobson

Salve. I'm running into a compilation problem trying to write some
code using libpqxx[0] ( <http://pqxx.tk/> )'s pqxx::Connection and
pqxx::Transactor classes. I seem to be running into a problem with
templates that I do not understand.

I have a class that provides a Transactor (a functor that operates on a
database, in pqxx) to check for sessions. The interface is as follows:

--SessionTransaction.hpp--
#ifndef SESSIONTRANSACTION
#define SESSIONTRANSACTION

#include <string>
#include <pqxx/transactor.h>

#include "Session.hpp"

namespace lawn {
// slight naming goof here, NMF --owen
// XXX Fix this class name?
class SessionTransaction: public pqxx::Transactor {
public:
SessionTransaction (unsigned long sessionid,
const std::string& host);

void operator() (argument_type &T);

/// Sends data to mtarget, if defined.
void OnCommit ();

/// PQXX passes the transactor in as const, so we need a way to
/// communicate data back to the originating Session.
void SetSession (Session* target);

private:
bool mlogin;
unsigned long msid;
std::string mhost;
Session* mtarget;
};
}

#endif
--EOF--

The implementation for this class has not yet been written. I am
including this header in another part of the project, which *has* been
(partially) written, and attempting to compile only that file while I
complete the implementation and work out how SessionTransaction needs
to behave.

--Session.hpp--
#ifndef SESSION
#define SESSION

#include <cgicc/Cgicc.h>

#include "Database.hpp"

namespace lawn {
class Session {
public:
Session (const cgicc::Cgicc& cgi, DB_Handle db);

void SetUserid (unsigned long userid); //< called by
SessionTransaction::OnCommit, mainly
private:
bool mloggedin;
unsigned long muserid;
};
};

#endif
--EOF--

--Session.cpp--
#include <sstream>
#include <vector>

#include "Session.hpp"
#include "SessionTransaction.hpp"

namespace lawn {
Session::Session (const cgicc::Cgicc& cgi, DB_Handle db):
mloggedin (false),
muserid (0)
{
cgicc::CgiEnvironment env = cgi.getEnvironment ();
bool foundid = false;
unsigned long sessionid = 0;
std::vector<cgicc::HTTPCookie> cookies = env.getCookieList ();

for (std::vector<cgicc::HTTPCookie>::iterator i = cookies.begin ();
i != cookies.end ();
i++) {
if (i->getName () == "lawn_session") {
foundid = true; // it's possible to find an id of 0
std::stringstream idstream (i->getValue ());
idstream >> sessionid;
}
}

if (foundid) {
SessionTransaction strans (sessionid, env.getRemoteAddr ());
strans.SetSession (this);

db->Perform (strans, 1); // This is line 28 in Session.cpp
}
};
}
--EOF--

All of which conforms to the way the examples in the libpqxx
distribution seem to be written. However, upon attempting to compile
Session.cpp into Session.o, the following errors occur:

[oj@acolyte src]$ make Session.o
g++ -g -O3 -Wall -pedantic -ansi -I/usr/local/pqxx/include -c -o
Session.o Session.cpp
/usr/local/pqxx/include/pqxx/connection.h: In member function `void
pqxx::Connection::perform(const TRANSACTOR&, int) [with TRANSACTOR =
lawn::SessionTransaction]':
Session.cpp:28: instantiated from here
/usr/local/pqxx/include/pqxx/connection.h:296: `X' has incomplete type
/usr/local/pqxx/include/pqxx/connection.h:296: storage size of `X'
isn't known
/usr/local/pqxx/include/pqxx/connection.h:297: no match for call to `(
lawn::SessionTransaction) (<typeprefixerror>&)'
SessionTransaction.hpp:14: candidates are: void
lawn::SessionTransaction::eek:perator()(pqxx::Transaction&)
/usr/local/pqxx/include/pqxx/connection.h:301: invalid use of undefined
type `
const struct pqxx::in_doubt_error'
/usr/local/pqxx/include/pqxx/connection.h:40: forward declaration of
`const
struct pqxx::in_doubt_error'
/usr/local/pqxx/include/pqxx/connection.h:301: warning: `...' handler
must be
the last handler for its try block
make: *** [Session.o] Error 1

It looks to me like the compiler (G++ 3.2.2) believes that
SessionTransaction is an incomplete specialisation of something. For
hints, I had a look at the referenced section of connection.h:

--connection.h segment--
template<typename TRANSACTOR>
inline void Connection::perform(const TRANSACTOR &T,
int Attempts)
//[t4]
{
if (Attempts <= 0) return;

bool Done = false;

// Make attempts to perform T
// TODO: Differentiate between db-related exceptions and other
exceptions?
do
{
--Attempts;

// Work on a copy of T2 so we can restore the starting situation if
need be
TRANSACTOR T2(T);
try
{
typename TRANSACTOR::argument_type X(*this, T2.Name());
T2(X); // Line 296
X.Commit(); // Line 297
Done = true;
}
catch (const in_doubt_error &)
{
// Not sure whether transaction went through or not. The last
thing in
// the world that we should do now is retry.
T2.OnDoubt();
throw;
}
catch (const PGSTD::exception &e)
{
// Could be any kind of error.
T2.OnAbort(e.what());
if (Attempts <= 0) throw;
continue;
}
catch (...)
{
// Don't try to forge ahead if we don't even know what happened
T2.OnAbort("Unknown exception");
throw;
}

T2.OnCommit();
} while (!Done);
}
--End of segment--

Interestingly, other files that use connection.h but do not call
pqxx::Connection::perform (...) compile fine, as do the various
examples provided with the library.

Any thoughts, or should I take this to the libpqxx guys and leave this
july group alone?

--Owen

[0] I know where the libpqxx support group is as well and will be
posting there shortly; I suspect the problem is with my understanding
of templates, hence my post here.
 
S

Shane Neph

void operator() (argument_type &T);

what is pqxx::Transactor::argument_type?

if (foundid) {
SessionTransaction strans (sessionid, env.getRemoteAddr ());
strans.SetSession (this);

db->Perform (strans, 1); // This is line 28 in Session.cpp

if you comment out line 28, does the compiler still complain?

[oj@acolyte src]$ make Session.o
g++ -g -O3 -Wall -pedantic -ansi -I/usr/local/pqxx/include -c -o
Session.o Session.cpp
/usr/local/pqxx/include/pqxx/connection.h: In member function `void
pqxx::Connection::perform(const TRANSACTOR&, int) [with TRANSACTOR =
lawn::SessionTransaction]':
Session.cpp:28: instantiated from here
/usr/local/pqxx/include/pqxx/connection.h:296: `X' has incomplete type
/usr/local/pqxx/include/pqxx/connection.h:296: storage size of `X'
isn't known
/usr/local/pqxx/include/pqxx/connection.h:297: no match for call to `(
lawn::SessionTransaction) (<typeprefixerror>&)'
SessionTransaction.hpp:14: candidates are: void
lawn::SessionTransaction::eek:perator()(pqxx::Transaction&)

it looks like the compiler believes pqxx::Transactor::argument_type is
pqxx::Transaction?
/usr/local/pqxx/include/pqxx/connection.h:301: invalid use of undefined
type `
const struct pqxx::in_doubt_error'

invalid use of _undefined type_ pqxx::in_doubt_error
/usr/local/pqxx/include/pqxx/connection.h:40: forward declaration of
`const
struct pqxx::in_doubt_error'
/usr/local/pqxx/include/pqxx/connection.h:301: warning: `...' handler
must be
the last handler for its try block
make: *** [Session.o] Error 1

It looks like 'X' is what we need to look at.

--connection.h segment--
template<typename TRANSACTOR>
inline void Connection::perform(const TRANSACTOR &T,
int Attempts)
// Work on a copy of T2 so we can restore the starting situation if
need be
TRANSACTOR T2(T);
try
{
typename TRANSACTOR::argument_type X(*this, T2.Name());
T2(X); // Line 296
X.Commit(); // Line 297
Done = true;

Is argument_type a non-leaf class with some undefined pure virtuals?

Not fun. Good luck.
Shane
 
O

Owen Jacobson

Shane said:
what is pqxx::Transactor::argument_type?

According to the documentation, argument_type is used to define the QoS
that your transactor provides (non-transaction, normal transaction,
robust transaction at the cost of some speed) using children of
pqxx::TransactionItf. pqx::Transactor::argument_type is
pqxx::Transaction.

According to the source:

class Transactor
{
public:
...
/// Define transaction class to use as a wrapper for this code.
/** Select the quality of service for your transactor by overriding
this in
* your derived class.
*/
typedef Transaction argument_type;
....

if you comment out line 28, does the compiler still complain?

Commenting out that line does indeed allow the file to compile, which
is what led me to wonder about libpqxx's code in the first place rather
than assuming the library was beyond reproach.
invalid use of _undefined type_ pqxx::in_doubt_error

That's inside the library. There shouldn't be anything going on that'd
cause in_doubt_error to be undefined iff I call connection::perform
(...), though.
/usr/local/pqxx/include/pqxx/connection.h:40: forward declaration of
`const
struct pqxx::in_doubt_error'
/usr/local/pqxx/include/pqxx/connection.h:301: warning: `...'
handler must be
the last handler for its try block
make: *** [Session.o] Error 1

It looks like 'X' is what we need to look at.

X is, in this case, an instance of lawn::SessionTransaction, which *is*
my code.
Is argument_type a non-leaf class with some undefined pure virtuals?

*checks*

You know, the documentation leads to me wonder if this might not be the
case. The source, however, says that all pure-virtual functions in the
parent pqxx::TransactionItf class are actually defined in
pqxx::Transaction.
Not fun. Good luck.

Not at all. It looks like a simple concept on paper, too. Thanks,
Owen
 
S

Shane Neph

Something to watch for, also, is circular dependencies. They often give
hard-to-diagnose error messages similar in nature to this one, I believe (I
haven't used g++ in a long time). Draw out a directed graph of your
dependencies in this case to see if you get a circle. Forward declarations
can sometimes be helpful, though I'm not exactly a "pro" at this yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top