473,379 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Implementing template

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

Jan 19 '07 #1
2 1579
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 TT 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
Jan 19 '07 #2
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::open(string filename)
{
if (_isOpen)
throw Exception("File::open() - Another file is already open");
this->_filename=filename;
_file.open(filename.c_str(),ios::in);
if (!_file)
throw Exception("File::open() - Couldn't open the file for reading");
if (readWord().compare("ANNIE_FILE"))
throw Exception("File::open() - The file doesn't appear to be an
annie file");
if (readWord().compare(ANNIE_VERSION))
throw Exception("File::open() - 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.

Jan 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ian | last post by:
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++ Design by Andrei Alexandrescu. The test code is listed below and the compiler error message that is generated is: ...
3
by: RA Scheltema | last post by:
hi all, Basically I have the following situation in my header-file: --- .... template <typename ta_type> struct A
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
27
by: Pete | last post by:
I'm doing exercise 8-2 from "Accelerated C++" where we're supposed to implement library algorithms, trying to minimize the number of iterator operations. I have two questions so far. First,...
32
by: Abhishek Srivastava | last post by:
Hi, Somebody recently asked me to implement the sizeof operator, i.e. to write a function that accepts a parameter of any type, and without using the sizeof operator, should be able to return...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
1
by: steve | last post by:
Hello ! I have a special need implementing a lexical parser. I have a C restricted grammar, in which I accept such operations as (3<3.5) or (s<"string"). My problem is that I meet some...
2
by: soy.hohe | last post by:
Hi all I have a class StreamLogger which implements operator << in this way: template <class TStreamLogger& operator<<(const T& t) { <print the stuff using fstream, etc.> return *this; }
1
by: aditi kauts | last post by:
Hi I am implementing security on Contact base smart card which follows scosta template. In card structure ef is directly under DF and DF is directly under MF. According to the template i am...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.