Textractor_test/GUI/host/textthread.h

42 lines
1.1 KiB
C
Raw Normal View History

#pragma once
2018-08-23 00:24:55 +08:00
#include "common.h"
2018-08-25 00:50:20 +08:00
#include "types.h"
2018-07-19 11:40:44 +08:00
class TextThread
{
public:
using EventCallback = std::function<void(TextThread*)>;
using OutputCallback = std::function<bool(TextThread*, std::wstring&)>;
inline static EventCallback OnCreate, OnDestroy;
2018-10-08 12:26:43 +08:00
inline static OutputCallback Output;
2018-11-09 17:24:33 +08:00
inline static int flushDelay = 400; // flush every 400ms by default
inline static int maxBufferSize = 1000;
2018-10-08 12:26:43 +08:00
TextThread(ThreadParam tp, HookParam hp, std::optional<std::wstring> name = {});
2018-08-25 00:50:20 +08:00
~TextThread();
void AddSentence(const std::wstring& sentence);
2018-11-04 09:41:38 +08:00
void Push(const BYTE* data, int len);
2018-12-22 03:11:40 +08:00
ThreadSafe<std::wstring> storage;
const int64_t handle;
2018-09-21 10:32:47 +08:00
const std::wstring name;
const ThreadParam tp;
2018-10-31 08:50:50 +08:00
const HookParam hp;
2018-09-21 10:32:47 +08:00
private:
2019-01-05 16:47:32 +08:00
inline static int threadCounter = 0;
2018-12-14 11:44:55 +08:00
void Flush();
2018-08-22 10:43:30 +08:00
std::wstring buffer;
std::unordered_set<wchar_t> repeatingChars;
std::mutex bufferMutex;
DWORD lastPushTime;
ThreadSafe<std::vector<std::wstring>> queuedSentences;
2019-01-10 11:35:01 +08:00
struct TimerDeleter { void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } };
2018-12-04 07:29:30 +08:00
AutoHandle<TimerDeleter> timer = NULL; // this needs to be last so it's destructed first
};