FileMonitor.h
branchv_0
changeset 7 e6065118326f
child 10 8382173bfc35
equal deleted inserted replaced
6:fd93a46db15b 7:e6065118326f
       
     1 /**
       
     2  * ShaderShark
       
     3  * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #pragma once
       
    19 
       
    20 #include <string>
       
    21 #include <memory>
       
    22 
       
    23 class FileMonitor;
       
    24 
       
    25 class WatchedFile {
       
    26 public:
       
    27 	class Impl;
       
    28 	WatchedFile();
       
    29 	virtual ~WatchedFile();
       
    30 	std::string getFileName() const;
       
    31 	uint32_t getMask() const;
       
    32 	FileMonitor getFileMonitor() const;
       
    33 private:
       
    34 	std::shared_ptr<Impl> impl;
       
    35 	friend FileMonitor;
       
    36 };
       
    37 
       
    38 class FileEvent {
       
    39 public:
       
    40 	std::string fileName;
       
    41 	uint32_t mask;
       
    42 	// TODO: more information, see: man 7 inotify
       
    43 };
       
    44 
       
    45 class FileMonitor {
       
    46 public:
       
    47 	class Impl;
       
    48 	FileMonitor();
       
    49 	virtual ~FileMonitor();
       
    50 	/**
       
    51 	 * @return file descriptor that can be monitored by e.g. epoll
       
    52 	 */
       
    53 	int getFD() const;
       
    54 	/**
       
    55 	 * @param fileName file to be monitored
       
    56 	 * @param mask IN_CLOSE_WRITE and other IN_*
       
    57 	 * @return monitoring will continue until any copy of this object exists
       
    58 	 */
       
    59 	WatchedFile watch(const std::string& fileName);
       
    60 	WatchedFile watch(const std::string& fileName, uint32_t mask);
       
    61 	/**
       
    62 	 * @param event object to be filled with information
       
    63 	 * @return true if event was filled, call this method until false returned
       
    64 	 */
       
    65 	bool readEvent(FileEvent& event);
       
    66 private:
       
    67 	std::shared_ptr<Impl> impl;
       
    68 	FileMonitor(std::shared_ptr<Impl> impl);
       
    69 	friend WatchedFile;
       
    70 };