status_builder.h 1.2 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

14 15
#ifndef OR_TOOLS_BASE_STATUS_BUILDER_H_
#define OR_TOOLS_BASE_STATUS_BUILDER_H_
Valentin Platzgummer's avatar
Valentin Platzgummer committed
16

17
#include <sstream>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
18

19
#include "absl/status/status.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
20

21 22 23
namespace util {

class StatusBuilder {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
24
 public:
25 26 27
  explicit StatusBuilder(const absl::Status& status) : code_(status.code()) {
    ss_ << std::string(status.message());
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
28

29 30 31
  operator absl::Status() const {  // NOLINT
    return absl::Status(code_, ss_.str());
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
32

33 34 35 36 37
  template <class T>
  StatusBuilder& operator<<(const T& t) {
    ss_ << t;
    return *this;
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
38

39 40 41
 private:
  const absl::StatusCode code_;
  std::ostringstream ss_;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
42 43
};

44
}  // namespace util
Valentin Platzgummer's avatar
Valentin Platzgummer committed
45

46
#endif  // OR_TOOLS_BASE_STATUS_BUILDER_H_