Design Pattern Examples
Overview of object-oriented design patterns
Composite_Exercise.cpp
Go to the documentation of this file.
1
6
7#include <filesystem>
8#include <iostream>
9
10#include "helpers/formatstring.h"
11
12#include "Composite_Exercise.h"
15
16
17namespace // Anonymous
18{
19 using namespace DesignPatternExamples_cpp;
20
30 std::string Composite_Exercise_FormatEntry(FileDirEntry* entry, int depth)
31 {
32 const size_t NAME_PADDING_SIZE = 20;
33 std::string output = "";
34 std::string spaces(static_cast<size_t>(depth) * 2, ' ');
35 output.append(Helpers::formatstring("%s%s", spaces.c_str(), entry->Name().c_str()));
36 size_t padding = NAME_PADDING_SIZE - entry->Name().size() - (static_cast<size_t>(depth) * 2);
37 if (entry->FileDirType() == FileDirTypes::Directory)
38 {
39 output.append("/");
40 padding--;
41 }
42 output.append(std::string(padding, ' '));
43 output.append(Helpers::formatstring("%4d", entry->Length()));
44 output.append(Helpers::formatstring(" %s", entry->WhenModified().ToString().c_str()));
45 output.append("\n");
46
47 FileDirEntryList children = entry->Children();
48 if (!children.empty())
49 {
50 for (size_t index = 0; index < children.size(); ++index)
51 {
52 output.append(Composite_Exercise_FormatEntry(children[index].get(), depth + 1));
53 }
54 }
55
56 return output;
57 }
58
59
66 {
67 std::string output = Composite_Exercise_FormatEntry(entry, 2);
68 std::cout << output << std::endl;
69 }
70
71} // end namespace anonymous
72
73
75{
76
88 // ! [Using Composite in C++]
90 {
91 std::cout << std::endl;
92 std::cout << "Composite Exercise" << std::endl;
93
94 try
95 {
96 std::string filepath = "root";
97 FileDirEntry* rootEntry = Composite_FileAccess::GetEntry(filepath);
98 std::cout << " Showing object '" << filepath << "'" << std::endl;
100
101 filepath = "root/subdir1/FileD.txt";
102 rootEntry = Composite_FileAccess::GetEntry(filepath);
103 std::cout << " Showing object '" << filepath << "'" << std::endl;
105 }
106 catch (std::filesystem::filesystem_error& e)
107 {
108 std::cout << "Error! filesystem_error: " << e.what() << std::endl;
109 }
110
111 std::cout << " Done." << std::endl;
112 }
113 // ! [Using Composite in C++]
114
115} // end namespace
static void Composite_Exercise_ShowEntry(FileDirEntry *entry)
Recursively display the contents of the hierarchical list of FileDirEntry objects starting with the g...
static void Composite_Exercise_FormatEntry(FileDirEntry *entry, int depth, DynamicString *output)
Format the specified entry for display.
static FileDirEntry * GetEntry(std::string filepath)
Return a FileDirEntry object representing the specified file "path" in an internal list of data entri...
Base class representing a File or Directory entry.
virtual FileDirTypes FileDirType()
The type of this entry as represented by a value from the FileDirTypes enumeration.
virtual std::string Name()
The name of this entry.
virtual DateTime WhenModified()
When this entry was last modified.
virtual long Length()
The length in bytes of this entry. Directory entries are the sum of the length of all children.
virtual FileDirEntryList Children()
The children of this entry. Is empty if the entry can never have any children (that is,...
std::string ToString()
Format the DateTime as a string. The format is "standard" (in this case, preset to 02/22/2023 10:26:1...
Definition: DateTime.cpp:17
Declaration of the Composite_Exercise() function as used in the Composite Pattern.
Declaration of the Composite_FileAccess class used in the Composite Pattern.
Implementation of the FileDirEntry, FileEntry, and DirEntry classes used in the Composite Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
std::vector< std::shared_ptr< FileDirEntry > > FileDirEntryList
Makes it easier to refer to a list of child nodes.
void Composite_Exercise()
Example of using the Composite design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....