473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template oddness with libpqxx

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::operator()(pqxx::Transac tion&)
/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.
Jul 19 '05 #1
3 3533
> 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::operator()(pqxx::Transac tion&)
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
Jul 19 '05 #2
Shane Neph wrote:
void operator() (argument_type &T);
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 (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?


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.
/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


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.
--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?


*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
Jul 19 '05 #3
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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
1
by: Toby White | last post by:
Can ayone tell me what the expected behaviour of the following snippet is: <?xml version="1.0" encoding="UTF-8" ?> <html xmlns="http://www.w3.org/1999/xhtml"...
3
by: John Abel | last post by:
Hi, I'm running Python 2.3.5/2.4.2 on OSX 10.4.2, and am trying to run CGI scripts using the builtin Apache. For ease, I've symlinked my custom modules into the...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
1
by: cedarmillxing215 | last post by:
The oddness is on the last line of code. This is stripped down as far as I could figure, and I provide several similar examples that work as expected. I have no idea whether this is a compiler...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.