OSDN Git Service

Update CWindowsManagerDialog - check some pointers for null and made … (#824) (2)
[winmerge-jp/winmerge-jp.git] / Src / PathContext.h
1 /**
2  *  @file PathContext.h
3  *
4  *  @brief Declarations of PathInfo and PathContext
5  */
6 #pragma once
7
8 #include "UnicodeString.h"
9 #include <vector>
10
11 class PathContext;
12 class PathContextIterator;
13
14 /**
15  * @brief Information for one path.
16  *
17  * Path is stored in normalized format (no trailing slash).
18  */
19 class PathInfo
20 {
21         friend class PathContext;
22 public:
23         PathInfo() {}
24         PathInfo(const PathInfo &pi);
25
26         String GetPath(bool bNormalized = true) const;
27         String& GetRef() { return m_sPath; }
28         void SetPath(const TCHAR *path);
29         void SetPath(const String & path);
30         void NormalizePath();
31
32 private:
33         String m_sPath;  /**< Directory / file path */
34 };
35
36 /**
37  * @brief Set path.
38  * @param [in] sPath New path for item.
39  */
40 inline void PathInfo::SetPath(const TCHAR *sPath)
41 {
42         m_sPath = sPath;
43 }
44
45 inline void PathInfo::SetPath(const String & sPath)
46 {
47         m_sPath = sPath;
48 }
49
50 /**
51  * @brief Holds path information of compared files/directories.
52  */
53 class PathContext
54 {
55 public:
56         typedef PathContextIterator const_iterator;
57
58         PathContext();
59         explicit PathContext(const String& sLeft);
60         PathContext(const String& sLeft, const String& sRight);
61         PathContext(const String& sLeft, const String& sMiddle, const String& sRight);
62         PathContext(const PathContext &paths);
63         explicit PathContext(const std::vector<String>& paths);
64
65         String GetAt(int nIndex) const;
66         String& GetElement(int nIndex);
67         void SetAt(int nIndex, const String& newElement);
68         String operator[](int nIndex) const { return GetAt(nIndex); }
69         String& operator[](int nIndex) { return GetElement(nIndex); }
70
71         String GetLeft(bool bNormalized = true) const;
72         String GetRight(bool bNormalized = true) const;
73         String GetMiddle(bool bNormalized = true) const;
74         String GetPath(int index, bool bNormalized = true) const;
75         void SetLeft(const String& path, bool bNormalized = true);
76         void SetRight(const String& path, bool bNormalized = true);
77         void SetMiddle(const String& path, bool bNormalized = true);
78         void SetPath(int index, const String& path, bool bNormalized = true);
79         void SetSize(int nFiles);
80         int GetSize() const;
81         void RemoveAll();
82         void Swap(int nFromIndex, int nToIndex);
83
84         const_iterator begin() const;
85         const_iterator end() const;
86 private:
87         int m_nFiles;
88         PathInfo m_path[3]; /**< First, second, third path (left path at start) */
89 };
90
91 /**
92  * @brief set number of files.
93  */
94 inline void PathContext::SetSize(int nFiles)
95 {
96         m_nFiles = nFiles;
97 }
98
99 /**
100  * @brief Return number of files.
101  */
102 inline int PathContext::GetSize() const
103 {
104         return m_nFiles;
105 }
106
107 class PathContextIterator : public std::iterator<std::forward_iterator_tag, String>
108 {
109 public:
110         explicit PathContextIterator(const PathContext *pPathContext) : m_pPathContext(pPathContext)
111         {
112                 m_sel =  (pPathContext->GetSize() == 0) ? -1 : 0;
113         }
114
115         PathContextIterator() : m_pPathContext(nullptr), m_sel(-1)
116         {
117         }
118
119         ~PathContextIterator() {}
120
121         PathContextIterator& operator=(const PathContextIterator& it)
122         {
123                 m_sel = it.m_sel;
124                 m_pPathContext = it.m_pPathContext;
125                 return *this;
126         }
127
128         PathContextIterator& operator++()
129         {
130                 m_sel++;
131                 if (m_sel >= m_pPathContext->GetSize())
132                         m_sel = -1;
133                 return *this;
134         }
135
136         String operator*()
137         {
138                 return m_pPathContext->GetAt(m_sel);
139         }
140
141         bool operator==(const PathContextIterator& it) const
142         {
143                 return m_sel == it.m_sel;
144         }
145
146         bool operator!=(const PathContextIterator& it) const
147         {
148                 return m_sel != it.m_sel;
149         }
150
151 public:
152         const PathContext *m_pPathContext;
153         int m_sel;
154 };
155
156 inline PathContextIterator PathContext::begin() const
157 {
158         return PathContextIterator(this);
159 }
160
161 inline PathContextIterator PathContext::end() const
162 {
163         return PathContextIterator();
164 }