Design Pattern Examples
Overview of object-oriented design patterns
cplusplus/Composite_FileDirEntry.h
Go to the documentation of this file.
1
7
8#pragma once
9#ifndef __COMPOSITE_FILEDIRENTRY_H__
10#define __COMPOSITE_FILEDIRENTRY_H__
11
12#include <string>
13#include <vector>
14#include "helpers/DateTime.h"
16
18{
19
25 {
30
35 };
36
37 class FileDirEntry; // forward declaration
38
42 typedef std::vector<std::shared_ptr<FileDirEntry>> FileDirEntryList;
43
48 {
49 protected:
51 std::string name;
52 long length;
54
55 public:
61 , length(0)
62 {
63 }
64
74 FileDirEntry(FileDirTypes type, std::string entryName, long size, DateTime modDate)
75 : fileDirType(type)
76 , name(entryName)
77 , length(size)
78 , whenModified(modDate)
79 {
80 }
81
85 virtual FileDirTypes FileDirType() { return fileDirType; }
86
90 virtual std::string Name() { return name; }
91
96 virtual long Length() { return length; }
97
101 virtual DateTime WhenModified() { return whenModified; }
102
108 };
109
110
111 //########################################################################
112 //########################################################################
113
119 class FileEntry : public FileDirEntry
120 {
121 public:
129 FileEntry(std::string entryName, long size, DateTime modDate)
130 : FileDirEntry(FileDirTypes::File, entryName, size, modDate)
131 {
132 }
133 };
134
135
136
137 //########################################################################
138 //########################################################################
139
140
149 class DirEntry : public FileDirEntry
150 {
151 public:
154
161 DirEntry(std::string entryName, DateTime modDate, FileDirEntryList children)
162 : FileDirEntry(FileDirTypes::Directory, entryName, 0, modDate)
163 , _children(children)
164 , _lengthSet(false)
165 {
166 }
167
168
175 long Length()
176 {
177 if (!_lengthSet)
178 {
179 // Recurse over all children, accumulating their size.
180 for (FileDirEntryList::iterator entryIter = std::begin(_children);
181 entryIter != std::end(_children);
182 entryIter++)
183 {
184 length += (*entryIter)->Length();
185 }
186 _lengthSet = true;
187 }
188 return length;
189 }
190
191
200 {
201 return _children;
202 }
203 };
204
205} // end namespace
206
207#endif //__COMPOSITE_FILEDIRENTRY_H__
208
Represents a Directory entry.
FileDirEntryList _children
bool _lengthSet
FileDirEntryList Children()
Retrieve the children of this node.
DirEntry(std::string entryName, DateTime modDate, FileDirEntryList children)
Construct a DirEntry instance.
long Length()
Retrieve the size of all children of this directory. The length is calculated on the first call and c...
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.
FileDirEntry()
Default constructor.
DateTime whenModified
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.
FileDirTypes fileDirType
std::string name
FileDirEntry(FileDirTypes type, std::string entryName, long size, DateTime modDate)
Constructor.
virtual FileDirEntryList Children()
The children of this entry. Is empty if the entry can never have any children (that is,...
long length
Represents a File entry.
FileEntry(std::string entryName, long size, DateTime modDate)
Constructor.
Represents a timestamp composed of a date and a time encoded in a time_t value. Provides ways of gett...
The namespace containing all Design Pattern Examples implemented in C++.
FileDirTypes
Represents the type of entries allowed in the hierarchy for the Composite design pattern example.
@ Directory
Represents a directory entry that can contain other FileDirEntry components.
std::vector< std::shared_ptr< FileDirEntry > > FileDirEntryList
Makes it easier to refer to a list of child nodes.
Structure representing a File (FileEntry) or Directory (DirEntry) entry. This is included as the firs...