Program Listing for File result.hpp

Return to documentation for file (include/nix/valid/result.hpp)

// Copyright (c) 2014, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE file in the root of the Project.

#ifndef NIX_RESULT_H
#define NIX_RESULT_H

#include <nix/Platform.hpp>
#include <nix/None.hpp>
#include <nix/valid/helper.hpp>

#include <vector>
#include <string>
#include <ostream>

namespace nix {
namespace valid {

class NIXAPI Result {
    std::vector<Message> errors;
    std::vector<Message> warnings;
public:

    Result(const std::vector<Message> &errs = std::vector<Message>(),
           const std::vector<Message> &warns = std::vector<Message>());

    Result(none_t t, const std::vector<Message> &warns);

    Result(const std::vector<Message> & errs, none_t t);

    Result(none_t t, const Message &warn);

    Result(const Message &err, none_t t);

    std::vector<Message> getWarnings() const;

    std::vector<Message> getErrors() const;

    Result concat(const Result &result);

    Result addError(const Message &error);

    Result addWarning(const Message &warning);

    bool ok() const;

    bool hasErrors() const;

    bool hasWarnings() const;

    NIXAPI friend std::ostream& operator<<(std::ostream &out, const Result &res) {
        // make temp copies to set prefixes on
        std::vector<Message> tmp_errors = res.getErrors();
        std::vector<Message> tmp_warnings = res.getWarnings();

        for (const auto &warn : tmp_warnings) {
            if (!warn.id.empty())
                out << "ID " << warn.id << " ";
            out << "WARNING: " << warn.msg << std::endl;
        }

        for (const auto &err : tmp_errors) {
            if (!err.id.empty())
                out << "ID " << err.id << " ";

            out << "ERROR: " << err.msg << std::endl;
        }

        return out;
    }
};

} // namespace valid
} // namespace nix

#endif // NIX_RESULT_H