Merge pull request #748 from jokva/message-container-internal-detail

Remove MessageContainer from ParseContext + storage type change
This commit is contained in:
Joakim Hove
2016-04-06 17:15:06 +02:00
5 changed files with 125 additions and 93 deletions

View File

@@ -24,95 +24,115 @@
namespace Opm {
void MessageContainer::error(const std::string& msg, const std::string& filename, const int lineno)
Location::Location( const std::string& fn, size_t ln ) :
filename( fn ), lineno( ln )
{
std::unique_ptr<Location> loc(new Location{filename, lineno});
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Error, msg, std::move(loc)});
if( ln == 0 )
throw std::invalid_argument( "Invalid line number 0 for file '"
+ fn + "'" );
}
void MessageContainer::error( const std::string& msg,
const std::string& filename,
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Error, msg, Location { filename, lineno } }
);
}
void MessageContainer::error(const std::string& msg)
{
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Error, msg, nullptr});
void MessageContainer::error( const std::string& msg ) {
m_messages.emplace_back( Message { Message::Error, msg, {} } );
}
void MessageContainer::bug(const std::string& msg, const std::string& filename, const int lineno)
{
std::unique_ptr<Location> loc(new Location{filename, lineno});
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Bug, msg, std::move(loc)});
void MessageContainer::bug( const std::string& msg,
const std::string& filename,
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Bug, msg, { filename, lineno } }
);
}
void MessageContainer::bug(const std::string& msg)
{
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Bug, msg, nullptr});
void MessageContainer::bug( const std::string& msg ) {
m_messages.emplace_back( Message { Message::Bug, msg, {} } );
}
void MessageContainer::warning(const std::string& msg, const std::string& filename, const int lineno)
{
std::unique_ptr<Location> loc(new Location{filename, lineno});
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Warning, msg, std::move(loc)});
void MessageContainer::warning( const std::string& msg,
const std::string& filename,
const size_t lineno ) {
m_messages.emplace_back( Message {
Message::Warning, msg, { filename, lineno }
}
);
}
void MessageContainer::warning(const std::string& msg)
{
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Warning, msg, nullptr});
void MessageContainer::warning( const std::string& msg ) {
m_messages.emplace_back(
Message { Message::Warning, msg,{} }
);
}
void MessageContainer::info(const std::string& msg, const std::string& filename, const int lineno)
{
std::unique_ptr<Location> loc(new Location{filename, lineno});
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Info, msg, std::move(loc)});
void MessageContainer::info( const std::string& msg,
const std::string& filename,
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Info, msg, { filename, lineno } }
);
}
void MessageContainer::info(const std::string& msg)
{
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Info, msg, nullptr});
void MessageContainer::info( const std::string& msg ) {
m_messages.emplace_back( Message { Message::Info, msg, {} } );
}
void MessageContainer::debug(const std::string& msg, const std::string& filename, const int lineno)
{
std::unique_ptr<Location> loc(new Location{filename, lineno});
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Debug, msg, std::move(loc)});
void MessageContainer::debug( const std::string& msg,
const std::string& filename,
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Debug, msg, { filename, lineno } }
);
}
void MessageContainer::debug(const std::string& msg)
{
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Debug, msg, nullptr});
void MessageContainer::debug( const std::string& msg ) {
m_messages.emplace_back( Message { Message::Debug, msg, {} } );
}
void MessageContainer::problem(const std::string& msg, const std::string& filename, const int lineno)
{
std::unique_ptr<Location> loc(new Location{filename, lineno});
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Problem, msg, std::move(loc)});
void MessageContainer::problem( const std::string& msg,
const std::string& filename,
const size_t lineno ) {
m_messages.emplace_back(
Message { Message::Problem, msg, { filename, lineno } }
);
}
void MessageContainer::problem(const std::string& msg)
{
m_messages.emplace_back(Message{MessageType::MessageTypeEnum::Problem, msg, nullptr});
void MessageContainer::problem( const std::string& msg ) {
m_messages.emplace_back( Message { Message::Problem, msg, {} } );
}
void MessageContainer::add( const Message& msg ) {
this->m_messages.push_back( msg );
}
void MessageContainer::add( Message&& msg ) {
this->m_messages.push_back( std::move( msg ) );
}
std::vector<Message>::const_iterator MessageContainer::begin() const
{
MessageContainer::const_iterator MessageContainer::begin() const {
return m_messages.begin();
}
std::vector<Message>::const_iterator MessageContainer::end() const
{
MessageContainer::const_iterator MessageContainer::end() const {
return m_messages.end();
}

View File

@@ -20,17 +20,26 @@
#ifndef MESSAGECONTAINER_H
#define MESSAGECONTAINER_H
#endif // OPM_MESSAGECONTAINER_HEADER_INCLUDED
#include <string>
#include <vector>
#include <memory>
namespace Opm {
namespace MessageType {
struct Location {
Location() = default;
Location( const std::string&, size_t );
enum MessageTypeEnum {
std::string filename;
size_t lineno = 0;
explicit operator bool() const {
return lineno != 0;
}
};
struct Message {
enum type {
Debug = 1,
Info = 2,
Warning = 3,
@@ -39,47 +48,52 @@ namespace Opm {
Bug = 6
};
} // namespace MessageType
Message( type mt, const std::string& msg, Location&& loc ) :
mtype( mt ), message( msg ), location( std::move( loc ) ) {}
Message( type mt, const std::string& msg ) :
mtype( mt ), message( msg ) {}
struct Location {
std::string filename;
int lineno;
};
struct Message {
MessageType::MessageTypeEnum mtype;
type mtype;
std::string message;
std::unique_ptr<Location> location;
Location location;
};
///Message container is used to replace OpmLog functionalities.
class MessageContainer {
public:
void error(const std::string& msg, const std::string& filename, const int lineno);
using const_iterator = std::vector< Message >::const_iterator;
void error(const std::string& msg, const std::string& filename, const size_t lineno);
void error(const std::string& msg);
void bug(const std::string& msg, const std::string& filename, const int lineno);
void bug(const std::string& msg, const std::string& filename, const size_t lineno);
void bug(const std::string& msg);
void warning(const std::string& msg, const std::string& filename, const int lineno);
void warning(const std::string& msg, const std::string& filename, const size_t lineno);
void warning(const std::string& msg);
void info(const std::string& msg, const std::string& filename, const int lineno);
void info(const std::string& msg, const std::string& filename, const size_t lineno);
void info(const std::string& msg);
void debug(const std::string& msg, const std::string& filename, const int lineno);
void debug(const std::string& msg, const std::string& filename, const size_t lineno);
void debug(const std::string& msg);
void problem(const std::string& msg, const std::string& filename, const int lineno);
void problem(const std::string& msg, const std::string& filename, const size_t lineno);
void problem(const std::string& msg);
std::vector<Message>::const_iterator begin() const;
std::vector<Message>::const_iterator end() const;
void add( const Message& );
void add( Message&& );
const_iterator begin() const;
const_iterator end() const;
private:
std::vector<Message> m_messages;
};
} // namespace Opm
#endif // OPM_MESSAGECONTAINER_HEADER_INCLUDED

View File

@@ -77,14 +77,20 @@ namespace Opm {
}
void ParseContext::handleError( const std::string& errorKey , const std::string& msg) const {
Message::type ParseContext::handleError(
const std::string& errorKey,
const std::string& msg ) const {
InputError::Action action = get( errorKey );
if (action == InputError::WARN)
OpmLog::addMessage(Log::MessageType::Warning , msg);
return Message::Warning;
else if (action == InputError::THROW_EXCEPTION)
throw std::invalid_argument(errorKey + ": " + msg);
return Message::Debug;
}
std::map<std::string,InputError::Action>::const_iterator ParseContext::begin() const {
@@ -96,19 +102,6 @@ namespace Opm {
return m_errorContexts.end();
}
const MessageContainer& ParseContext::getMessageContainer() const
{
return m_messageContainer;
}
MessageContainer& ParseContext::getMessageContainer()
{
return m_messageContainer;
}
bool ParseContext::hasKey(const std::string& key) const {
if (m_errorContexts.find( key ) == m_errorContexts.end())
return false;

View File

@@ -80,7 +80,7 @@ namespace Opm {
public:
ParseContext();
ParseContext(const std::vector<std::pair<std::string , InputError::Action>> initial);
void handleError( const std::string& errorKey , const std::string& msg) const;
Message::type handleError( const std::string& errorKey, const std::string& msg ) const;
bool hasKey(const std::string& key) const;
void updateKey(const std::string& key , InputError::Action action);
void update(InputError::Action action);
@@ -88,8 +88,6 @@ namespace Opm {
InputError::Action get(const std::string& key) const;
std::map<std::string,InputError::Action>::const_iterator begin() const;
std::map<std::string,InputError::Action>::const_iterator end() const;
const MessageContainer& getMessageContainer() const;
MessageContainer& getMessageContainer();
/*
When the key is added it is inserted in 'strict mode',
i.e. with the value 'InputError::THROW_EXCEPTION. If you
@@ -199,7 +197,6 @@ namespace Opm {
void envUpdate( const std::string& envVariable , InputError::Action action );
void patternUpdate( const std::string& pattern , InputError::Action action);
std::map<std::string , InputError::Action> m_errorContexts;
MessageContainer m_messageContainer;
}; }

View File

@@ -34,8 +34,8 @@ BOOST_AUTO_TEST_CASE(TestIterator) {
msgContainer.bug("This is a bug.", "dummy.log", 20);
{
BOOST_CHECK_EQUAL("This is an error.", msgContainer.begin()->message);
BOOST_CHECK_EQUAL("dummy.log", (msgContainer.end()-1)->location->filename);
BOOST_CHECK_EQUAL(20, (msgContainer.end()-1)->location->lineno);
BOOST_CHECK_EQUAL("dummy.log", (msgContainer.end()-1)->location.filename);
BOOST_CHECK_EQUAL(20, (msgContainer.end()-1)->location.lineno);
}
MessageContainer msgList;
@@ -51,6 +51,14 @@ BOOST_AUTO_TEST_CASE(TestIterator) {
BOOST_CHECK_EQUAL(msg.message, msgString[i]);
i++;
}
}
BOOST_AUTO_TEST_CASE(LocationImplicitConversion) {
MessageContainer mc;
mc.warning( "Warning" );
mc.info( "Info", "filename", 10 );
BOOST_CHECK( !mc.begin()->location );
BOOST_CHECK( (mc.begin() + 1)->location );
BOOST_CHECK_THROW( mc.info( "msg", "filename", 0 ), std::invalid_argument );
}