views:

2147

answers:

4

Hi, I'd like to implement a custom manipulator for ostream to do some manipulation on the next item being inserted into the stream. For example, let's say I have a custom manipulator quote:

std::ostringstream os;
std::string name("Joe");
os << "SELECT * FROM customers WHERE name = " << quote << name;

The manipulator quote will quote name to produce:

SELECT * FROM customers WHERE name = 'Joe'

How do I go about accomplishing that? Thanks.

+6  A: 

[EDIT: "True manipulator semantics" (i.e. a persistent quoting state) could also be achieved by wrapping an std::ostream rather than deriving from it, as noted by Benôit in the comments.]

To the best of my knowledge this cannot be done directly without either deriving a new class from std::ostream or similar, or wrapping such a class in another class that forwards most methods to its contained std::ostream object. That's because, for the code example you provide to work, you will need to somehow modify the behaviour of std::ostream& operator<<(std::ostream&, std::string const&), which is defined somewhere in the iostreams hierarchy (or possibly wherever std::string is defined). You will also need to use the (somewhat ugly) facilities in ios_base to record a boolean flag holding the current quoting state. Look up ios_base::xalloc(), ios_base::iword() and ios_base::pword() to find out how to do that.

However, if you are willing to use the following syntax:

os << "SELECT * FROM customers WHERE name = " << quote(name);

This can be done very simply using a global function (in an appropriate namespace of course).

This syntax has the advantage that quoting is not persistent, meaning it can't "leak out" when a function sets the quote formatting flag and forgets to set it back to its original value.

j_random_hacker
I can't reconcile this answer with the other two answers that seem to suggest that it can be done. Which is correct?
1800 INFORMATION
It can be done as said in the other answers. The only mistake in the current answer is that it can be done "without deriving". But clearly there is a need for a new class and an overloaded templated operator<<.
Benoît
@1800: litb's and Martin York's clever solutions have different semantics than all existing iostreams manipulators -- they will quote only until the end of the current statement. Please see my comments on their answers.
j_random_hacker
Johannes Schaub - litb
Johannes Schaub - litb
+6  A: 

Try this:

#include <iostream>
#include <iomanip>

// The Object that we put on the stream.
// Pass in the character we want to 'quote' the next object with.
class Quote
{
    public:
        Quote(char x)
            :m_q(x)
        {}
    private:
        // Classes that actual does the work.
        class Quoter
        {
            public:
                Quoter(Quote const& quote,std::ostream& output)
                    :m_q(quote.m_q)
                    ,m_s(output)
                {}

                // The << operator for all types. Outputs the next object
                // to the stored stream then returns the stream. 
                template<typename T>
                std::ostream& operator<<(T const& quoted)
                {
                    return m_s << m_q << quoted << m_q;
                }

            private:
                char            m_q;
                std::ostream&   m_s;
        };
        friend Quote::Quoter operator<<(std::ostream& str,Quote const& quote);

    private:
        char    m_q;
};

// When you pass an object of type Quote to an ostream it returns
// an object of Quote::Quoter that has overloaded the << operator for
// all types. This will quote the next object and the return the stream
// to continue processing as normal.
Quote::Quoter operator<<(std::ostream& str,Quote const& quote)
{
    return Quote::Quoter(quote,str);
}


int main()
{
    std::cout << Quote('"') << "plop" << std::endl;
}
Martin York
Another clever answer, however you should mention that your Quote manipulator has unusual semantics -- once again it quotes "until the end of the statement". Which is fine, but not consistent with the behaviour of any other iostreams manipulator.
j_random_hacker
@ j_random_hacker: No it quotes the next object that looks like it is placed on the stream then returns the stream for subsequent objects. So the symantics are as you would expect.
Martin York
@Martin: Whoops, you're right about only quoting the next item. But it's still the case that behaviour differs from other next-item-only manipulators (e.g. setw): 'cout << Quote('"'); cout << "plop";' does not quote "plop". Not saying that that's bad (I actually think it's safer), just different.
j_random_hacker
+9  A: 

It's particularly difficult to add a manipulator to a C++ stream, as one has no control of how the manipulator is used. One can imbue a new locale into a stream, which has a facet installed that controls how numbers are printed - but not how strings are output. And then the problem would still be how to store the quoting state safely into the stream.

Strings are output using an operator defined in the std namespace. If you want to change the way those are printed, yet keeping the look of manipulators, you can create a proxy class:

namespace quoting {
struct quoting_proxy {
    explicit quoting_proxy(std::ostream & os):os(os){}

    template<typename Rhs>
    friend std::ostream & operator<<(quoting_proxy const& q, 
                                     Rhs const& rhs) {
        return q.os << rhs;
    }

    friend std::ostream & operator<<(quoting_proxy const& q, 
                                     std::string const& rhs) {
        return q.os << "'" << rhs << "'";
    }

    friend std::ostream & operator<<(quoting_proxy const& q, 
                                     char const* rhs) {
        return q.os << "'" << rhs << "'";
    }
private:
    std::ostream & os;
};

struct quoting_creator { } quote;
quoting_proxy operator<<(std::ostream & os, quoting_creator) {
    return quoting_proxy(os);
}
}

int main() {
    std::cout << quoting::quote << "hello" << std::endl; 
}

Which would be suitable to be used for ostream. If you want to generalize, you can make it a template too and also accept basic_stream instead of plain string. It has different behaviors to standard manipulators in some cases. Because it works by returning the proxy object, it will not work for cases like

std::cout << quoting::quote; 
std::cout << "hello";
Johannes Schaub - litb
Clever answer litb, however you should mention that your quoting::quote has different semantics than all other manipulators -- notably, 'cout << quote << "X";' quotes but 'cout << quote; cout << "X";' does not. You could even call this a feature, however it is inconsistent with other manipulators.
j_random_hacker
j_random_hacker. hmm i see. well i thought about dropping the "feel" in "look and feel" :). now you gave me a good argument for doing that. thanks :)
Johannes Schaub - litb
Much clearer now. Good point about locales for changing numeric output BTW -- that's a useful "access route" I'd forgotten about. :)
j_random_hacker
I made the same mistake here as I did with Martin's answer -- your quoting::quote actually quotes just the next item in the statement rather than all items until the end of the statement. (Which is in many ways safer than maintaining persistent format state I think.)
j_random_hacker
+1  A: 

Or just use OTL which basically already implements a stream interface for SQL very similarly to your example.

Assaf Lavie