status.hpp 13.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "config.hpp"

#include <errno.h>

#include <cassert>
#include <cstring>
#include <map>
#include <string>

namespace VideoStitch {

enum class Origin {
  // algorithms
  BlendingMaskAlgorithm,
  CalibrationAlgorithm,
  CropAlgorithm,
  ExposureAlgorithm,
  PhotometricCalibrationAlgorithm,
  MaskInterpolationAlgorithm,
  ScoringAlgorithm,
  StabilizationAlgorithm,
  SynchronizationAlgorithm,
  MotionEstimationAlgorithm,
  EpipolarCurvesAlgorithm,

  // audio
  AudioPipeline,
  AudioPreProcessor,

  // blending
  ImageFlow,
  ImageWarper,

  // stitching pipeline
  Input,
  PreProcessor,
  Stitcher,
  PostProcessor,
  Output,
  Surface,

  // project handling
  PanoramaConfiguration,
  AudioPipelineConfiguration,

  // low level GPU backend
  GPU,

  // utils
  GeometryProcessingUtils,

  ExternalModule,
  Unspecified,
};

enum class ErrType {
  // unable to acquire a crucial resource
  OutOfResources,

  // a component needed to finish the operation could not be set up correctly,
  // usually has an underlying cause
  SetupFailure,

  InvalidConfiguration,

  // encountered an irrecoverable, unexpected error on runtime
  RuntimeError,

  // unable to compute a valid result
  AlgorithmFailure,

  // did not finish as user requested to stop
  OperationAbortedByUser,

  // a feature or configuration that has not been implemented
  UnsupportedAction,

  // runtime sanity check. asserts false.
  ImplementationError,

  None,
};

/**
 * An object that is either indicating normal operation (.ok())
 * or holds an error description on the encountered failure
 */
class VS_EXPORT Status {
 public:
  /**
   * Create an Ok Status
   */
  Status();

  /**
   * Create an Ok Status
   */
  static Status OK() { return Status(); }

  /**
   * Create an error Status
   */
  Status(Origin o, ErrType t, const std::string& message);

  /**
   * Create an error Status with an underlying cause
   */
  Status(Origin o, ErrType t, const std::string& message, Status underlyingCause);

  /**
   * Create an error Status
   */
  Status(Origin o, ErrType t, const std::string& tag, const std::string& message);

  /**
   * Create an error Status with an underlying cause
   */
  Status(Origin o, ErrType t, const std::string& tag, const std::string& message, Status underlyingCause);

  /**
   * Return the module or point of origin
   */
  Origin getOrigin() const;

  /**
   * Return the type
   */
  ErrType getType() const;

  /**
   * Return the runtime error information (English string)
   */
  std::string getErrorMessage() const;

  // these convenience methods don't require access to Status internals
  // and could be moved outside
  // ---
  std::string getOriginString() const;
  std::string getTypeString() const;
  // ---

  /**
   * Copy contructor.
   * @param other copy source
   */
  Status(const Status& other);

  /**
   * Copy assignment constructor
   * @param other copy source
   */
  Status& operator=(Status other) {
    std::swap(description, other.description);
    return *this;
  }

  ~Status();

  /**
   * Is the status Ok?
   */
  bool ok() const { return !description; }

  /**
   * Is there information on the underlying cause?
   */
  bool hasCause() const;

  /**
   * Access the underlying cause. Only valid if hasCause().
   */
  const Status& getCause() const;

  /**
   * Check the error type recursively in all underlying causes
   * @param t The error type to check
   * @return true if the status or one of its causes has this error type
   */
  bool hasUnderlyingCause(ErrType t) const;

 private:
  class Description;
  Description* description;
};

/** Result<CustomStatusCode> provides a more customizable Status
 *  Valid cases are not just 'Ok', but also the additional states
 *  defined in the enum class CustomStatusCode.
 *
 *  This is useful for operations that can result in a certain state
 *  that is acted upon by the caller, without this state being a runtime error.
 *
 *  Result<T> thus is either:
 *  1) Ok: still the default case. Create with Result<T>::OK(), test with .ok()
 *  2) custom code: in a custom state defined in enum class T
 *  3) ErrorWithStatus: in an error state, declared with an enclosed `Status`
 *
 *  Note: a CustomCode is not ok() and does not enclose an error Status
 */
template <typename CustomStatusCode>
class Result {
 public:
  typedef CustomStatusCode StatusCode;

  static_assert(sizeof StatusCode::Ok, "CustomStatusCode must be an enum class and must contain `Ok`");
  static_assert(sizeof StatusCode::ErrorWithStatus,
                "CustomStatusCode must an enum class must contain `ErrorWithStatus`");

  /**
   * Create an Ok Result, the default state
   */
  Result() : status(), code(StatusCode::Ok) {}

  /**
   * Create an Ok Result, the default state
   */
  static Result OK() { return Result(); }

  /**
   * Create an Result with a custom defined state
   *
   * The parameter state must not be Ok or ErrorWithStatus
   * as they define special states.
   */
  template <StatusCode state>
  static Result fromCode() {
    static_assert(state != StatusCode::Ok, "Use Result::OK()");
    static_assert(state != StatusCode::ErrorWithStatus, "Use fromError()");
    return Result(state);
  }

  /**
   * Create an Result with a custom defined state from an error status
   */
  static Result fromError(const Status& errorStatus) { return Result(errorStatus); }

  /**
   * Create an error Result, enclosing a Status
   */
  Result(Status genericStatus) : status(genericStatus) {
    if (genericStatus.ok()) {
      code = StatusCode::Ok;
    } else {
      code = StatusCode::ErrorWithStatus;
    }
  }

  bool ok() const { return code == StatusCode::Ok; }

  /**
   * Returns Ok, ErrorWithStatus or a custom state
   */
  StatusCode getCode() const { return code; }

  /**
   * Always Status::OK(), unless `getCode()` returns ErrorWithStatus
   */
  Status getStatus() const { return status; }

 private:
  // Construction should go through ::fromCode which disallows ErrorWithStatus without a Status
  Result(StatusCode state) : status(), code(state) {}

  Status status;
  StatusCode code;
};

/**
 * A deleter.
 */
template <class T>
struct DefaultDeleter {
  /**
   * Create a default deleter.
   */
  DefaultDeleter() {}
  /**
   * Copy constructor.
   */
  DefaultDeleter(const DefaultDeleter<T>& /*other*/) {}

  /**
   * Deletes the given object.
   * @param t Object to delete. Can be NULL.
   */
  void operator()(T* t) const { delete t; }
};

/**
 * An object that provides RAII on a factory-created object with an error status.
 * See DefaultDeleter for the constraints on Deleter.
 */
template <class T, class Deleter = DefaultDeleter<T>, class StatusClass = Status>
class Potential {
 public:
  /**
   * Move constructor. Should use Rvalue references, but we want to handle non-c++11 compilers.
   */
  Potential(const Potential& other) : object_(other.object_), status_(other.status_), deleter_(other.deleter_) {
    const_cast<T*&>(other.object_) = nullptr;
  }

  /**
   * Create a Potential with a null object and a given error
   */
  Potential(Origin o, ErrType t, const std::string& message) : object_(nullptr), status_(o, t, message), deleter_() {}
  Potential(Origin o, ErrType t, const std::string& tag, const std::string& message)
      : object_(nullptr), status_(o, t, tag, message), deleter_() {}

  /**
   * Create a Potential with a null object and a given error and an underlying error cause
   */
  Potential(Origin o, ErrType t, const std::string& message, Status underlyingCause)
      : object_(nullptr), status_(o, t, message, underlyingCause), deleter_() {}
  Potential(Origin o, ErrType t, const std::string& tag, const std::string& message, Status underlyingCause)
      : object_(nullptr), status_(o, t, tag, message, underlyingCause), deleter_() {}

  /**
   * Creates an Potential with a null object and a given status.
   * @param status root status code
   */
  Potential(const StatusClass& status) : object_(NULL), status_(status), deleter_() {
    // TODOSTATUS enable this assertion, it's confusing to have an .ok Potential without a value
    // it's currently used in the algorithms to optionally return a value
    // assert(!status_.ok());
  }

  /**
   * Creates an Potential from the given object
   * @param object Factory-created object. Ownership is transferred to the Potential.
   * @param deleter Custom deleter to be used for deleting @a object.
   */
  Potential(T* object, const Deleter& deleter = Deleter())
      : object_(object), status_(StatusClass::OK()), deleter_(deleter) {
    // Potential(nullptr) used to create a potential with an OutOfResources Status code
    // this feature was removed, create a Potential with a custom Status message if you are out of resources
    assert(object != nullptr);
  }

  ~Potential() { deleter_(object_); }

  /**
   * Returns the object.
   */
  T* operator->() { return object_; }

  /**
   * Returns the object
   */
  const T* operator->() const { return object_; }

  /**
   * Returns the object. Ownership is NOT transfered.
   */
  T* object() const { return object_; }

  /**
   * Returns the status for this object.
   */
  const StatusClass& status() const { return status_; }

  /**
   * Returns whether the Status of this Potential object is Ok
   */
  bool ok() const { return status_.ok(); }

  /**
   * Releases the created object and passes ownership to the caller.
   */
  T* release() {
    T* tmp = object_;
    object_ = NULL;
    return tmp;
  }

 private:
  Potential& operator=(const Potential&);
  T* object_;
  const StatusClass status_;
  const Deleter deleter_;
};

/**
 * An object that stores an optional value of type T and a Status code.
 * Similar to usage of std::optional.
 * A Status is always provided, even if the optional value is present.
 */
template <class T>
class PotentialValue {
 public:
  /**
   * @brief Creates an PotentialValue with a default constructed value and a an error
   */
  PotentialValue(const Status& status) : value_(), status_(status) {}

  /**
   * @brief Creates an PotentialValue with a value and a given status.
   * Useful when there's no default constructor for the value
   * @param status root status code
   * @param value The value to be stored
   */
  PotentialValue(const Status& status, T value) : value_(value), status_(status) {}

  /**
   * @brief Creates an PotentialValue from the given value. Status will be 'Ok', as value is present.
   * @param value The value to be stored
   */
  PotentialValue(const T& value) : value_(value), status_(Status::OK()) {}

  /**
   * @brief Creates a PotentialValue with move semantics
   * @param value The value to be stored
   */
  PotentialValue(T&& value) : value_(std::move(value)), status_(Status::OK()) {}

  /**
   * @brief Affectation operator
   */
  PotentialValue& operator=(const PotentialValue&) = default;

  /**
   * @brief Affectation operator with move semantics
   */
  PotentialValue& operator=(PotentialValue&& other) {
    value_ = std::move(other.value_);
    status_ = other.status_;
    return *this;
  }

  /**
   * @brief Copy constructor
   */
  PotentialValue(const PotentialValue& other) = default;

  /**
   * @brief Copy constructor with move semantics
   */
  PotentialValue(PotentialValue&& other) : value_(std::move(other.value_)), status_(other.status_) {}

  ~PotentialValue() {}

  /**
   * Returns the value
   */
  T value() const { return value_; }

  /**
   * Returns the value
   */
  const T& ref() const { return value_; }

  /**
   * Returns the value
   */
  T&& releaseValue() { return std::move(value_); }

  /**
   * Returns the status for this object.
   */
  const Status& status() const { return status_; }

  /**
   * Returns whether the Status of this potential value is Ok.
   */
  bool ok() const { return status_.ok(); }

 protected:
  T value_;        ///< Stored (optional) value. Default constructed if missing on init.
  Status status_;  ///< Stored status, not optional
};

#define PROPAGATE_FAILURE_STATUS(call)         \
  {                                            \
    const VideoStitch::Status status = (call); \
    if (!status.ok()) {                        \
      return status;                           \
    }                                          \
  }
#define FAIL_RETURN PROPAGATE_FAILURE_STATUS

#define PROPAGATE_FAILURE_CAUSE(call, origin, type, msg) \
  {                                                      \
    const VideoStitch::Status status = (call);           \
    if (!status.ok()) {                                  \
      return Status(origin, type, msg, status);          \
    }                                                    \
  }
#define FAIL_CAUSE PROPAGATE_FAILURE_CAUSE

#define PROPAGATE_FAILURE_MSG(call, msg)                                \
  {                                                                     \
    const VideoStitch::Status status = (call);                          \
    if (!status.ok()) {                                                 \
      return Status(status.getOrigin(), status.getType(), msg, status); \
    }                                                                   \
  }
#define FAIL_MSG PROPAGATE_FAILURE_MSG

#define PROPAGATE_FAILURE_CONDITION(condition, origin, type, msg) \
  {                                                               \
    if (!(condition)) {                                           \
      return Status(origin, type, msg);                           \
    }                                                             \
  }
#define FAIL_CONDITION PROPAGATE_FAILURE_CONDITION
}  // namespace VideoStitch