libcommonc++  0.7
Permissions.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Permissions_hxx
24 #define __ccxx_Permissions_hxx
25 
26 #include <commonc++/Common.h++>
27 #include <commonc++/String.h++>
28 
29 namespace ccxx {
30 
39 {
40  public:
41 
47  Permissions(uint_t mask = 0);
48 
50  Permissions(const Permissions& other);
51 
53  ~Permissions();
54 
56  static const int USER_READ;
57 
59  static const int USER_WRITE;
60 
62  static const int USER_READ_WRITE;
63 
65  static const int USER_EXECUTE;
66 
68  static const int USER_ALL;
69 
71  static const int GROUP_READ;
72 
74  static const int GROUP_WRITE;
75 
77  static const int GROUP_READ_WRITE;
78 
80  static const int GROUP_EXECUTE;
81 
83  static const int GROUP_ALL;
84 
86  static const int OTHERS_READ;
87 
89  static const int OTHERS_WRITE;
90 
92  static const int OTHERS_READ_WRITE;
93 
95  static const int OTHERS_EXECUTE;
96 
98  static const int OTHERS_ALL;
99 
101  static const int ALL_READ_WRITE;
102 
104  static const int ALLBITS;
105 
106  // user
107 
109  inline bool canUserRead() const
110  { return((_mask & USER_READ) != 0); }
111 
113  inline void setUserRead()
114  { _mask |= USER_READ; }
115 
117  inline void clearUserRead()
118  { _mask &= ~USER_READ; }
119 
121  inline bool canUserWrite() const
122  { return((_mask & USER_WRITE) != 0); }
123 
125  inline void setUserWrite()
126  { _mask |= USER_WRITE; }
127 
129  inline void clearUserWrite()
130  { _mask &= ~USER_WRITE; }
131 
133  inline bool canUserExecute() const
134  { return((_mask & USER_EXECUTE) != 0); }
135 
137  inline void setUserExecute()
138  { _mask |= USER_EXECUTE; }
139 
141  inline void clearUserExecute()
142  { _mask &= ~USER_EXECUTE; }
143 
144  // group
145 
147  inline bool canGroupRead() const
148  { return((_mask & GROUP_READ) != 0); }
149 
151  inline void setGroupRead()
152  { _mask |= GROUP_READ; }
153 
155  inline void clearGroupRead()
156  { _mask &= ~GROUP_READ; }
157 
159  inline bool canGroupWrite() const
160  { return((_mask & GROUP_WRITE) != 0); }
161 
163  inline void setGroupWrite()
164  { _mask |= GROUP_WRITE; }
165 
167  inline void clearGroupWrite()
168  { _mask &= ~GROUP_WRITE; }
169 
171  inline bool canGroupExecute() const
172  { return((_mask & GROUP_EXECUTE) != 0); }
173 
175  inline void setGroupExecute()
176  { _mask |= GROUP_EXECUTE; }
177 
179  inline void clearGroupExecute()
180  { _mask &= ~GROUP_EXECUTE; }
181 
182  // others
183 
185  inline bool canOthersRead() const
186  { return((_mask & OTHERS_READ) != 0); }
187 
189  inline void setOthersRead()
190  { _mask |= OTHERS_READ; }
191 
193  inline void clearOthersRead()
194  { _mask &= ~OTHERS_READ; }
195 
197  inline bool canOthersWrite() const
198  { return((_mask & OTHERS_WRITE) != 0); }
199 
201  inline void setOthersWrite()
202  { _mask |= OTHERS_WRITE; }
203 
205  inline void clearOthersWrite()
206  { _mask &= ~OTHERS_WRITE; }
207 
209  inline bool canOthersExecute() const
210  { return((_mask & OTHERS_EXECUTE) != 0); }
211 
213  inline void setOthersExecute()
214  { _mask |= OTHERS_EXECUTE; }
215 
217  inline void clearOthersExecute()
218  { _mask &= ~OTHERS_EXECUTE; }
219 
221  inline void clearAll()
222  { _mask = 0; }
223 
224  // operators
225 
227  Permissions& operator=(const Permissions& other);
228 
230  inline uint_t getMask() const
231  { return(_mask); }
232 
234  inline operator uint_t()
235  { return(_mask); }
236 
238  String toString() const;
239 
240  inline uint_t operator=(uint_t rhs)
241  { return((_mask = rhs)); }
242 
244  { return(_mask |= (rhs & ALLBITS)); }
245 
246  inline uint_t operator&=(uint_t rhs)
247  { return(_mask &= (rhs & ALLBITS)); }
248 
251 
257 
258  private:
259 
260  uint_t _mask;
261 };
262 
263 inline uint_t operator|(const Permissions& perm, const uint_t rhs)
264 { return(perm.getMask() | (rhs & Permissions::ALLBITS)); }
265 
266 inline uint_t operator&(const Permissions& perm, const uint_t rhs)
267 { return(perm.getMask() & (rhs & Permissions::ALLBITS)); }
268 
269 } // namespace ccxx
270 
271 #endif // __ccxx_Permissions_hxx
uint_t operator|=(uint_t rhs)
Definition: Permissions.h++:243
uint_t getMask() const
Get the permissions mask.
Definition: Permissions.h++:230
static const int USER_READ
User read mask.
Definition: Permissions.h++:56
static const int GROUP_WRITE
Group write mask.
Definition: Permissions.h++:74
static const int ALLBITS
Complete permissions mask.
Definition: Permissions.h++:104
static const int GROUP_ALL
Group (all permissions) mask.
Definition: Permissions.h++:83
static const int OTHERS_READ
Others read mask.
Definition: Permissions.h++:86
bool canOthersExecute() const
Test for others execute access.
Definition: Permissions.h++:209
bool canOthersRead() const
Test for others read access.
Definition: Permissions.h++:185
void clearOthersWrite()
Disable others write access.
Definition: Permissions.h++:205
void clearGroupExecute()
Disable group execute access.
Definition: Permissions.h++:179
void setUserExecute()
Enable user execute access.
Definition: Permissions.h++:137
void setOthersExecute()
Enable others execute access.
Definition: Permissions.h++:213
void clearUserRead()
Disable user read access.
Definition: Permissions.h++:117
static const int GROUP_READ_WRITE
Group read and write mask.
Definition: Permissions.h++:77
void setGroupWrite()
Enable group write access.
Definition: Permissions.h++:163
bool canUserWrite() const
Test for user write access.
Definition: Permissions.h++:121
uint_t operator=(uint_t rhs)
Definition: Permissions.h++:240
static const Permissions defaultFilePerms
Default permissions for files: read & write for user, read for group.
Definition: Permissions.h++:250
static const int OTHERS_READ_WRITE
Others read and write mask.
Definition: Permissions.h++:92
static const int ALL_READ_WRITE
All read and write mask.
Definition: Permissions.h++:101
uint_t operator &(const Permissions &perm, const uint_t rhs)
Definition: Permissions.h++:266
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
static const int GROUP_EXECUTE
Group execute mask.
Definition: Permissions.h++:80
void setOthersWrite()
Enable others write access.
Definition: Permissions.h++:201
void setGroupRead()
Enable group read access.
Definition: Permissions.h++:151
static const int GROUP_READ
Group read mask.
Definition: Permissions.h++:71
void clearAll()
Disable all access.
Definition: Permissions.h++:221
bool canUserExecute() const
Test for user execute access.
Definition: Permissions.h++:133
void setOthersRead()
Enable others read access.
Definition: Permissions.h++:189
void setGroupExecute()
Enable group execute access.
Definition: Permissions.h++:175
uint_t operator|(const Permissions &perm, const uint_t rhs)
Definition: Permissions.h++:263
void clearUserExecute()
Disable user execute access.
Definition: Permissions.h++:141
bool canGroupRead() const
Test for group read access.
Definition: Permissions.h++:147
static const int OTHERS_WRITE
Others write mask.
Definition: Permissions.h++:89
void clearGroupRead()
Disable group read access.
Definition: Permissions.h++:155
static const int USER_WRITE
User write mask.
Definition: Permissions.h++:59
static const Permissions defaultDirPerms
Default permissions for directories: read, write & execute for user, read & execute for group...
Definition: Permissions.h++:256
void setUserRead()
Enable user read access.
Definition: Permissions.h++:113
bool canUserRead() const
Test for user read access.
Definition: Permissions.h++:109
#define COMMONCPP_API
Definition: Common.h++:126
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
static const int OTHERS_ALL
Others (all permissions) mask.
Definition: Permissions.h++:98
static const int USER_EXECUTE
User execute mask.
Definition: Permissions.h++:65
bool canGroupExecute() const
Test for group execute access.
Definition: Permissions.h++:171
bool canOthersWrite() const
Test for others write access.
Definition: Permissions.h++:197
void setUserWrite()
Enable user write access.
Definition: Permissions.h++:125
bool canGroupWrite() const
Test for group write access.
Definition: Permissions.h++:159
File permissions.
Definition: Permissions.h++:38
void clearGroupWrite()
Disable group write access.
Definition: Permissions.h++:167
static const int USER_ALL
User (all permissions) mask.
Definition: Permissions.h++:68
void clearOthersRead()
Disable others read access.
Definition: Permissions.h++:193
void clearOthersExecute()
Disable others execute access.
Definition: Permissions.h++:217
static const int USER_READ_WRITE
User read and write mask.
Definition: Permissions.h++:62
Definition: AllocationMap.c++:25
static const int OTHERS_EXECUTE
Others execute mask.
Definition: Permissions.h++:95
void clearUserWrite()
Disable user write access.
Definition: Permissions.h++:129