Skip to main content
gamedev.net
🔒 Locked

Failed template buffer!!!

Started by programering Sep 8, 2003 at 2:58 AM 5 replies 400+ views
Original Post
programering
programering
I''ve got an idea of creating a template buffer instead of making two different buffers of byte and char (ByteBuffer, CharBuffer). Here''s the code:

#include <minmax.h>

template<typename T0, T1>
size_t GetSizeDiff(T0 t0, T1 t1)
{
	size_t size0 = sizeof(t0);
	size_t size1 = sizeof(t1);
	size_t biggest = max(size0, size1);
	size_t smallest = min(size0, size1);
	size_t size_diff = biggest / smallest;
	return size_diff;
}
 
  
template<typename Tmp>
class Buffer
{
public:
 
	Tmp *buffer;
	long length;
	Tmp *ptr;
 
	Buffer(long size)
	{
		buffer = (Tmp *) malloc(sizeof(Tmp) * size);
		length = size;
		ptr = buffer;
	}
 
	template<typename T>
	Buffer(Buffer<T> *buf)
	{
		this->length = buf->GetSize() / GetSizeDiff(Tmp, T);
		this->buffer = (Tmp *) buf->GetBuf();
		this->ptr = this->buffer;
	}
 
	Tmp *GetBuf(void)
	{
		return buffer;
	}
 
	long *GetSize(void)
	{
		return &length
	}
 
	Tmp *GetPtr(size_t offset)
	{
		return ptr += offset;
	}
 
	void RstPtr(void)
	{
		ptr = buffer;
	}
 
	template<typename T>
	Buffer<T> *operator=(void)
	{
		return new Buffer<T>(this);
	}
 
	~Buffer(void)
	{
		free(buffer);
	}
};
 
 
template<typename T>
Buffer::Buffer(Buffer<T> *buf)
{
	size_t this_size = sizeof(Tmp);
	size_t that_size = sizeof(T);
	size_t biggest = max(this_size, that_size);
	size_t smallest = min(this_size, that_size);
	size_t size_diff = biggest / smallest;
	this->length = buf->GetSize() / size_diff;
	this->buffer = (Tmp *) buf->GetBuf();
	this->ptr = this->buffer;
}
 
But when I compile it I got these errors: Compiling... SDL_TEST.cpp D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(22) : error C2061: syntax error : identifier ''T1'' D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(23) : error C2061: syntax error : identifier ''T1'' D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(80) : error C2805: binary ''operator ='' has too few parameters D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(89) : see reference to class template instantiation ''Buffer'' being compiled D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(95) : error C2955: ''Buffer'' : use of class template requires template argument list D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(89) : see declaration of ''Buffer'' D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(105) : error C2995: ''Buffer::Buffer'' : template function has already been defined D:\SDL\SDL_TEST\SDL_TEST\SDL_TEST.cpp(52) : see declaration of ''Buffer::Buffer'' Error executing cl.exe. SDL_TEST.obj - 5 error(s), 0 warning(s) Can someone clear this up, tell me how I shall write the templates. ------------------------------- Anton Karlsson Klingis Entertainment Games with silly humor Just dreaming wont make you (more) skilled in game programming/development.
Qw3r7yU10p!
Qw3r7yU10p!
you need typename in front of both the types

[edited by - petewood on September 8, 2003 4:02:22 AM]
programering
programering
you mean?:

template<typename T0, typename T1>


[edited by - programering on September 8, 2003 4:12:06 AM]
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by programering
you mean?:


yes

also just to point out you''re dividing two integers which might not give the results you really want.
programering
programering
What do you mean, why shouldn''t that work?

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Packa bajs med winzip!

Just dreaming wont make you (more) skilled in game programming/development.
Qw3r7yU10p!
Qw3r7yU10p!
e.g.

5/1 == 5
5/2 == 2
5/3 == 1
5/4 == 1
5/5 == 1


[edited by - petewood on September 8, 2003 6:01:51 AM]
programering
programering
The size function:

size_t GetSizeDiff(size_t size0, size_t size1)
{
size_t biggest = max(size0, size1);
size_t smallest = min(size0, size1);
size_t size_diff = biggest / smallest;
return size_diff;
}


The ByteBuffer class:

class ByteBuffer {
public:

byte *buffer;
long length;
byte *ptr;

ByteBuffer(long size)
{
buffer = (byte *) malloc(sizeof(byte) * size);
length = size;
ptr = buffer;
}

Buffer(filebuf *buf)
{
istream is = new istream(buf);
is->seek_g(0, 0, ios::end); // or whatever the syntax is

long size = is->tell_g();
this(size);
is->read(0, buffer, size);
delete is;
}

byte *GetBuf(void)
{
return buffer;
}

long *GetSize(void)
{
return &length
}

byte *GetPtr(size_t offset)
{
return ptr += offset;
}

void RstPtr(void)
{
ptr = buffer;
}

CharBuffer *operator=(void)
{
return new CharBuffer(this);
}

CharBuffer *operator=(ByteBuffer *buf)
{
return new CharBuffer(buf);
}

~Buffer(void)
{
free(buffer);
}
};


And the CharBuffer class:

class CharBuffer {
public:

char *buffer;
long length;
char *ptr;

ByteBuffer(long size)
{
buffer = (char *) malloc(sizeof(char) * size);
length = size;
ptr = buffer;
}

Buffer(ByteBuffer *buf)
{
length = buf->GetSize();
buffer = (char *) buf->GetBuf();
ptr = buffer;
}

char *GetBuf(void)
{
return buffer;
}

long *GetSize(void)
{
return &length
}

char *GetPtr(size_t offset)
{
return ptr += offset;
}

void RstPtr(void)
{
ptr = buffer;
}

~Buffer(void)
{
free(buffer);
}
};


Since a byte is a unsigned char, byte and char must have same sizes in bits.

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Packa bajs med winzip!

Just dreaming wont make you (more) skilled in game programming/development.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.