Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:10:13

0001 /**
0002  * Copyright (c) 2017-present, Facebook, Inc.
0003  * All rights reserved.
0004  *
0005  * This source code is licensed under the BSD-style license found in the
0006  * LICENSE file in the root directory of this source tree.
0007  */
0008 
0009 #pragma once
0010 
0011 #include <string>
0012 
0013 namespace gloo {
0014 namespace transport {
0015 namespace tcp {
0016 
0017 class Error {
0018  public:
0019   // Constant instance that indicates success.
0020   static const Error kSuccess;
0021 
0022   /* implicit */ Error() : valid_(false) {}
0023 
0024   virtual ~Error() = default;
0025 
0026   // Converting to boolean means checking if there is an error. This
0027   // means we don't need to use an `std::optional` and allows for a
0028   // snippet like the following:
0029   //
0030   //   if (error) {
0031   //     // Deal with it.
0032   //   }
0033   //
0034   operator bool() const {
0035     return valid_;
0036   }
0037 
0038   // Returns an explanatory string.
0039   // Like `std::exception` but returns a `std::string`.
0040   virtual std::string what() const;
0041 
0042  protected:
0043   explicit Error(bool valid) : valid_(valid) {}
0044 
0045  private:
0046   const bool valid_;
0047 };
0048 
0049 class SystemError : public Error {
0050  public:
0051   explicit SystemError(const char* syscall, int error)
0052       : Error(true), syscall_(syscall), error_(error) {}
0053 
0054   std::string what() const override;
0055 
0056  private:
0057   const char* syscall_;
0058   const int error_;
0059 };
0060 
0061 class ShortReadError : public Error {
0062  public:
0063   ShortReadError(ssize_t expected, ssize_t actual)
0064       : Error(true), expected_(expected), actual_(actual) {}
0065 
0066   std::string what() const override;
0067 
0068  private:
0069   const ssize_t expected_;
0070   const ssize_t actual_;
0071 };
0072 
0073 class ShortWriteError : public Error {
0074  public:
0075   ShortWriteError(ssize_t expected, ssize_t actual)
0076       : Error(true), expected_(expected), actual_(actual) {}
0077 
0078   std::string what() const override;
0079 
0080  private:
0081   const ssize_t expected_;
0082   const ssize_t actual_;
0083 };
0084 
0085 } // namespace tcp
0086 } // namespace transport
0087 } // namespace gloo