|
| 1 | +/** |
| 2 | + * \file |
| 3 | + * \author Michal Sedlak <[email protected]> |
| 4 | + * \brief Thread safe file list |
| 5 | + * |
| 6 | + * Copyright (C) 2021 CESNET, z.s.p.o. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer in |
| 15 | + * the documentation and/or other materials provided with the |
| 16 | + * distribution. |
| 17 | + * 3. Neither the name of the Company nor the names of its contributors |
| 18 | + * may be used to endorse or promote products derived from this |
| 19 | + * software without specific prior written permission. |
| 20 | + * |
| 21 | + * ALTERNATIVELY, provided that this notice is retained in full, this |
| 22 | + * product may be distributed under the terms of the GNU General Public |
| 23 | + * License (GPL) version 2 or later, in which case the provisions |
| 24 | + * of the GPL apply INSTEAD OF those given above. |
| 25 | + * |
| 26 | + * This software is provided ``as is, and any express or implied |
| 27 | + * warranties, including, but not limited to, the implied warranties of |
| 28 | + * merchantability and fitness for a particular purpose are disclaimed. |
| 29 | + * In no event shall the company or contributors be liable for any |
| 30 | + * direct, indirect, incidental, special, exemplary, or consequential |
| 31 | + * damages (including, but not limited to, procurement of substitute |
| 32 | + * goods or services; loss of use, data, or profits; or business |
| 33 | + * interruption) however caused and on any theory of liability, whether |
| 34 | + * in contract, strict liability, or tort (including negligence or |
| 35 | + * otherwise) arising in any way out of the use of this software, even |
| 36 | + * if advised of the possibility of such damage. |
| 37 | + * |
| 38 | + */ |
| 39 | +#pragma once |
| 40 | + |
| 41 | +#include <string> |
| 42 | +#include <mutex> |
| 43 | +#include <vector> |
| 44 | + |
| 45 | +/** |
| 46 | + * \brief A file list that allows thread safe retrieval of items; |
| 47 | + */ |
| 48 | +class FileList { |
| 49 | +public: |
| 50 | + using Iterator = std::vector<std::string>::const_iterator; |
| 51 | + |
| 52 | + /** |
| 53 | + * \brief Add files matching the specified pattern onto the list. |
| 54 | + * \param[in] pattern The file path glob pattern. |
| 55 | + */ |
| 56 | + void |
| 57 | + add_files(const std::string &pattern); |
| 58 | + |
| 59 | + /** |
| 60 | + * \brief A thread safe method to retrieve a filename off the list. |
| 61 | + * \param[out] filename The filename |
| 62 | + * \return true if the filename was retrieved, false if the list was empty |
| 63 | + */ |
| 64 | + bool |
| 65 | + pop(std::string &filename); |
| 66 | + |
| 67 | + /** |
| 68 | + * \brief Clear all files in the list. |
| 69 | + */ |
| 70 | + void clear(); |
| 71 | + |
| 72 | + /** |
| 73 | + * \brief Get the length of the list |
| 74 | + * \return The length of the list |
| 75 | + */ |
| 76 | + size_t length() const { return m_files.size(); } |
| 77 | + |
| 78 | + /** |
| 79 | + * \brief Check whether the list is empty |
| 80 | + * \return true or false |
| 81 | + */ |
| 82 | + bool empty() const { return m_files.empty(); } |
| 83 | + |
| 84 | + /** |
| 85 | + * \brief Get an iterator to the beginning of the list |
| 86 | + * \return The constant iterator |
| 87 | + */ |
| 88 | + Iterator begin() const { return m_files.cbegin(); } |
| 89 | + |
| 90 | + /** |
| 91 | + * \brief Get an iterator representing the end of the list |
| 92 | + * \return The constant iterator |
| 93 | + */ |
| 94 | + Iterator end() const { return m_files.cend(); } |
| 95 | + |
| 96 | +private: |
| 97 | + std::mutex m_mutex; |
| 98 | + std::vector<std::string> m_files; |
| 99 | +}; |
0 commit comments