/*********************************************************\
         __      __           __  __
        / /     / / _____    / / / / ______   __     __
       / /     / / /___  \  / / / / / ___  | / /    / /
      / /_____/ / _____| / / / / / / /__/ / / /    / /
     / ______  / / ___  / / / / / / _____/ / /    / /
    / /     / / / /__/ / / / / / / |____  / |____/ /
   /_/     /_/ |______/ /_/ /_/  \_____/  \_____  /
                                         ______/ /
            High Level Game Framework   /_______/

  ------------------------------------------------------

  Copyright (c) 2009 - Rodrigo Braz Monteiro.
  This file is subject to the terms of license.txt.

\*********************************************************/

#pragma once

namespace Halley {
	class Thread {
	public:
		void Start();
		int Wait();
		bool IsDone();

		Thread(bool detached=false);
		virtual ~Thread();

		static void async(std::function<void()> function);

	protected:
		virtual int Run() = 0;

	private:
		bool running;
		bool detached;
		SDL_Thread *thread;

		static int RunThread(void *data);
	};

	class FunctionThread : public Thread {
	public:
		FunctionThread(std::function<int()> function, bool detached=false) : f(function), Thread(detached) {}

	private:
		int Run() { return f(); }

		std::function<int()> f;
	};

	class FunctionVoidThread : public Thread {
	public:
		FunctionVoidThread(std::function<void()> function, bool detached=false) : f(function), Thread(detached) {}

	private:
		int Run() { f(); return 0; }

		std::function<void()> f;
	};
}
