| 1 |
#ifndef PROTON_MSG_H |
| 2 |
#define PROTON_MSG_H |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <sstream> |
| 26 |
#include <iostream> |
| 27 |
|
| 28 |
namespace proton { |
| 29 |
|
| 30 |
/** A simple facade for std::ostringstream that allows |
| 31 |
* in place construction of a message and automatic conversion |
| 32 |
* to string. |
| 33 |
* E.g. |
| 34 |
*@code |
| 35 |
* void foo(const std::string&); |
| 36 |
* foo(msg() << "hello " << 32); |
| 37 |
*@endcode |
| 38 |
* Will construct the string "hello 32" and pass it to foo() |
| 39 |
*/ |
| 40 |
struct msg { |
| 41 |
std::ostringstream os; |
| 42 |
msg() {} |
| 43 |
msg(const msg& m) : os(m.str()) {} |
| 44 |
std::string str() const { return os.str(); } |
| 45 |
operator std::string() const { return str(); } |
| 46 |
template <class T> msg& operator<<(const T& t) { os << t; return *this; } |
| 47 |
}; |
| 48 |
|
| 49 |
inline std::ostream& operator<<(std::ostream& o, const msg& m) { return o << m.str(); } |
| 50 |
|
| 51 |
/** Construct a message using operator << and append (file:line) */ |
| 52 |
#define QUOTe_(x) #x |
| 53 |
#define QUOTE(x) QUOTe_(x) |
| 54 |
#define MSG(message) (::proton::msg() << message) |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
#endif /*!PROTON_MSG_H*/ |