<commit_before>/*
**
** Author(s):
**  - <NAME> <<EMAIL>>
**
** Copyright (C) 2012, 2013 Aldebaran Robotics
** See COPYING for the license
*/

#include <futurehandler.hpp>

/**
 * @brief globalFutureHandler
 * Foreach callback set in Java, an entry is added here.
 * QiMessaging bindings search here when callback from future is called to find out which Java callback
 * needs to be called.
 */
static qi::FutureHandler globalFutureHandler;

namespace qi {

  void FutureHandler::onSuccess(JNIEnv *env, jclass cls, qi::CallbackInfo *info)
  {
    jmethodID mid = env->GetMethodID(cls, "onSuccess", "(Lcom/aldebaran/qi/Future;[Ljava/lang/Object;)V");
    if (mid == 0)
    {
      qiLogError("qimessaging.jni") << "onSuccess method of com.aldebaran.qi.Callback is not implemented";
      return;
    }

    jobject fut = FutureHandler::futurePointer(env, info);
    env->CallVoidMethod(info->instance, mid, fut, info->args);
    env->DeleteGlobalRef(fut);
  }

  void FutureHandler::onFailure(JNIEnv *env, jclass cls, qi::CallbackInfo *info)
  {
    jmethodID mid = env->GetMethodID(cls, "onFailure", "(Lcom/aldebaran/qi/Future;[Ljava/lang/Object;)V");
    if (mid == 0)
    {
      qiLogError("qimessaging.jni") << "onFailure method of com.aldebaran.qi.Callback is not implemented";
      return;
    }

    jobject fut = FutureHandler::futurePointer(env, info);
    env->CallVoidMethod(info->instance, mid, fut, info->args);
    env->DeleteGlobalRef(fut);
  }

  void FutureHandler::onComplete(JNIEnv *env, jclass cls, qi::CallbackInfo *info)
  {
    jmethodID mid = env->GetMethodID(cls, "onComplete", "(Lcom/aldebaran/qi/Future;[Ljava/lang/Object;)V");
    if (mid == 0)
    {
      qiLogError("qimessaging.jni") << "onComplete method of com.aldebaran.qi.Callback is not implemented";
      return;
    }

    jobject fut = FutureHandler::futurePointer(env, info);
    env->CallVoidMethod(info->instance, mid, fut, info->args);
    env->DeleteGlobalRef(fut);
  }

  qi::CallbackInfo* FutureHandler::methodInfo(const qi::Future<qi::AnyValue> &future)
  {
    for (std::map<qi::Future<qi::AnyValue>*, qi::CallbackInfo*>::iterator it = globalFutureHandler._map.begin();
         it != globalFutureHandler._map.end(); ++it)
    {
      qi::Future<qi::AnyValue> *iterator = (*it).first;

      if (*iterator == future)
        return (*it).second;
    }

    return 0;
  }

  void FutureHandler::addCallbackInfo(qi::Future<AnyValue> *future, qi::CallbackInfo* info)
  {
    globalFutureHandler._map[future] = info;
  }

  void FutureHandler::removeCallbackInfo(const qi::Future<qi::AnyValue>& future)
  {
    for (std::map<qi::Future<qi::AnyValue>*, qi::CallbackInfo*>::iterator it = globalFutureHandler._map.begin();
         it != globalFutureHandler._map.end(); ++it)
    {
      qi::Future<qi::AnyValue> *iterator = (*it).first;

      if (*iterator == future)
      {
        delete (*it).second;
        globalFutureHandler._map.erase(it);
        return;
      }
    }
  }

  jobject FutureHandler::futurePointer(JNIEnv *env, qi::CallbackInfo* info)
  {
    for (std::map<qi::Future<qi::AnyValue>*, qi::CallbackInfo*>::iterator it = globalFutureHandler._map.begin();
         it != globalFutureHandler._map.end(); ++it)
    {
      if ((*it).second == info)
      {
        jclass futureCls = 0;
        jmethodID init = 0;

        if ((init = env->GetMethodID(cls_future, "<init>", "(J)V")) == 0)
        {
          qiLogError("qimessaging.jni") << "Cannot find com.aldebaran.qi.Future.<init>(J) constructor";
          return 0;
        }

        // Create a new Future class.
        // Add a new global ref to object to avoid destruction before entry into Java code.
        jobject future = env->NewObject(cls_future, init, (*it).first);
#ifdef ANDROID
        return env->NewGlobalRef(future);
#else
        env->NewGlobalRef(future);
        return future;
#endif
      }
    }

    return 0;
  }

} // !qi<commit_msg>Remove unused variable<commit_after>/*
**
** Author(s):
**  - <NAME> <<EMAIL>>
**
** Copyright (C) 2012, 2013 Aldebaran Robotics
** See COPYING for the license
*/

#include <futurehandler.hpp>

/**
 * @brief globalFutureHandler
 * Foreach callback set in Java, an entry is added here.
 * QiMessaging bindings search here when callback from future is called to find out which Java callback
 * needs to be called.
 */
static qi::FutureHandler globalFutureHandler;

namespace qi {

  void FutureHandler::onSuccess(JNIEnv *env, jclass cls, qi::CallbackInfo *info)
  {
    jmethodID mid = env->GetMethodID(cls, "onSuccess", "(Lcom/aldebaran/qi/Future;[Ljava/lang/Object;)V");
    if (mid == 0)
    {
      qiLogError("qimessaging.jni") << "onSuccess method of com.aldebaran.qi.Callback is not implemented";
      return;
    }

    jobject fut = FutureHandler::futurePointer(env, info);
    env->CallVoidMethod(info->instance, mid, fut, info->args);
    env->DeleteGlobalRef(fut);
  }

  void FutureHandler::onFailure(JNIEnv *env, jclass cls, qi::CallbackInfo *info)
  {
    jmethodID mid = env->GetMethodID(cls, "onFailure", "(Lcom/aldebaran/qi/Future;[Ljava/lang/Object;)V");
    if (mid == 0)
    {
      qiLogError("qimessaging.jni") << "onFailure method of com.aldebaran.qi.Callback is not implemented";
      return;
    }

    jobject fut = FutureHandler::futurePointer(env, info);
    env->CallVoidMethod(info->instance, mid, fut, info->args);
    env->DeleteGlobalRef(fut);
  }

  void FutureHandler::onComplete(JNIEnv *env, jclass cls, qi::CallbackInfo *info)
  {
    jmethodID mid = env->GetMethodID(cls, "onComplete", "(Lcom/aldebaran/qi/Future;[Ljava/lang/Object;)V");
    if (mid == 0)
    {
      qiLogError("qimessaging.jni") << "onComplete method of com.aldebaran.qi.Callback is not implemented";
      return;
    }

    jobject fut = FutureHandler::futurePointer(env, info);
    env->CallVoidMethod(info->instance, mid, fut, info->args);
    env->DeleteGlobalRef(fut);
  }

  qi::CallbackInfo* FutureHandler::methodInfo(const qi::Future<qi::AnyValue> &future)
  {
    for (std::map<qi::Future<qi::AnyValue>*, qi::CallbackInfo*>::iterator it = globalFutureHandler._map.begin();
         it != globalFutureHandler._map.end(); ++it)
    {
      qi::Future<qi::AnyValue> *iterator = (*it).first;

      if (*iterator == future)
        return (*it).second;
    }

    return 0;
  }

  void FutureHandler::addCallbackInfo(qi::Future<AnyValue> *future, qi::CallbackInfo* info)
  {
    globalFutureHandler._map[future] = info;
  }

  void FutureHandler::removeCallbackInfo(const qi::Future<qi::AnyValue>& future)
  {
    for (std::map<qi::Future<qi::AnyValue>*, qi::CallbackInfo*>::iterator it = globalFutureHandler._map.begin();
         it != globalFutureHandler._map.end(); ++it)
    {
      qi::Future<qi::AnyValue> *iterator = (*it).first;

      if (*iterator == future)
      {
        delete (*it).second;
        globalFutureHandler._map.erase(it);
        return;
      }
    }
  }

  jobject FutureHandler::futurePointer(JNIEnv *env, qi::CallbackInfo* info)
  {
    for (std::map<qi::Future<qi::AnyValue>*, qi::CallbackInfo*>::iterator it = globalFutureHandler._map.begin();
         it != globalFutureHandler._map.end(); ++it)
    {
      if ((*it).second == info)
      {
        jmethodID init = 0;

        if ((init = env->GetMethodID(cls_future, "<init>", "(J)V")) == 0)
        {
          qiLogError("qimessaging.jni") << "Cannot find com.aldebaran.qi.Future.<init>(J) constructor";
          return 0;
        }

        // Create a new Future class.
        // Add a new global ref to object to avoid destruction before entry into Java code.
        jobject future = env->NewObject(cls_future, init, (*it).first);
#ifdef ANDROID
        return env->NewGlobalRef(future);
#else
        env->NewGlobalRef(future);
        return future;
#endif
      }
    }

    return 0;
  }

} // !qi
{'fraction_non_alphanumeric': 0.15347355003186744, 'fraction_numerical': 0.004206500956022944, 'mean_word_length': 3.3759063022866704, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1649888', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2558, 'n_words': 552}