Fichier texte en cours : threads.txt






#ifndef THREADS_WRAPPER_H
#define THREADS_WRAPPER_H

#include "Singleton.h"
#include "Types.h"

#include
#include
#include
#include

class ThreadsWrapper : public Singleton
{
public:
         typedef uint32 ThreadSlotNumber;

         class Thread
         {
                  friend class ThreadsWrapper;
         
         public:
                  std::string sName;
                  std::thread::id lastId;
                  bool finished;

         protected:
                  std::vector pThreads;
                  void* pRetValue;
                  std::function pRunFunction;
                  uint32 iSlot;
         };

         ThreadsWrapper();
         virtual ~ThreadsWrapper();

         // --------------------- //

         // Mettre en protected
         Thread* RegisterThread(const std::function & threadFonction, uint32 iSlot)
         {
                  Thread* pThread = new Thread;

                  pThread->pRetValue = nullptr;
                  pThread->pRunFunction = threadFonction;
                  pThread->lastId = std::thread::id();
                  pThread->sName = "thread1";
                  pThread->iSlot = iSlot;
                  pThread->finished = false;

                  m_entries[iSlot] = pThread;

                  return pThread;
         }

         /*
         template
         Thread* CreateThread(void (f)(Params...), Args... args)
         {
                  std::function tF = std::bind(f, args ...);
                  std::function tRun = std::bind(&ThreadsWrapper::RunThread, this, tF);

                  debugprint("CreateThread 1")
                  // On enregistre le thread
                  Thread* t = RegisterThread(tRun, m_iCurrentRegisteredThread);

                  m_iCurrentRegisteredThread++.

                  return t;
         }
         */
         /*
         template
         Thread* CreateThread(R f, Args... args)
         {
                  std::function tF = std::bind(f, args ...);
                  std::function tRun = std::bind(&ThreadsWrapper::RunThread, this, tF);

                  debugprint("CreateThread 2")

                  // On enregistre le thread
                  Thread* t = RegisterThread(tRun, m_iCurrentRegisteredThread);

                  m_iCurrentRegisteredThread++;

                  return t;
         }
         */
         // R = void
         // R retour = ...


         template
         Thread* CreateThread(R (C::*f) (Params...), Args... args)
         {
                  // La fonction à threader
                  std::function tF = std::bind(f, args ...);
                  std::function tR =
                           std::bind(&ThreadsWrapper::RunThread, this, tF, m_iCurrentRegisteredThread);

                  // La fonction de biais entre le thread et la fonction à threader
         //         std::function tFinal = std::bind(tR);

                  //ReturnValue r;

                  debugprint("CreateThread 3")

                  // On enregistre le thread
                  Thread* t = RegisterThread(tR, m_iCurrentRegisteredThread);

                  m_iCurrentRegisteredThread++;

                  return t;
         }

         /*
         template
         Thread* CreateThread(R (*f)(Args... args1))
         {

                  // La fonction à threader
                  std::function tF = std::bind(f, args ...);
                  // La fonction de biais entre le thread et la fonction à threader
                  std::function tRun = std::bind(&ThreadsWrapper::RunTest, this, tF);         

                  ReturnValue r;

                  debugprint("CreateThread 3")

                  // On enregistre le thread
                  Thread* t = RegisterThread(tRun, m_iCurrentRegisteredThread);

                  m_iCurrentRegisteredThread++;

                  return nullptr;
         }
         */

         bool IsRegistered(const Thread* t)
         {
                  if (t == nullptr)
                  {
                           return false;
                  }

                  return m_entries.count(t->iSlot) > 0;
         }

         // todo : utiliser std::this_thread::get_id();...
         std::thread::id LaunchThread(Thread* t)
         {
                  if (IsRegistered(t))
                  {
                           // On lance le thread !
                           std::thread* pExec = new std::thread (t->pRunFunction);

                           t->pThreads.push_back(pExec);

                           std::thread::id id = pExec->get_id();

                           t->lastId = id;

                           return id;
                  }
                  else
                  {
                           // On renvoie un id par defaut !
                           return std::thread::id();
                  }
         }

         std::thread::id GetThreadId(const Thread* t)
         {
                  if (IsRegistered(t))
                  {
                           return t->lastId;
                  }
                  else
                  {
                           return std::thread::id();
                  }
         }

         template
         T GetReturnValue(const Thread* t) // pointeur obligé normalement
         {
                  if (IsRegistered(t) && t->finished)
                  {
                           return (T)t->pRetValue;
                  }
                  else
                  {
                           return nullptr;
                  }
         }

protected:

         /*
                  template
                  void RunThread(Retour&& func)
                  {
                           Retour r = func();

                           debugprint("RunThread 1")
                  }
                  */

                  template
                  void RunThread(std::function f, uint32 iSlot)
                  {
                           R r = f();

                           ReturnValue value;
                           value.value = r;

                           Thread* pThread = m_entries[iSlot];

                           if (pThread)
                           {
                                    pThread->pRetValue = r;
                                    pThread->finished = true;
                           }

                           debugprint("RunThread 2")
                  }

                  /*
                  void RunThread(std::function f)
                  {
                           f();

                           debugprint("RunThread 3")
                  }
         */
                  /*
                  template
                  Thread* GetThreadOfFunction(std::function f)
                  {
                           for (auto i = 0; i < m_entries.size(); i++)
                           {
                                    Thread* pCurrent = m_entries[i];

                                    if (pCurrent->pRunFunction == f)
                                    {
                                             return pCurrent;
                                    }
                           }

                           return nullptr;
                  }
                  */

private:
         std::map m_entries;

         /*
         template
         static std::map> m_values;
         */

         uint32 m_iCurrentRegisteredThread;
};


#endif