Skip to content

Commit e5f46f3

Browse files
committed
Create Message
Message is a wrapper/utility class that allows to easily create and store a string based on a set of given arguments. Re ECFLOW-1931
1 parent e3ee585 commit e5f46f3

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

ACore/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(srcs
3939
src/ecflow/core/Indentor.hpp
4040
src/ecflow/core/Log.hpp
4141
src/ecflow/core/LogVerification.hpp
42+
src/ecflow/core/Message.hpp
4243
src/ecflow/core/NOrder.hpp
4344
src/ecflow/core/NState.hpp
4445
src/ecflow/core/NodePath.hpp
@@ -149,6 +150,7 @@ set(test_srcs
149150
test/TestFile.cpp
150151
test/TestGetUserDetails.cpp
151152
test/TestLog.cpp
153+
test/TestMessage.cpp
152154
test/TestMigration.cpp
153155
test/TestNodePath.cpp
154156
test/TestPasswdFile.cpp

ACore/src/ecflow/core/Message.hpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2009- ECMWF.
3+
*
4+
* This software is licensed under the terms of the Apache Licence version 2.0
5+
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
* In applying this licence, ECMWF does not waive the privileges and immunities
7+
* granted to it by virtue of its status as an intergovernmental organisation
8+
* nor does it submit to any jurisdiction.
9+
*/
10+
11+
#ifndef ecflow_core_Message_HPP
12+
#define ecflow_core_Message_HPP
13+
14+
#include <sstream>
15+
#include <string>
16+
17+
namespace ecf {
18+
19+
///
20+
/// \brief Message is a utility class that easily creates a string-based Message from an arbitrary set of inputs
21+
///
22+
/// The goal is to facilitate the construction/formatting of messages for error handling and logging.
23+
/// A Message implicitly allows conversion to std::string, and provides the streaming operator<<.
24+
///
25+
class Message {
26+
public:
27+
template <typename... ARGS>
28+
explicit Message(ARGS&&... args) {
29+
((buffer << std::forward<ARGS>(args)), ...);
30+
}
31+
32+
[[nodiscard]] std::string str() const { return buffer.str(); }
33+
[[nodiscard]] operator std::string() const { return buffer.str(); }
34+
35+
private:
36+
std::ostringstream buffer;
37+
};
38+
39+
inline std::ostream& operator<<(std::ostream& o, const Message& m) {
40+
o << std::string(m);
41+
return o;
42+
}
43+
44+
} // namespace ecf
45+
46+
#endif /* ecflow_core_Message_HPP */

ACore/test/TestMessage.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2009- ECMWF.
3+
*
4+
* This software is licensed under the terms of the Apache Licence version 2.0
5+
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6+
* In applying this licence, ECMWF does not waive the privileges and immunities
7+
* granted to it by virtue of its status as an intergovernmental organisation
8+
* nor does it submit to any jurisdiction.
9+
*/
10+
11+
#include <iostream>
12+
13+
#include <boost/test/unit_test.hpp>
14+
15+
#include "ecflow/core/Message.hpp"
16+
17+
BOOST_AUTO_TEST_SUITE(CoreTestSuite)
18+
19+
BOOST_AUTO_TEST_SUITE(TestMessage)
20+
21+
BOOST_AUTO_TEST_CASE(test_message_can_create_from_various_arguments) {
22+
using namespace ecf;
23+
using namespace std::string_literals;
24+
25+
BOOST_CHECK_EQUAL(Message().str(), ""s);
26+
BOOST_CHECK_EQUAL(Message("a", ' ', "1").str(), "a 1"s);
27+
BOOST_CHECK_EQUAL(Message("a", ' ', "1.01").str(), "a 1.01"s);
28+
BOOST_CHECK_EQUAL(Message("a", ' ', 1).str(), "a 1"s);
29+
BOOST_CHECK_EQUAL(Message("a", ' ', 1.01).str(), "a 1.01"s);
30+
BOOST_CHECK_EQUAL(Message("a", ' ', true).str(), "a 1"s);
31+
BOOST_CHECK_EQUAL(Message("a", Message("b", 'c')).str(), "abc"s);
32+
}
33+
34+
BOOST_AUTO_TEST_CASE(test_message_can_convert_to_string) {
35+
using namespace ecf;
36+
using namespace std::string_literals;
37+
38+
std::string s = Message("a", Message("b", 'c'));
39+
BOOST_CHECK_EQUAL(s, "abc"s);
40+
}
41+
42+
BOOST_AUTO_TEST_CASE(test_message_can_stream) {
43+
using namespace ecf;
44+
using namespace std::string_literals;
45+
46+
std::ostringstream oss;
47+
oss << Message("a", Message("b", 'c'));
48+
49+
BOOST_CHECK_EQUAL(oss.str(), "abc"s);
50+
}
51+
52+
BOOST_AUTO_TEST_SUITE_END()
53+
54+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)