task.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 #pragma once
18 
21 
22 #include <future>
23 #include <memory>
24 
25 #pragma once
26 
27 namespace Aws {
28 namespace DataFlow {
29 
34  UNKNOWN, // safe default
35  FAIL, // the upload failed
36  SUCCESS, // the upload succeeded
37  INVALID_DATA // the upload was attempted and failed because the input data is bad and will never succeed
38 };
39 
46 template<typename T>
47 class IPublisher {
48 public:
49  virtual UploadStatus attemptPublish(T &batch_data) = 0;
50 };
51 
55 template <typename Status, typename T>
56 using UploadStatusFunction = std::function<void (const Status& upload_status, const T &message)>;
57 
62 template <typename T>
63 class Task {
64 public:
65  virtual ~Task() = default;
66 
73  virtual void run(std::shared_ptr<IPublisher<T>> publisher) {
74  auto status = publisher->attemptPublish(getBatchData());
75  this->onComplete(status);
76  }
77 
81  virtual void cancel() {
82  this->onComplete(FAIL);
83  }
84 
90  virtual void onComplete(const UploadStatus &status) = 0;
91 
97  virtual T& getBatchData() = 0;
98 };
99 
100 template<typename T>
101 class BasicTask :
102  public Task<T> {
103 public:
104  explicit BasicTask(
105  std::shared_ptr<T> batch_data) : Task<T>()
106  {
107  this->batch_data_ = batch_data;
108  // null is allowable as there is a guard above (default action do nothing)
109  this->upload_status_function_ = nullptr;
110  }
111 
112  virtual ~BasicTask() = default;
113 
114  void onComplete(const UploadStatus &status) override {
115  if (upload_status_function_) {
116  upload_status_function_(status, *batch_data_);
117  }
118  }
119 
121  const UploadStatusFunction<UploadStatus, T> upload_status_function)
122  {
123  // null is allowable as there is a guard above (default action do nothing)
124  upload_status_function_ = upload_status_function;
125  }
126 
127  T& getBatchData() override {
128  return *batch_data_;
129  }
130 
131 private:
132  std::shared_ptr<T> batch_data_;
134 };
135 
136 } // namespace DataFlow
137 } // namespace Aws
UploadStatus
Definition: task.h:33
UploadStatusFunction< UploadStatus, T > upload_status_function_
Definition: task.h:133
virtual void cancel()
Definition: task.h:81
T & getBatchData() override
Definition: task.h:127
void setOnCompleteFunction(const UploadStatusFunction< UploadStatus, T > upload_status_function)
Definition: task.h:120
void onComplete(const UploadStatus &status) override
Definition: task.h:114
std::function< void(const Status &upload_status, const T &message)> UploadStatusFunction
Definition: task.h:56
virtual void run(std::shared_ptr< IPublisher< T >> publisher)
Definition: task.h:73
virtual UploadStatus attemptPublish(T &batch_data)=0
BasicTask(std::shared_ptr< T > batch_data)
Definition: task.h:104
std::shared_ptr< T > batch_data_
Definition: task.h:132


dataflow_lite
Author(s): AWS RoboMaker
autogenerated on Fri May 7 2021 02:18:22