Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /** | 1 /** |
2 * \file process.h | 2 * \file process.h |
3 */ | 3 */ |
4 | 4 |
5 #ifndef PROCESS_H | 5 #ifndef PROCESS_H |
6 #define PROCESS_H | 6 #define PROCESS_H |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 #include <set> | 9 #include <set> |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 * \par Implementation | 75 * \par Implementation |
76 * Add "= delete" for C++11. | 76 * Add "= delete" for C++11. |
77 */ | 77 */ |
78 Windows_Handle operator=( const Windows_Handle & ) ; | 78 Windows_Handle operator=( const Windows_Handle & ) ; |
79 | 79 |
80 }; | 80 }; |
81 | 81 |
82 //------------------------------------------------------- | 82 //------------------------------------------------------- |
83 // file_name_set: case-insensitive wide-string set | 83 // file_name_set: case-insensitive wide-string set |
84 //------------------------------------------------------- | 84 //------------------------------------------------------- |
85 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) ; | 85 int wcscmpi( const wchar_t * s1, const wchar_t * s2 ) ; |
Wladimir Palant
2014/03/28 07:30:17
I realize that this isn't your code but still - wh
Eric
2014/03/28 12:06:00
It's mine.
Oleksandr
2014/03/28 12:48:35
That's in TODO: http://codereview.adblockplus.org/
Wladimir Palant
2014/03/28 13:55:24
Using an existing comparison string is one part of
| |
86 | 86 |
87 struct file_name | 87 struct file_name |
88 { | 88 { |
89 /** | 89 /** |
90 * Pointer to wide-character string, which is supposed to be null-terminated. | 90 * Pointer to wide-character string, which is supposed to be null-terminated. |
91 */ | 91 */ |
92 const wchar_t * name ; | 92 const wchar_t * name ; |
93 | 93 |
94 file_name( const wchar_t * name ) : name( name ) {} ; | 94 file_name( const wchar_t * name ) : name( name ) {} ; |
95 } ; | 95 } ; |
96 | 96 |
97 template <> | 97 template <> |
98 struct std::less< file_name > | 98 struct std::less< file_name > |
99 : std::binary_function< file_name, file_name, bool > | 99 : std::binary_function< file_name, file_name, bool > |
100 { | 100 { |
101 bool operator()( const file_name & a, const file_name & b ) const | 101 bool operator()( const file_name & a, const file_name & b ) const |
102 { | 102 { |
103 return wcscmpi( a.name, b.name ) < 0 ; | 103 return wcscmpi( a.name, b.name ) < 0 ; |
104 } | 104 } |
105 } ; | 105 } ; |
106 | 106 |
107 struct file_name_set | 107 struct file_name_set |
108 : public std::set< file_name > | 108 : public std::set< file_name > |
109 { | 109 { |
110 file_name_set(){}; | |
111 | |
110 file_name_set( const wchar_t * file_name_list[], size_t n_file_names ) | 112 file_name_set( const wchar_t * file_name_list[], size_t n_file_names ) |
111 { | 113 { |
112 for ( unsigned int j = 0 ; j < n_file_names ; ++ j ) | 114 for ( unsigned int j = 0 ; j < n_file_names ; ++ j ) |
113 { | 115 { |
114 insert( file_name( file_name_list[ j ] ) ) ; | 116 insert( file_name( file_name_list[ j ] ) ) ; |
115 } | 117 } |
116 } | 118 } |
117 } ; | 119 } ; |
118 | 120 |
119 //------------------------------------------------------- | 121 //------------------------------------------------------- |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 Snapshot() ; | 261 Snapshot() ; |
260 | 262 |
261 /** | 263 /** |
262 * Reconstruct the current instance with a new system snapshot. | 264 * Reconstruct the current instance with a new system snapshot. |
263 */ | 265 */ |
264 void refresh() ; | 266 void refresh() ; |
265 | 267 |
266 /** | 268 /** |
267 * Return a pointer to the first process in the snapshot. | 269 * Return a pointer to the first process in the snapshot. |
268 */ | 270 */ |
269 PROCESSENTRY32W * begin() ; | 271 PROCESSENTRY32W * first() ; |
270 | |
271 /** | |
272 * The end pointer is an alias for the null pointer. | |
273 */ | |
274 inline PROCESSENTRY32W * end() const { return 0 ; } | |
275 | 272 |
276 /** | 273 /** |
277 * Return a pointer to the next process in the snapshot. | 274 * Return a pointer to the next process in the snapshot. |
278 * begin() must have been called first. | 275 * begin() must have been called first. |
279 */ | 276 */ |
280 PROCESSENTRY32W * next() ; | 277 PROCESSENTRY32W * next() ; |
281 | |
282 /** | |
283 * Type definition for pointer to underlying structure. | |
284 */ | |
285 typedef PROCESSENTRY32W * Pointer ; | |
286 } ; | 278 } ; |
287 | 279 |
288 class ModulesSnapshot | 280 class ModulesSnapshot |
289 { | 281 { |
290 /** | 282 /** |
291 * Handle to the process snapshot. | 283 * Handle to the process snapshot. |
292 */ | 284 */ |
293 Windows_Handle handle ; | 285 Windows_Handle handle; |
Wladimir Palant
2014/03/28 07:30:17
Style nit: no space before the semicolon please (a
| |
294 | 286 |
295 /** | 287 /** |
296 * Buffer for reading a single module entry out of the snapshot. | 288 * Buffer for reading a single module entry out of the snapshot. |
297 */ | 289 */ |
298 MODULEENTRY32W module; | 290 MODULEENTRY32W module; |
299 | 291 |
300 /** | 292 /** |
301 * Copy constructor declared private and not defined. | 293 * Copy constructor declared private and not defined. |
302 * | 294 * |
303 * \par Implementation | 295 * \par Implementation |
304 * Add "= delete" for C++11. | 296 * Add "= delete" for C++11. |
305 */ | 297 */ |
306 ModulesSnapshot( const ModulesSnapshot & ) ; | 298 ModulesSnapshot(const ModulesSnapshot&); |
307 | 299 |
308 /** | 300 /** |
309 * Copy assignment declared private and not defined. | 301 * Copy assignment declared private and not defined. |
310 * | 302 * |
311 * \par Implementation | 303 * \par Implementation |
312 * Add "= delete" for C++11. | 304 * Add "= delete" for C++11. |
313 */ | 305 */ |
314 ModulesSnapshot operator=( const ModulesSnapshot & ) ; | 306 ModulesSnapshot operator=(const ModulesSnapshot&); |
315 | 307 |
316 | 308 |
317 public: | 309 public: |
318 /** | 310 /** |
319 * Default constructor takes the snapshot. | 311 * Default constructor takes the snapshot. |
320 */ | 312 */ |
321 ModulesSnapshot(DWORD processId) ; | 313 ModulesSnapshot(DWORD processId); |
322 | 314 |
323 /** | 315 /** |
324 * Return a pointer to the first process in the snapshot. | 316 * Return a pointer to the first process in the snapshot. |
325 */ | 317 */ |
326 MODULEENTRY32W * begin() ; | 318 MODULEENTRY32W* first(); |
327 | |
328 /** | |
329 * The end pointer is an alias for the null pointer. | |
330 */ | |
331 inline MODULEENTRY32W * end() const { return 0 ; } | |
332 | 319 |
333 /** | 320 /** |
334 * Return a pointer to the next process in the snapshot. | 321 * Return a pointer to the next process in the snapshot. |
335 * begin() must have been called first. | 322 * begin() must have been called first. |
336 */ | 323 */ |
337 MODULEENTRY32W * next() ; | 324 MODULEENTRY32W* next(); |
338 | 325 }; |
339 /** | |
340 * Type definition for pointer to underlying structure. | |
341 */ | |
342 typedef MODULEENTRY32W * Pointer ; | |
Wladimir Palant
2014/03/28 07:30:17
What is that type good for? Faking a generic itera
Oleksandr
2014/03/28 12:48:35
Yes.
On 2014/03/28 07:30:17, Wladimir Palant wrote
| |
343 } ; | |
344 | 326 |
345 | 327 |
346 //------------------------------------------------------- | 328 //------------------------------------------------------- |
347 // initialize_process_list | 329 // initialize_process_list |
348 //------------------------------------------------------- | 330 //------------------------------------------------------- |
349 /** | 331 /** |
350 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 332 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. |
351 * \tparam Admittance Function type for argument 'admit' | 333 * \tparam Admittance Function type for argument 'admit' |
352 * \tparam Extractor Function type for argument 'extract' | 334 * \tparam Extractor Function type for argument 'extract' |
353 * \param admit A unary predicate function class that determines what's included | 335 * \param admit A unary predicate function class that determines what's included |
354 * A process appears in the list only if the predicate returns true. | 336 * A process appears in the list only if the predicate returns true. |
355 * The use of this predicate is analogous to that in std::copy_if. | 337 * The use of this predicate is analogous to that in std::copy_if. |
356 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. | 338 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. |
357 */ | 339 */ |
358 template< class T, class Admittance, class Extractor > | 340 template<class T, class Admittance, class Extractor> |
359 void initialize_process_list( std::vector< T > & v, Snapshot & snap, Admittance admit = Admittance(), Extractor extract = Extractor() ) | 341 void initialize_process_list(std::vector<T>& v, Snapshot& snap, Admittance admit = Admittance(), Extractor extract = Extractor()) |
360 { | 342 { |
361 Snapshot::Pointer p = snap.begin() ; | 343 PROCESSENTRY32W* p = snap.first(); |
362 while ( p != snap.end() ) | 344 while (p != NULL) |
363 { | 345 { |
364 if ( admit( * p ) ) | 346 if (admit(*p )) |
365 { | 347 { |
366 /* | 348 /* |
367 * We don't have C++11 emplace_back, which can construct the element in p lace. | 349 * We don't have C++11 emplace_back, which can construct the element in p lace. |
368 * Instead, we copy the return value of the converter. | 350 * Instead, we copy the return value of the converter. |
369 */ | 351 */ |
370 v.push_back( extract( * p ) ); | 352 v.push_back(extract(*p)); |
371 } | 353 } |
372 p = snap.next() ; | 354 p = snap.next(); |
373 } | 355 } |
374 } ; | 356 }; |
375 | 357 |
376 //------------------------------------------------------- | 358 //------------------------------------------------------- |
377 // initialize_process_set | 359 // initialize_process_set |
378 //------------------------------------------------------- | 360 //------------------------------------------------------- |
379 /** | 361 /** |
380 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. | 362 * \tparam T The type into which a PROCESSENTRY32W struture is extracted. |
381 * \tparam Admittance Function type for argument 'admit' | 363 * \tparam Admittance Function type for argument 'admit' |
382 * \tparam Extractor Function type for argument 'extract' | 364 * \tparam Extractor Function type for argument 'extract' |
383 * \param admit A unary predicate function class that determines what's included | 365 * \param admit A unary predicate function class that determines what's included |
384 * A process appears in the list only if the predicate returns true. | 366 * A process appears in the list only if the predicate returns true. |
385 * The use of this predicate is analogous to that in std::copy_if. | 367 * The use of this predicate is analogous to that in std::copy_if. |
386 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. | 368 * \param convert A conversion function that takes a PROCESSENTRY32W as input ar gument and returns an element of type T. |
387 */ | 369 */ |
388 template< class T, class Admittance, class Extractor > | 370 template<class T, class Admittance, class Extractor> |
389 void initialize_process_set( std::set< T > & set, Snapshot & snap, Admittance ad mit = Admittance(), Extractor extract = Extractor() ) | 371 void initialize_process_set(std::set< T > & set, Snapshot& snap, Admittance admi t = Admittance(), Extractor extract = Extractor()) |
390 { | 372 { |
391 Snapshot::Pointer p = snap.begin() ; | 373 PROCESSENTRY32W* p = snap.first(); |
392 while ( p != snap.end() ) | 374 while (p != NULL) |
393 { | 375 { |
394 if ( admit( * p ) ) | 376 if (admit(*p)) |
395 { | 377 { |
396 set.insert( extract( * p ) ); | 378 set.insert(extract(*p)); |
397 } | 379 } |
398 p = snap.next() ; | 380 p = snap.next(); |
399 } | 381 } |
400 } ; | 382 }; |
401 | 383 |
402 //------------------------------------------------------- | 384 //------------------------------------------------------- |
403 // enumerate_windows | 385 // enumerate_windows |
404 //------------------------------------------------------- | 386 //------------------------------------------------------- |
405 | 387 |
406 /** | 388 /** |
407 * States of a window enumeration. | 389 * States of a window enumeration. |
408 */ | 390 */ |
409 typedef enum | 391 typedef enum |
410 { | 392 { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
519 */ | 501 */ |
520 std::set< DWORD > pid_set ; | 502 std::set< DWORD > pid_set ; |
521 | 503 |
522 /** | 504 /** |
523 * Set of executable names by which to filter. | 505 * Set of executable names by which to filter. |
524 * | 506 * |
525 * The argument of the filter constructor is a set by reference. | 507 * The argument of the filter constructor is a set by reference. |
526 * Since it does not make a copy for itself, we define it as a class member to provide its allocation. | 508 * Since it does not make a copy for itself, we define it as a class member to provide its allocation. |
527 */ | 509 */ |
528 file_name_set file_names ; | 510 file_name_set file_names ; |
511 /** | |
512 * Set of module (DLL) names by which to filter. | |
513 */ | |
529 file_name_set module_names ; | 514 file_name_set module_names ; |
Wladimir Palant
2014/03/28 07:30:17
Nit: The comment no longer applies here, I guess m
Eric
2014/03/28 12:06:00
Yes. The comment beginning /** is a Doxygen commen
| |
530 | |
531 /** | |
532 * Filter function object matches on any of the exe names specified in the con structor. | |
533 */ | |
534 // process_by_any_file_name_CI filter ; | |
Wladimir Palant
2014/03/28 07:30:17
Please remove this line instead of commenting it.
| |
535 | 515 |
536 process_by_any_exe_with_any_module filter ; | 516 process_by_any_exe_with_any_module filter ; |
537 | 517 |
538 /** | 518 /** |
539 * Copy function object copies just the process ID. | 519 * Copy function object copies just the process ID. |
540 */ | 520 */ |
541 copy_PID copy ; | 521 copy_PID copy ; |
542 | 522 |
543 /** | 523 /** |
544 * Snapshot of running processes. | 524 * Snapshot of running processes. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 if ( ! b ) | 557 if ( ! b ) |
578 { | 558 { |
579 // Assert the process that created the window is not in our pid_set | 559 // Assert the process that created the window is not in our pid_set |
580 return true ; | 560 return true ; |
581 } | 561 } |
582 return f( window ) ; | 562 return f( window ) ; |
583 } | 563 } |
584 } ; | 564 } ; |
585 | 565 |
586 public: | 566 public: |
587 Process_Closer( Snapshot & snapshot, const wchar_t * file_name_list[], size_t n_file_names, const wchar_t * module_name_list[], size_t n_module_names ) | 567 template <size_t n_file_names, size_t n_module_names> |
588 : snapshot( snapshot ), file_names( file_name_list, n_file_names ), module_n ames(module_name_list, n_module_names), filter( file_names, module_names ) | 568 Process_Closer(Snapshot & snapshot, const wchar_t* (&file_name_list)[n_file_na mes], const wchar_t* (&module_name_list)[n_module_names]) |
Wladimir Palant
2014/03/28 07:30:17
With this signature getting very complex, it is pr
Oleksandr
2014/03/28 12:48:35
This won't work since we need to be able to pass a
Eric
2014/03/28 13:15:01
It should work for that. Zero is a valid template
Oleksandr
2014/03/28 13:49:27
Hm. Care to elaborate on this? The C++ specs seems
Eric
2014/03/28 15:13:19
Oops. I was wrong about that; you're right. The ti
| |
569 : snapshot(snapshot), file_names(file_name_list, n_file_names), module_names (module_name_list, n_module_names), filter(file_names, module_names) | |
570 { | |
571 update() ; | |
572 } | |
573 template <size_t n_file_names> | |
574 Process_Closer(Snapshot & snapshot, const wchar_t * (&file_name_list)[n_file_n ames]) | |
575 : snapshot(snapshot), file_names(file_name_list, n_file_names), module_names (), filter(file_names, module_names) | |
589 { | 576 { |
590 update() ; | 577 update() ; |
591 } | 578 } |
592 | 579 |
593 /** | 580 /** |
594 * Refresh our state to match the snapshot state. | 581 * Refresh our state to match the snapshot state. |
595 */ | 582 */ |
596 void refresh() | 583 void refresh() |
597 { | 584 { |
598 pid_set.clear() ; | 585 pid_set.clear() ; |
(...skipping 12 matching lines...) Expand all Loading... | |
611 } | 598 } |
612 | 599 |
613 /* | 600 /* |
614 * Shut down every process in the pid_set. | 601 * Shut down every process in the pid_set. |
615 */ | 602 */ |
616 bool shut_down() ; | 603 bool shut_down() ; |
617 | 604 |
618 } ; | 605 } ; |
619 | 606 |
620 #endif | 607 #endif |
LEFT | RIGHT |