#ifndef LINESCANNER_H_ #define LINESCANNER_H_ // LineScanner is a simple one-type scanner class initialized // from a single string. When that single string of input is // consumed, it will throw a runtime_error. It scans only the // provided type of values which must be able to use the >> // extraction operator // Beth Katz - September 2007 // Constructor // LineScanner(const std::string & source) // // Mutator method // T nextValue( ) // returns the next value of type T if it exists // throws runtime_error if there is any problem // // Usage: // LineScanner intScanner("5 -42 17 9"); // string s = "Millersville University"; // LineScanner charScan(s); // would return individual characters skipping the space // LineScanner strScan(s); // would return each of the two words one at a time #include #include #include template class LineScanner { public: explicit LineScanner(const std::string & source) : theStream(source) { } virtual ~LineScanner( ) { } virtual T nextValue( ) { T value; if (theStream >> value) { return value; } else { throw std::runtime_error("No more input."); } } private: std::stringstream theStream; }; #endif /*LINESCANNER_H_*/