OSDN Git Service

refactor
[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() = default;
24
25         String GetPath(bool bNormalized = true) const;
26         String& GetRef() { return m_sPath; }
27         void SetPath(const tchar_t *path);
28         void SetPath(const String & path);
29         void NormalizePath();
30
31 private:
32         String m_sPath;  /**< Directory / file path */
33 };
34
35 /**
36  * @brief Set path.
37  * @param [in] sPath New path for item.
38  */
39 inline void PathInfo::SetPath(const tchar_t *sPath)
40 {
41         m_sPath = sPath;
42 }
43
44 inline void PathInfo::SetPath(const String & sPath)
45 {
46         m_sPath = sPath;
47 }
48
49 /**
50  * @brief Holds path information of compared files/directories.
51  */
52 class PathContext
53 {
54 public:
55         typedef PathContextIterator const_iterator;
56
57         PathContext();
58         explicit PathContext(const String& sLeft);
59         PathContext(const String& sLeft, const String& sRight);
60         PathContext(const String& sLeft, const String& sMiddle, const String& sRight);
61         explicit PathContext(const std::vector<String>& paths);
62
63         String GetAt(int nIndex) const;
64         String& GetElement(int nIndex);
65         void SetAt(int nIndex, const String& newElement);
66         String operator[](int nIndex) const { return GetAt(nIndex); }
67         String& operator[](int nIndex) { return GetElement(nIndex); }
68
69         String GetLeft(bool bNormalized = true) const;
70         String GetRight(bool bNormalized = true) const;
71         String GetMiddle(bool bNormalized = true) const;
72         String GetPath(int index, bool bNormalized = true) const;
73         void SetLeft(const String& path, bool bNormalized = true);
74         void SetRight(const String& path, bool bNormalized = true);
75         void SetMiddle(const String& path, bool bNormalized = true);
76         void SetPath(int index, const String& path, bool bNormalized = true);
77         void SetSize(int nFiles);
78         int GetSize() const;
79         void RemoveAll();
80         void Swap(int nFromIndex, int nToIndex);
81
82         const_iterator begin() const;
83         const_iterator end() const;
84 private:
85         int m_nFiles;
86         PathInfo m_path[3]; /**< First, second, third path (left path at start) */
87 };
88
89 /**
90  * @brief set number of files.
91  */
92 inline void PathContext::SetSize(int nFiles)
93 {
94         m_nFiles = nFiles;
95 }
96
97 /**
98  * @brief Return number of files.
99  */
100 inline int PathContext::GetSize() const
101 {
102         return m_nFiles;
103 }
104
105 class PathContextIterator : public std::iterator<std::forward_iterator_tag, String>
106 {
107 public:
108         explicit PathContextIterator(const PathContext *pPathContext) : m_pPathContext(pPathContext)
109         {
110                 m_sel =  (pPathContext->GetSize() == 0) ? -1 : 0;
111         }
112
113         PathContextIterator() : m_pPathContext(nullptr), m_sel(-1)
114         {
115         }
116
117         ~PathContextIterator() = default;
118
119         PathContextIterator& operator=(const PathContextIterator& it)
120         {
121                 m_sel = it.m_sel;
122                 m_pPathContext = it.m_pPathContext;
123                 return *this;
124         }
125
126         PathContextIterator& operator++()
127         {
128                 m_sel++;
129                 if (m_sel >= m_pPathContext->GetSize())
130                         m_sel = -1;
131                 return *this;
132         }
133
134         String operator*()
135         {
136                 return m_pPathContext->GetAt(m_sel);
137         }
138
139         bool operator==(const PathContextIterator& it) const
140         {
141                 return m_sel == it.m_sel;
142         }
143
144         bool operator!=(const PathContextIterator& it) const
145         {
146                 return m_sel != it.m_sel;
147         }
148
149 public:
150         const PathContext *m_pPathContext;
151         int m_sel;
152 };
153
154 inline PathContextIterator PathContext::begin() const
155 {
156         return PathContextIterator(this);
157 }
158
159 inline PathContextIterator PathContext::end() const
160 {
161         return PathContextIterator();
162 }