From ca32c59a58d37c1822e185a2d5f3d0d3e8943593 Mon Sep 17 00:00:00 2001 From: Marijn Tamis Date: Thu, 3 May 2018 18:22:48 +0200 Subject: NvCloth 1.1.4 Release. (24070740) --- NvCloth/samples/SampleBase/utils/DataStream.h | 72 +++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 NvCloth/samples/SampleBase/utils/DataStream.h (limited to 'NvCloth/samples/SampleBase/utils/DataStream.h') diff --git a/NvCloth/samples/SampleBase/utils/DataStream.h b/NvCloth/samples/SampleBase/utils/DataStream.h new file mode 100644 index 0000000..c47da39 --- /dev/null +++ b/NvCloth/samples/SampleBase/utils/DataStream.h @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include +#include +#include + +class DataStream +{ +public: + DataStream(); + DataStream(void* data, int sizeInBytes); + DataStream(const char* filename); + static bool fileExists(const char* filename); + + void saveToFile(const char* filename); + + template + void write(T& value) + { + if(mBuffer.size() < mWritePos + sizeof(T)) + mBuffer.resize(mWritePos + sizeof(T)); + memcpy(&mBuffer[mWritePos], &value, sizeof(T)); + mWritePos += sizeof(T); + } + template<> + void write(std::string& value) + { + int len = (int)value.length(); + if(mBuffer.size() < mWritePos + len + sizeof(int)) + mBuffer.resize(mWritePos + len + sizeof(int)); + memcpy(&mBuffer[mWritePos], &len, sizeof(int)); + mWritePos += sizeof(int); + memcpy(&mBuffer[mWritePos], value.c_str(), len); + mWritePos += len; + } + + void write(void* data, int sizeInBytes); + + template + T read() + { + T value; + assert(mReadPos + sizeof(T) <= mBuffer.size()); + memcpy(&value, &mBuffer[mReadPos], sizeof(T)); + mReadPos += sizeof(T); + return value; + } + template<> + std::string read() + { + int len = read(); + std::string value; + value.resize(len); + assert(mReadPos + len < (int)mBuffer.size()); + memcpy(&value[0], &mBuffer[mReadPos], len); + mReadPos += len; + return value; + } +private: + std::vector mBuffer; + int mReadPos; + int mWritePos; +}; \ No newline at end of file -- cgit v1.2.3