1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/********************************************************************
*
*	File:		fsShareCompletions.h
*
*	Purpose:	Share results can arrive on any thread (Java, WinRT, UIKit);
*				they are queued and handed out from update().
*
*********************************************************************/

#ifndef fsShareCompletionshdr
#define fsShareCompletionshdr

#include "../../fsShareSheetService.h"

#include <mutex>
#include <utility>
#include <vector>

class fsShareCompletions
{
public:
	void push(fsShareSheetService::DoneCallback done, fsShareResult result, std::string detail = std::string())
	{
		std::lock_guard<std::mutex> lock(mMutex);
		mPending.push_back({std::move(done), result, std::move(detail)});
	}

	void deliver()
	{
		std::vector<Pending> pending;
		{
			std::lock_guard<std::mutex> lock(mMutex);
			pending.swap(mPending);
		}
		for (auto& item : pending)
		{
			if (item.done)
			{
				item.done(item.result, item.detail);
			}
		}
	}

private:
	struct Pending
	{
		fsShareSheetService::DoneCallback done;
		fsShareResult result;
		std::string detail;
	};

	std::mutex mMutex;
	std::vector<Pending> mPending;
};

#endif