Since you people seem rather interested, I have attached the file that I uploaded to the then-existing Boost Vault as the proof of concept for my interest check to Boost concerning such an input manipulator. I made this interest check before I approached brewbuck, with the code inspired by an input manipulator implementation in some version of g++'s standard library implementation.
A member of the Boost mailing list pointed out that changing basic_istream itself to overload operator>> could be better than a manipulator if this were ever to be added to the C++ standard: Interest check: constant input matching manipulator.
Note that this is just code to start a discussion: it is not guaranteed to be robust, complete, efficient or even correct; in addition to the disclaimers in the Boost license, I stress that I accept no liability for any damages resulting from the use of the code. You have been warned
EDIT:
Originally Posted by
Elysia
In my opinion, it is always better to do something than nothing at all.
We have three choices: ignore it, assert it or throw an exception. Which one is best? I don't know. Therefore, I put an assert there to remind me that if I did something wrong, I would become reminded.
The mistake is that you made a function call in your assertion. If the assertion is disabled, rhs.GetChar() is not called, which looks wrong, don't you agree?
EDIT #2: Oh, but GetChar() is a const member function with no side effects, so I guess that's okay here.
EDIT #3: Come to think of it, the header file is less than 100 lines long, so I might as well post it here for those who would rather not download it:
Code:
#ifndef BOOST_IOMANIP_HPP_
#define BOOST_IOMANIP_HPP_
// Copyright Eugene Wee 2009.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/* boost::iomanip::literal is an input stream manipulator that matches input
* with a specified string or character literal. If the input read matches the
* literal, it is ignored, otherwise the stream is placed in a failed state.
* Whitespace is not skipped, hence the std::ws manipulator should be used to
* skip preceding whitespace, if necessary.
*/
#include <string>
#include <memory>
#include <istream>
#include <ios>
namespace boost
{
namespace iomanip
{
namespace detail
{
// Implementation defined type to implement the manipulator.
template<typename charT, typename Traits = std::char_traits<charT>,
typename Allocator = std::allocator<charT> >
struct token_matcher
{
typedef std::basic_string<charT, Traits, Allocator> token_type;
token_matcher(const token_type& token) : token(token) {}
token_type token;
};
// Extracts and matches input character-wise from the stream.
template<typename charT, typename Traits, typename Allocator>
std::basic_istream<charT, Traits>&
operator>>(std::basic_istream<charT, Traits>& in,
token_matcher<charT, Traits, Allocator> matcher)
{
typename std::basic_string<charT, Traits, Allocator>::iterator
i = matcher.token.begin(),
end = matcher.token.end();
typename std::basic_istream<charT, Traits>::char_type c;
for (; i != end && in.get(c); ++i)
{
if (*i != c)
{
in.clear(std::basic_ios<charT, Traits>::failbit);
break;
}
}
return in;
}
} // namespace detail
// Match with literals as standard string objects.
template<typename charT, typename Traits, typename Allocator>
inline
detail::token_matcher<charT, Traits, Allocator>
literal(const std::basic_string<charT, Traits, Allocator>& token)
{
return detail::token_matcher<charT, Traits, Allocator>(token);
}
// Match with literals as null terminated strings.
template<typename charT>
inline detail::token_matcher<charT> literal(const charT* token)
{
return detail::token_matcher<charT>(std::basic_string<charT>(token));
}
// Match with literals as individual characters.
template<typename charT>
inline detail::token_matcher<charT> literal(charT token)
{
return detail::token_matcher<charT>(std::basic_string<charT>(1, token));
}
} // namespace iomanip
} // namespace boost
#endif