Home | History | Annotate | Download | only in cc
      1 /*
      2  * Copyright (c) 2015 PLUMgrid, Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #pragma once
     18 
     19 #include <cstdio>
     20 #include <string>
     21 
     22 namespace ebpf {
     23 
     24 class StatusTuple {
     25 public:
     26   StatusTuple(int ret) : ret_(ret) {}
     27 
     28   StatusTuple(int ret, const char *msg) : ret_(ret), msg_(msg) {}
     29 
     30   StatusTuple(int ret, const std::string &msg) : ret_(ret), msg_(msg) {}
     31 
     32   template <typename... Args>
     33   StatusTuple(int ret, const char *fmt, Args... args) : ret_(ret) {
     34     char buf[2048];
     35     snprintf(buf, sizeof(buf), fmt, args...);
     36     msg_ = std::string(buf);
     37   }
     38 
     39   void append_msg(const std::string& msg) {
     40     msg_ += msg;
     41   }
     42 
     43   int code() { return ret_; }
     44 
     45   std::string msg() { return msg_; }
     46 
     47 private:
     48   int ret_;
     49   std::string msg_;
     50 };
     51 
     52 #define TRY2(CMD)              \
     53   do {                         \
     54     StatusTuple __stp = (CMD); \
     55     if (__stp.code() != 0) {   \
     56       return __stp;            \
     57     }                          \
     58   } while (0)
     59 
     60 }  // namespace ebpf
     61