summaryrefslogtreecommitdiffstats
path: root/external/optick/optick_serialization.h
blob: 91de32d54e40af61c57e8b8de6412942a9713a9e (plain) (blame)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once
#include "optick_common.h"

#if USE_OPTICK
#include "optick_memory.h"

#if defined(OPTICK_MSVC)
#pragma warning( push )

//C4250. inherits 'std::basic_ostream'
#pragma warning( disable : 4250 )

//C4127. Conditional expression is constant
#pragma warning( disable : 4127 )
#endif

namespace Optick
{
	class OutputDataStream : private ostringstream 
	{
	public:
		static OutputDataStream Empty;
		// Move constructor rocks!
		// Beware of one copy here(do not use it in performance critical parts)
		string GetData();

		// It is important to make private inheritance in order to avoid collision with default operator implementation
		friend OutputDataStream &operator << ( OutputDataStream &stream, const char* val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, int val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, uint64 val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, uint32 val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, int64 val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, char val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, byte val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, int8 val);
		friend OutputDataStream &operator << ( OutputDataStream &stream, float val);
		friend OutputDataStream &operator << ( OutputDataStream &stream, const string& val );
		friend OutputDataStream &operator << ( OutputDataStream &stream, const wstring& val );

		OutputDataStream& Write(const char* buffer, size_t size);
	};

	template<class T>
	OutputDataStream& operator<<(OutputDataStream &stream, const vector<T>& val)
	{
		stream << (uint32)val.size();

		for(auto it = val.begin(); it != val.end(); ++it)
		{
			const T& element = *it;
			stream << element;
		}

		return stream;
	}

	template<class T, uint32 N>
	OutputDataStream& operator<<(OutputDataStream &stream, const MemoryPool<T, N>& val)
	{
		stream << (uint32)val.Size();

		val.ForEach([&](const T& data)
		{
			stream << data;
		});

		return stream;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	class InputDataStream : private stringstream {
	public:
		bool CanRead() { return !eof(); }

		InputDataStream();

		void Append(const char *buffer, size_t length);
		bool Skip(size_t length);
		size_t Length();

		template<class T>
		bool Peek(T& data)
		{
			if (Length() < sizeof(T))
				return false;

			pos_type currentPos = tellg();
			read((char*)&data, sizeof(T));
			seekg(currentPos);
			return true;
		}

		template<class T>
		bool Read(T& data)
		{
			if (Length() < sizeof(T))
				return false;

			read((char*)&data, sizeof(T));
			return true;
		}

		friend InputDataStream &operator >> (InputDataStream &stream, byte &val );
		friend InputDataStream &operator >> (InputDataStream &stream, int16 &val);
		friend InputDataStream &operator >> (InputDataStream &stream, uint16 &val);
		friend InputDataStream &operator >> (InputDataStream &stream, int32 &val );
		friend InputDataStream &operator >> (InputDataStream &stream, uint32 &val );
		friend InputDataStream &operator >> (InputDataStream &stream, int64 &val );
		friend InputDataStream &operator >> (InputDataStream &stream, uint64 &val );
		friend InputDataStream &operator >> (InputDataStream &stream, string &val);
	};


}

#if defined(OPTICK_MSVC)
#pragma warning( pop )
#endif

#endif //USE_OPTICK