Line data Source code
1 : ///
2 : /// @file ar/file.h
3 : /// @copyright (c) 2015 by Petr Zemek (s3rvac@gmail.com) and contributors
4 : /// @license MIT, see the @c LICENSE file for more details
5 : /// @brief Representation and factory for files.
6 : ///
7 :
8 : #ifndef AR_FILE_H
9 : #define AR_FILE_H
10 :
11 : #include <memory>
12 : #include <string>
13 : #include <vector>
14 :
15 : namespace ar {
16 :
17 : ///
18 : /// Base class and factory for files.
19 : ///
20 52 : class File {
21 : public:
22 : virtual ~File() = 0;
23 :
24 : virtual std::string getName() const = 0;
25 : virtual std::string getContent() = 0;
26 : virtual void saveCopyTo(const std::string& directoryPath) = 0;
27 : virtual void saveCopyTo(const std::string& directoryPath,
28 : const std::string& name) = 0;
29 :
30 : static std::unique_ptr<File> fromContentWithName(
31 : const std::string& content, const std::string& name);
32 : static std::unique_ptr<File> fromFilesystem(const std::string& path);
33 : static std::unique_ptr<File> fromFilesystemWithOtherName(
34 : const std::string& path, const std::string& name);
35 :
36 : /// @name Disabled
37 : /// @{
38 : File(const File&) = delete;
39 : File(File&&) = delete;
40 : File& operator=(const File&) = delete;
41 : File& operator=(File&&) = delete;
42 : /// @}
43 :
44 : protected:
45 : File();
46 : };
47 :
48 : ///
49 : /// A vector-like container storing files.
50 : ///
51 41 : class Files {
52 : private:
53 : /// Underlying type of a container in which files are stored.
54 : using Container = std::vector<std::unique_ptr<File>>;
55 :
56 : public:
57 : using size_type = Container::size_type;
58 : using value_type = Container::value_type;
59 : using reference = Container::reference;
60 : using iterator = Container::iterator;
61 :
62 : public:
63 : Files();
64 : Files(Files&& other);
65 : ~Files();
66 :
67 : /// @name File Access
68 : /// @{
69 : reference front();
70 : reference back();
71 : /// @}
72 :
73 : /// @name Iterators
74 : /// @{
75 : iterator begin() noexcept;
76 : iterator end() noexcept;
77 : /// @}
78 :
79 : /// @name Capacity
80 : /// @{
81 : bool empty() const noexcept;
82 : size_type size() const noexcept;
83 : /// @}
84 :
85 : /// @name Modifiers
86 : /// @{
87 : void push_back(value_type file);
88 : /// @}
89 :
90 : private:
91 : Container files;
92 : };
93 :
94 : } // namespace ar
95 :
96 : #endif
|