| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 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 |
#ifndef SRC_PROGRESS_UPDATER_H |
| 18 |
#define SRC_PROGRESS_UPDATER_H |
| 19 |
|
| 20 |
#include <condition_variable> |
| 21 |
#include <functional> |
| 22 |
#include <iomanip> |
| 23 |
#include <iostream> |
| 24 |
#include <thread> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
class ProgressUpdater { |
| 28 |
public: |
| 29 |
explicit ProgressUpdater(size_t numProgressBars) : mNumBars(numProgressBars) { |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Initializes progress bars and starts the rendering thread. |
| 34 |
* Do not output to stdout before calling stop(). |
| 35 |
*/ |
| 36 |
void start(); |
| 37 |
/** |
| 38 |
* Shutdowns the rendering thread. |
| 39 |
*/ |
| 40 |
void stop(); |
| 41 |
|
| 42 |
void update(size_t progressBarIndex, float value); |
| 43 |
void update(size_t progressBarIndex, size_t value, size_t max); |
| 44 |
|
| 45 |
private: |
| 46 |
struct Update { |
| 47 |
size_t index; |
| 48 |
float value; |
| 49 |
}; |
| 50 |
|
| 51 |
void initProgressValues(); |
| 52 |
void printProgressBars(); |
| 53 |
void printUpdates(); |
| 54 |
void printUpdates(const std::vector<Update>& updates); |
| 55 |
|
| 56 |
size_t mNumBars; |
| 57 |
std::vector<float> mProgress; |
| 58 |
|
| 59 |
std::mutex mMutex; |
| 60 |
std::condition_variable mCondition; |
| 61 |
|
| 62 |
std::vector<Update> mUpdates; |
| 63 |
std::thread mThread; |
| 64 |
bool mExitRequested = false; |
| 65 |
}; |
| 66 |
|
| 67 |
#endif // SRC_PROGRESS_UPDATER_H |