Implementing template

S

sam

How to implement a template in following source code:-

#include "../include/File.h"
#include "../include/Exception.h"
#include "../include/defines.h"
#include <cstdlib>

using namespace std;
namespace annie
{
File::File()
{ _isOpen=false; }

File::File(string filename)
{ _isOpen=false; open(filename); }

void
File::eek:pen(string filename)
{
if (_isOpen)
throw Exception("File::eek:pen() - Another file is already open");
this->_filename=filename;
_file.open(filename.c_str(),ios::in);
if (!_file)
throw Exception("File::eek:pen() - Couldn't open the file for reading");
if (readWord().compare("ANNIE_FILE"))
throw Exception("File::eek:pen() - The file doesn't appear to be an
annie file");
if (readWord().compare(ANNIE_VERSION))
throw Exception("File::eek:pen() - The file is annie's file, but not the
right version");
}

void
File::_next()
{
char temp;
ws(_file);
while (_file.peek()=='#')
{
do
{
_file.get(temp);
}
while (temp!='\n' && !_file.eof());
ws(_file);
}
}

char
File::readChar()
{
char c='\0';
_next();
if (!_file.eof())
_file>>c;
return c;
}

int
File::readInt()
{
int i=0;
_next();
if (!_file.eof())
_file>>i;
ws(_file);
return i;
}

real
File::readDouble()
{
real d=0;
_next();
if (!_file.eof())
_file>>d;
ws(_file);
return d;
}

string
File::readWord()
{
string s("");
_next();
if (!_file.eof())
_file>>s;
ws(_file);
return s;
}

string
File::readLine()
{
string s;
_next();
getline(_file,s,'\n');
int commentPos=s.find_first_of('#',0);
return s.substr(0,commentPos);
}

void
File::close()
{
if (_isOpen)
{
_file.close();
_isOpen=false;
}
}

bool
File::eof()
{ return _file.eof() || (_file.peek()==-1);}

}; //namespace annie

I want to implement a template in readchar() and readdouble() and
readint() function.
I reading this sourcecode of network library and i surprised why he
didn't use the template here? Why he did that? as I can't directly
contact the programmer.
 
V

Victor Bazarov

sam said:
[..]
char
File::readChar()
{
char c='\0';
_next();
if (!_file.eof())
_file>>c;
return c;
}

int
File::readInt()
{
int i=0;
_next();
if (!_file.eof())
_file>>i;
ws(_file);
return i;
}

real
File::readDouble()
{
real d=0;
_next();
if (!_file.eof())
_file>>d;
ws(_file);
return d;
}
[..]

I want to implement a template in readchar() and readdouble() and
readint() function.

What's stopping you? Begin with the interface:

template<class T> T readItem();

then use one of the functions as the prototype, replace the type you
have with 'T' and see if it fits all types you expect it to be used
with. Make adjustments as needed. Consult FAQ section 35.
I reading this sourcecode of network library and i surprised why he
didn't use the template here? Why he did that? as I can't directly
contact the programmer.

Possibly because he didn't know much about templates and their
advantages... Hard to say for sure. You *do* need to contact the
programmer to know the true reason.

V
 
B

bjeremy

sam said:
How to implement a template in following source code:-

#include "../include/File.h"
#include "../include/Exception.h"
#include "../include/defines.h"
#include <cstdlib>

using namespace std;
namespace annie
{
File::File()
{ _isOpen=false; }

File::File(string filename)
{ _isOpen=false; open(filename); }

void
File::eek:pen(string filename)
{
if (_isOpen)
throw Exception("File::eek:pen() - Another file is already open");
this->_filename=filename;
_file.open(filename.c_str(),ios::in);
if (!_file)
throw Exception("File::eek:pen() - Couldn't open the file for reading");
if (readWord().compare("ANNIE_FILE"))
throw Exception("File::eek:pen() - The file doesn't appear to be an
annie file");
if (readWord().compare(ANNIE_VERSION))
throw Exception("File::eek:pen() - The file is annie's file, but not the
right version");
}

void
File::_next()
{
char temp;
ws(_file);
while (_file.peek()=='#')
{
do
{
_file.get(temp);
}
while (temp!='\n' && !_file.eof());
ws(_file);
}
}

char
File::readChar()
{
char c='\0';
_next();
if (!_file.eof())
_file>>c;
return c;
}

int
File::readInt()
{
int i=0;
_next();
if (!_file.eof())
_file>>i;
ws(_file);
return i;
}

real
File::readDouble()
{
real d=0;
_next();
if (!_file.eof())
_file>>d;
ws(_file);
return d;
}

string
File::readWord()
{
string s("");
_next();
if (!_file.eof())
_file>>s;
ws(_file);
return s;
}

string
File::readLine()
{
string s;
_next();
getline(_file,s,'\n');
int commentPos=s.find_first_of('#',0);
return s.substr(0,commentPos);
}

void
File::close()
{
if (_isOpen)
{
_file.close();
_isOpen=false;
}
}

bool
File::eof()
{ return _file.eof() || (_file.peek()==-1);}

}; //namespace annie

I want to implement a template in readchar() and readdouble() and
readint() function.
I reading this sourcecode of network library and i surprised why he
didn't use the template here? Why he did that? as I can't directly
contact the programmer.

I'm sure it was just a design decision. Not sure I disagree with him.
He is creating a File object. Unless the file is one specifically of
Ints or Chars or whatever, I don't see a need to specifically declare
the File as such... For example if a file can contain chars and ints,
would you declare File<char> ? Also, by naming methods readInt or
readChar, readWord we know through the api exactly what we are doing,
we are reading a word or an int or whatever. And lastly, since he was
dealing with ints, chars and words (well and doubles), if you used a
template, all those methods would require thier own specializations
anyway... He would end up writing three separate funtions either way...
As for doubles, he implemented that the same as an int... really if he
wanted them to be the same he could have just implemented the double in
terms of the ints implementation, but I still don't find it
"surprising" in any way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top