Overview
Features
Download
Documentation
Community
Add-Ons & Services

problem with useRef on complex data types in v. 1.5 [solved]

A general discussion forum.

problem with useRef on complex data types in v. 1.5 [solved]

Postby mkaut » 19 Nov 2012, 13:43

Hello,

I have a problem with useRef(const & MyType), where MyType is my custom class, with TypeHandlers, as described in section "Complex Data Types" of the guide. Note that I am using version 1.5.

The code builds (and works) fine with non-constant objects, using both use(&MyType) and useRef(&MyType), but fails when I try to read from a constant object using useRef(const & MyType).
The gcc log is:
Code: Select all
||=== MySQL with POCO, Debug ===|
.../Poco/Data/Binding.h|110|  required from ‘void Poco::Data::Binding<T>::bind(std::size_t) [with T = const std::vector<MyType>; std::size_t = unsigned int]’|
.../main.cpp|235|  required from here|
.../Poco/Data/TypeHandler.h|143|error: call of overloaded ‘bind(std::size_t&, const std::vector<MyType>&, Poco::Data::AbstractBinder::Direction&)’ is ambiguous|
.../Poco/Data/TypeHandler.h|143|note: candidates are:|
.../Poco/Data/AbstractBinder.h|339|note: void Poco::Data::AbstractBinder::bind(std::size_t, const Poco::Any&, Poco::Data::AbstractBinder::Direction)|
.../Poco/Data/AbstractBinder.h|342|note: void Poco::Data::AbstractBinder::bind(std::size_t, const Poco::Dynamic::Var&, Poco::Data::AbstractBinder::Direction)|
||=== Build finished: 6 errors, 0 warnings ===|

To me, this looks like a bug in the library, possibly caused by the change from Poco::Any to Poco::Dynamic::Var - or am I wrong and it is my error?

Thanks.

Michal
Last edited by mkaut on 30 Nov 2012, 11:42, edited 1 time in total.
mkaut
 
Posts: 19
Joined: 17 Oct 2012, 15:22

Re: problem with useRef on complex data types in v. 1.5

Postby alex » 26 Nov 2012, 07:16

please post code causing this and I'll look into it.
alex
 
Posts: 1102
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: problem with useRef on complex data types in v. 1.5

Postby mkaut » 29 Nov 2012, 12:00

Here is the smallest example I could think of (still quite big, sorry):
Code: Select all
#include "Poco/Data/Session.h"
#include "Poco/Data/MySQL/Connector.h"

namespace PD = Poco::Data;
using namespace PD::Keywords;

class Params {
public:
   double load;
   Params () : load(0.0) {}
};

namespace Poco {
namespace Data {
template <>
class TypeHandler<class Params> {
public:
   static std::size_t size() {
      return 1;
   }
   static void bind(std::size_t pos, const Params& obj,
                    AbstractBinder* pBinder, AbstractBinder::Direction dir) {
      TypeHandler<double>::bind(pos++, obj.load, pBinder, dir);
   }
   static void prepare(std::size_t pos, Params& obj,
                       AbstractPreparator* pPreparator) {
      TypeHandler<double>::prepare(pos++, obj.load, pPreparator);
   }
   static void extract(std::size_t pos, Params& obj,
                       const Params& defVal, AbstractExtractor* pExt) {
      double load;
      TypeHandler<double>::extract(pos++, load, defVal.load, pExt);
      obj.load = load;
   }
};
}} // namespace Poco::Data

class DataRows {
public:
   std::vector<Params> params;
   void send_to_dbase(PD::Session & dBase) const {
      dBase << "DELETE FROM Params", now;
      dBase << "INSERT INTO Params VALUES(?)", useRef(params), now;
   }
};

int main(int argc, char* argv[])
{
   PD::MySQL::Connector::registerConnector();
   std::string const conString = "host=localhost;db=test;user=michal";
   PD::Session ses(PD::MySQL::Connector::KEY, conString);

   DataRows dRows;
   dRows.params.resize(5);
   for (int i = 0; i < 5; ++i)
      dRows.params[i].load = i;

   dRows.send_to_dbase(ses);

   PD::MySQL::Connector::unregisterConnector();
}

This gives the following error message (gcc 4.7.1):
Code: Select all
/usr/local/include/Poco/Data/Binding.h|110|  required from ‘void Poco::Data::Binding<T>::bind(std::size_t) [with T = const std::vector<Params>; std::size_t = unsigned int]’|
<my_path>/main.cpp|61|  required from here|
/usr/local/include/Poco/Data/TypeHandler.h|143|error: call of overloaded ‘bind(std::size_t&, const std::vector<Params>&, Poco::Data::AbstractBinder::Direction&)’ is ambiguous|
/usr/local/include/Poco/Data/TypeHandler.h|143|note: candidates are:|
/usr/local/include/Poco/Data/AbstractBinder.h|339|note: void Poco::Data::AbstractBinder::bind(std::size_t, const Poco::Any&, Poco::Data::AbstractBinder::Direction)|
/usr/local/include/Poco/Data/AbstractBinder.h|342|note: void Poco::Data::AbstractBinder::bind(std::size_t, const Poco::Dynamic::Var&, Poco::Data::AbstractBinder::Direction)|
||=== Build finished: 6 errors, 0 warnings ===|

If I remove the `const' from DataRows::send_to_dbase(), the code both builds and runs as expected.

Hope this helps.

Michal
mkaut
 
Posts: 19
Joined: 17 Oct 2012, 15:22

Re: problem with useRef on complex data types in v. 1.5

Postby alex » 29 Nov 2012, 19:06

That's by design:

1) You define TypeHandler<Params>; the code calls useRef(const std:vector<Params>), Binding<std::vector<T> >::bind() specialization has TypeHandler<T>::bind() call, which does not match and compiler resorts to ambiguous Any/Var as the only left candidates for T.

2) When you remove const, you call useRef(std:vector<Params>) the Binding<std::vector<T> >::bind() resolves TypeHandler<Params>::bind() call and all is good.

Since the TypeHandler has non-const references in it, trying to treat it as 100% const is doomed to fail. Perhaps breaking down TypeHandler into TypeBinder/Extractor/Preparator would be a good idea, but that is a topic for some other discussion.
alex
 
Posts: 1102
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: problem with useRef on complex data types in v. 1.5

Postby mkaut » 30 Nov 2012, 11:41

Hello Alex,

thanks for the explanation.

Since I like having const on methods that do not change the object (such as output methods), I guess I will simply use const_cast to strip the const-ness where it creates problems.

Marking this as solved.

Michal
mkaut
 
Posts: 19
Joined: 17 Oct 2012, 15:22


Return to General Discussion

Who is online

Users browsing this forum: waynix and 1 guest