Loading...   


class DynamicStructure {
public:
	template <typename T>
	DynamicStructure(T pStart, int pSize) : mStart(reinterpret_cast<char*>(pStart)), mPointer(mStart), mSize(pSize), mWritten(0) {}

	template <typename T>
	void write(T pData) {
		if (mWritten + sizeof(T) <= mSize) {
			auto TPtr = reinterpret_cast<T*>(mPointer);
			*TPtr = pData;
			mPointer += sizeof(T);
			mWritten += sizeof(T);
		}
	}

	void writeString(std::string pData) {
		if (mWritten + pData.length() + 1 <= mSize) {
			strcpy(mPointer, pData.c_str());
			mPointer += pData.length() + 1;
			mWritten += pData.length() + 1;
		}
	}

	// DEBUG ONLY
	template <typename T>
	void read() {
		mPointer = mStart;
		for (int i = 0; i < 4; i++) {
			T* a = reinterpret_cast<T*>(mPointer);
			std::cout << *a << std::endl;
			mPointer += sizeof(T);
		}
	}

	// DEBUG ONLY
	void readString() {
		mPointer = mStart;
		for (int i = 0; i < 4; i++) {
			std::string a = mPointer;
			std::cout << a << std::endl;
			mPointer += a.length() + 1;
		}
	}

private:
	int mSize; // maximum number of bytes to be written.
	int mWritten; 
	char* mStart;
	char* mPointer;
};


void intTest() {
	void* mem = malloc(sizeof(int)* 4);
	memset(mem, 0, sizeof(int)* 4);
	DynamicStructure structure(mem, sizeof(int)* 4);
	structure.write<int>(1814382);
	structure.write<int>(9);
	structure.write<int>(-23443);
	structure.write<int>(470);

	structure.read<int>();
	free(mem);
}

void floatTest() {
	void* mem = malloc(sizeof(float)* 4);
	memset(mem, 0, sizeof(float)* 4);
	DynamicStructure structure(mem, sizeof(float)* 4);
	structure.write<float>(12.0f);
	structure.write<float>(-32345.5);
	structure.write<float>(2.0f);
	structure.write<float>(2933.987);

	structure.read<float>();
	free(mem);
}

void stringTest() {
	std::string a1 = "Hello There!";
	std::string a2 = "Where are the chips?";
	std::string a3 = "";
	std::string a4 = "a";
	std::string a5 = "kladsjkladsjk9013290132ijoads";
	int strSize = std::string(a1 + a2 + a3 + a4 + a5).length() + 5;
	void* mem = malloc(strSize);
	DynamicStructure structure(static_cast<char*>(mem), strSize);
	structure.writeString("Hello There!");
	structure.writeString("Where are the chips?");
	structure.writeString("");
	structure.writeString("kladsjkladsjk9013290132ijoads");

	structure.readString();
	free(mem);
}

void podTest() {
	struct myPOD {
		int mLevel;
		char mName[8];
		float mSize;
	};

	myPOD* myPODs = new myPOD[3];

	sprintf(myPODs[0].mName, "Robert");
	myPODs[0].mLevel = 6;
	myPODs[0].mSize = 2.5;

	sprintf(myPODs[1].mName, "AkkadiusSeven");
	myPODs[1].mLevel = 1;
	myPODs[1].mSize = -4.3;

	sprintf(myPODs[2].mName, "Coffee");
	myPODs[2].mLevel = 99;
	myPODs[2].mSize = 52.92;

	std::size_t memSize = sizeof(myPOD)* 3;
	void* mem = malloc(memSize);
	memset(mem, 0, memSize);
	DynamicStructure structure(mem, memSize);

	structure.write(myPODs[0]);
	structure.write(myPODs[1]);
	structure.write(myPODs[2]);

	myPOD* newPODs = reinterpret_cast<myPOD*>(mem);
	for (int i = 0; i < 3; i++) {
		std::stringstream ss;
		ss << "Name: " << newPODs[i].mName << " Level: " << newPODs[i].mLevel << " Size: " << newPODs[i].mSize;
		std::cout << ss.str() << std::endl;
	}

	free(mem);
}

int _tmain(int argc, _TCHAR* argv[]) {

	intTest();
	floatTest();
	stringTest();
	podTest();

	return 0;
}

Raw Paste Data