<commit_before>/*
 * Copyright 2016 CodiLime
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#ifndef VELES_DBIF_UNIVERSE
#define VELES_DBIF_UNIVERSE

#include <QObject>
#include <QCoreApplication>
#include <QPointer>

#include "dbif/types.h"
#include "dbif/method.h"
#include "dbif/info.h"
#include "dbif/promise.h"
#include "dbif/error.h"

namespace veles {
namespace dbif {

class ObjectHandleBase {
  PInfoReply baseSyncGetInfo(PInfoRequest req) {
    QPointer<InfoPromise> promise = getInfo(req);
    PInfoReply res;
    PError err;
    QObject::connect(promise, &InfoPromise::gotInfo, [&res] (PInfoReply reply) {
      res = reply;
    });
    QObject::connect(promise, &InfoPromise::gotError, [&err] (PError error) {
      err = error;
    });
    while (true) {
      if (res) {
        if (!promise.isNull())
          delete &*promise;
        return res;
      }
      if (err) {
        if (!promise.isNull())
          delete &*promise;
        throw err;
      }
      QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
    }
  }

  PMethodReply baseSyncRunMethod(PMethodRequest req) {
    QPointer<MethodResultPromise> promise = runMethod(req);
    PMethodReply res;
    PError err;
    QObject::connect(promise, &MethodResultPromise::gotResult, [&res] (PMethodReply reply) {
      res = reply;
    });
    QObject::connect(promise, &MethodResultPromise::gotError, [&err] (PError error) {
      err = error;
    });
    while (true) {
      if (res) {
        if (!promise.isNull())
          delete &*promise;
        return res;
      }
      if (err) {
        if (!promise.isNull())
          delete &*promise;
        throw err;
      }
      QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
    }
  }

 public:
  virtual ~ObjectHandleBase() {}

  virtual InfoPromise *getInfo(PInfoRequest req) = 0;
  virtual InfoPromise *subInfo(PInfoRequest req) = 0;
  virtual MethodResultPromise *runMethod(PMethodRequest req) = 0;
  virtual ObjectType type() const = 0;

  template<typename Request, typename... Args>
  QSharedPointer<typename Request::ReplyType> syncGetInfo(Args... args) {
    PInfoReply res = baseSyncGetInfo(QSharedPointer<Request>::create(args...));
    return res.dynamicCast<typename Request::ReplyType>();
  }

  template<typename Request, typename... Args>
  QSharedPointer<typename Request::ReplyType> syncRunMethod(Args... args) {
    PMethodReply res = baseSyncRunMethod(QSharedPointer<Request>::create(args...));
    return res.dynamicCast<typename Request::ReplyType>();
  }

  template<typename Request, typename... Args><commit_msg>Bugfix: add casts in universe to fix ubuntu build<commit_after>/*
 * Copyright 2016 CodiLime
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#ifndef VELES_DBIF_UNIVERSE
#define VELES_DBIF_UNIVERSE

#include <QObject>
#include <QCoreApplication>
#include <QPointer>

#include "dbif/types.h"
#include "dbif/method.h"
#include "dbif/info.h"
#include "dbif/promise.h"
#include "dbif/error.h"

namespace veles {
namespace dbif {

class ObjectHandleBase {
  PInfoReply baseSyncGetInfo(PInfoRequest req) {
    QPointer<InfoPromise> promise = getInfo(req);
    PInfoReply res;
    PError err;
    QObject::connect(static_cast<InfoPromise*>(promise), &InfoPromise::gotInfo, [&res] (PInfoReply reply) {
      res = reply;
    });
    QObject::connect(static_cast<InfoPromise*>(promise), &InfoPromise::gotError, [&err] (PError error) {
      err = error;
    });
    while (true) {
      if (res) {
        if (!promise.isNull())
          delete static_cast<InfoPromise*>(promise);
        return res;
      }
      if (err) {
        if (!promise.isNull())
          delete static_cast<InfoPromise*>(promise);
        throw err;
      }
      QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
    }
  }

  PMethodReply baseSyncRunMethod(PMethodRequest req) {
    QPointer<MethodResultPromise> promise = runMethod(req);
    PMethodReply res;
    PError err;
    QObject::connect(static_cast<MethodResultPromise*>(promise), &MethodResultPromise::gotResult, [&res] (PMethodReply reply) {
      res = reply;
    });
    QObject::connect(static_cast<MethodResultPromise*>(promise), &MethodResultPromise::gotError, [&err] (PError error) {
      err = error;
    });
    while (true) {
      if (res) {
        if (!promise.isNull())
          delete static_cast<MethodResultPromise*>(promise);
        return res;
      }
      if (err) {
        if (!promise.isNull())
          delete static_cast<MethodResultPromise*>(promise);
        throw err;
      }
      QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
    }
  }

 public:
  virtual ~ObjectHandleBase() {}

  virtual InfoPromise *getInfo(PInfoRequest req) = 0;
  virtual InfoPromise *subInfo(PInfoRequest req) = 0;
  virtual MethodResultPromise *runMethod(PMethodRequest req) = 0;
  virtual ObjectType type() const = 0;

  template<typename Request, typename... Args>
  QSharedPointer<typename Request::ReplyType> syncGetInfo(Args... args) {
    PInfoReply res = baseSyncGetInfo(QSharedPointer<Request>::create(args...));
    return res.dynamicCast<typename Request::ReplyType>();
  }

  template<typename Request, typename... Args>
  QSharedPointer<typename Request::ReplyType> syncRunMethod(Args... args) {
    PMethodReply res = baseSyncRunMethod(QSharedPointer<Request>::create(args...));
    return res.dynamicCast<typename Request::ReplyType>();
  }

  template<typename Request, typename... Args>
{'fraction_non_alphanumeric': 0.11854243542435425, 'fraction_numerical': 0.0036900369003690036, 'mean_word_length': 3.6697774587221823, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5488827', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1939, 'n_words': 505}