Beispiel #1
0
  void Session::loadService(const std::string &moduleName, const std::string& renameModule, const AnyReferenceVector& args)
  {
    size_t separatorPos = moduleName.find_last_of(".");
    std::string package = moduleName.substr(0, separatorPos);
    std::string factory = moduleName.substr(separatorPos + 1);

    std::string rename = renameModule;
    if (rename.empty())
      rename = factory;
    qi::AnyModule p = qi::import(package);

    AnyReferenceVector fullargs;
    SessionPtr thisptr = shared_from_this();
    fullargs.push_back(AnyReference::from(thisptr));
    fullargs.insert(fullargs.end(), args.begin(), args.end());

    int id = p.metaObject().findMethod(factory, fullargs);
    qi::Future<AnyReference> ret;
    if (id > 0)
      ret = p.metaCall(factory, fullargs);
    else
      ret = p.metaCall(factory, args);
    qi::AnyValue retval(ret.value(), false, true);
    registerService(rename, retval.to<qi::AnyObject>());
  }
Beispiel #2
0
TEST(Value, STL)
{
  std::vector<int> v;
  AutoAnyReference gv(v);
  gv.append(1);
  gv.append(3);
  gv.append(2);
  std::vector<int> w;
#if defined(_MSC_VER) && _MSC_VER <= 1500
  // Yup, it's broken.
  for (unsigned i=0; i<3; ++i)
    v.push_back(gv[i].toInt());
#else
  // seems there are overloads for push_back, need to explicitly cast to signature
  std::for_each(gv.begin(), gv.end(),
    boost::lambda::bind((void (std::vector<int>::*)(const int&))&std::vector<int>::push_back,
      boost::ref(w),
      boost::lambda::bind(&AnyReference::toInt, boost::lambda::_1)));
#endif
  ASSERT_EQ(3u, w.size());
  ASSERT_EQ(v, w);
  AnyIterator mine = std::min_element(gv.begin(), gv.end());
  ASSERT_EQ(1, (*mine).toInt());
  mine = std::find_if(gv.begin(), gv.end(),
    boost::lambda::bind(&AnyReference::toInt, boost::lambda::_1) == 3);
  ASSERT_EQ(3, (*mine).toInt());
  (*mine).setInt(4);
  ASSERT_EQ(4, v[1]);
  mine = std::find_if(gv.begin(), gv.end(),
    boost::lambda::bind(&AnyReference::toInt, boost::lambda::_1) == 42);
  ASSERT_EQ(mine, gv.end());

//TODO: we do not want that anymore?
  std::vector<int> v2;
  v2.push_back(10);
  v2.push_back(1);
  v2.push_back(100);
  // v has correct size
  for (unsigned int i = 0; i < v2.size(); ++i) {
    gv[i].update(v2[i]);
  }
  //std::copy(v2.begin(), v2.end(), gv.begin());
  ASSERT_EQ(v2, v);
  // copy other-way-round requires cast from AnyReference to int


  AnyReferenceVector vg;
  for (unsigned i=0; i<v.size(); ++i)
    vg.push_back(AnyReference::from(v[i]));
  std::sort(vg.begin(), vg.end());
  ASSERT_EQ(321, vg[0].toInt() + vg[1].toInt()*2 + vg[2].toInt() * 3);
}