Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Implementing template

Reply
Thread Tools

Implementing template

 
 
sam
Guest
Posts: n/a
 
      01-19-2007
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:pen(string filename)
{
if (_isOpen)
throw Exception("File:pen() - Another file is already open");
this->_filename=filename;
_file.open(filename.c_str(),ios::in);
if (!_file)
throw Exception("File:pen() - Couldn't open the file for reading");
if (readWord().compare("ANNIE_FILE"))
throw Exception("File:pen() - The file doesn't appear to be an
annie file");
if (readWord().compare(ANNIE_VERSION))
throw Exception("File: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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-19-2007
sam wrote:
> [..]
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
bjeremy
Guest
Posts: n/a
 
      01-19-2007
sam wrote:
> 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:pen(string filename)
> {
> if (_isOpen)
> throw Exception("File:pen() - Another file is already open");
> this->_filename=filename;
> _file.open(filename.c_str(),ios::in);
> if (!_file)
> throw Exception("File:pen() - Couldn't open the file for reading");
> if (readWord().compare("ANNIE_FILE"))
> throw Exception("File:pen() - The file doesn't appear to be an
> annie file");
> if (readWord().compare(ANNIE_VERSION))
> throw Exception("File: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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
implementing policy classes with template template parameters ian C++ 5 10-09-2003 03:19 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments