Function with arguments

You find a bug using QxOrm library and you know how to fix it : submit a patch on this forum

Function with arguments

Postby alqfr » Thu Dec 06, 2012 8:28 pm

After registering a member function that accepts a single int parameter and returns an int value, and running a test driver that inputs a vector<boost::any> of size 1, a run-time error of the following is observed:
[QxOrm] Invalid parameter at position '1'

After adding some cout statements in the macro named QX_FUNCTION_GET_PARAM_TYPE_ANY, I found that the typeid of P in the following statement
try { p = boost::any_cast<P>(params[PARAMCOUNT - 1]); }
is actually boost::value_initialized<int> and not int itself. This is because P as an input type is set as such in the preceding macro QX_FUNCTION_FETCH_PARAM.

This can be corrected by adding a static_cast to this statement in QX_FUNCTION_FETCH_PARAM:
{ qx_bool bTmp = qx::function::detail::FCT(params, static_cast< TYPE& >(VALUE), pThis);

I tested it and it is producing the right result now.
alqfr
 
Posts: 17
Joined: Thu Jul 19, 2012 3:47 pm

Re: Function with arguments

Postby qxorm » Fri Dec 07, 2012 8:55 am

Thanks for your patch, but before applying it, I would like to understand what is the problem.
Please, could you provide some code :
* the function signature you want to register (and the function implementation if not too long) ;
* the way you have registered the function into QxOrm context ;
* an example of call.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Function with arguments

Postby alqfr » Fri Dec 07, 2012 2:52 pm

Yes, of course. This is the sample code I have. You don't need to set up a database for testing the following code.

Code: Select all
// .hpp file: Note that the member method should have been a static method, but I have problems
// registering static methods. See another post in the forum about registering static methods.
class QX_DLL_EXPORT TestTable
{
  public:
    std::string id_;
    virtual ~TestTable() {}
    int square(int i);
};


Code: Select all
// .cpp file
#include "TestTable.h"
#include <QxMemLeak.h>
QX_REGISTER_CPP_TEST(TestTable);
namespace qx {

template <> void register_class(QxClass<TestTable> & t)
{
    cout << "Setting table name..." << endl;
    t.setName("testTable");
    t.id(&TestTable::id_, "id");
    // If TestTable::square is declared static, the following statement will not compile.
   //  Now  being non-static, it can compile, but it will fail in run-time without my patch.
    t.fct_1<int, int>(&TestTable::square, "square");
}

} // namespace qx

int TestTable::square(int i)
{
    return i * i;
}


Code: Select all
// test driver in main()
TestTablePtr tablePtr(qx::create_nude_ptr<TestTable>("TestTable"));
// some irrelevant code to populate the object
qx::IxFunctionX *fctList = type->getFctMemberX();
// 2 different ways to invoke the function.
string key = "square";   // in my full test driver, key will be reused - hence not a const char* here
if (!fctList->exist(key.c_str())) {
    cout << key.c_str() << " non-existent" << endl;
    throw;
}
qx::IxFunction_ptr fct = fctList->getByKey(key.c_str());
int a = 3;
vector<boost::any> params(1);
params[0] = a;
boost::any ret;
bool success = qx::QxClassX::invoke("TestTable", "square", *tablePtr, params, &ret);
int intRet = boost::any_cast<int>(ret);
cout << "square(" << a << ") = " << intRet << endl;

success = fct->invoke(tablePtr.get(), params, &ret);
intRet = boost::any_cast<int>(ret);
cout << "square(" << a << ") = " << intRet << endl;
alqfr
 
Posts: 17
Joined: Thu Jul 19, 2012 3:47 pm

Re: Function with arguments

Postby qxorm » Tue Dec 11, 2012 1:21 pm

Done, you can test it with the following BETA version :
http://www.qxorm.com/version/QxOrm_1.2.5_BETA_04.zip
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Function with arguments

Postby alqfr » Wed Dec 19, 2012 9:54 pm

The new 1.2.5-BETA-4 package aims to resolve 2 issues at once:
(i) registration/invocation of a static method (reported here viewtopic.php?f=2&t=340 -- the issue has been satisfactorily resolved); and
(ii) the invocation of functions with arguments (this thread).

The new package fixes one of my test cases involving a method with 1 integer argument, but it gave an error when I tried to invoke a method that takes in a string argument and a const char* argument as follows:
Code: Select all
template <> void register_class(QxClass<TestTable> & t)
{
    t.fct_2<string, const string&, char*>(&TestTable::concat, "concat");
}

string TestTable::concat(const string& sep, const char *str)
{
    return id_ + sep + str;
}


This example was working after my tentative fix to 1.2.4, and also after I have replaced my fix with yours in QxFunctionMacro.h (in 1.2.4 also), but it failed with 1.2.5-BETA-4 with the following run-time error:

[QxOrm] Invalid parameter at position '2'
ASSERT: "false" in file ../../include/QxFunction/QxFunction_2.h, line 85

So, it seems that other fixes in 1.2.5-BETA-4 might have interfered. Otherwise, I can confirm that the first parameter (of const string&) is read in correctly (whether I register it with const string& or string type in my call to t.fct_2).

Also, in either 1.2.4 or 1.2.5, registering the second parameter as const char* as in
t.fct_2<string, const string&, const char*>(&TestTable::concat, "concat");
would not even compile.
alqfr
 
Posts: 17
Joined: Thu Jul 19, 2012 3:47 pm

Re: Function with arguments

Postby qxorm » Thu Dec 20, 2012 10:50 am

Could you please provide an example of call (the way you initialize parameters and call the invoke method) ?

EDIT : it works for me :
Code: Select all
std::string concat(const std::string & s1, const char * c2) { return (s1 + c2); }

...

qx::IxFunction_ptr pFct = qx::function::bind_fct_2<void, std::string, const std::string &, char *>(& concat);
std::vector<boost::any> params;
char * c = "bbbb";
params.push_back(std::string("aa"));
params.push_back(c);
bInvoke = pFct->invoke(params, (& any_ret));

Be careful with the type in the vector of params : if you pass a const char * instead of char * => boost::any_cast will fail, I think this is your error.
qxorm
Site Admin
 
Posts: 481
Joined: Mon Apr 12, 2010 7:45 am

Re: Function with arguments

Postby alqfr » Wed Jan 02, 2013 7:44 pm

Indeed, I somehow was trying to pass in a const char* variable when the previous version of my code had char* instead. Everything is working fine now after I have reverted to using char*. Thanks.
alqfr
 
Posts: 17
Joined: Thu Jul 19, 2012 3:47 pm


Return to QxOrm - Submit a patch

Who is online

Users browsing this forum: No registered users and 4 guests

cron