<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yash Hayaran</title>
    <description>The latest articles on DEV Community by Yash Hayaran (@yashhayaran).</description>
    <link>https://dev.to/yashhayaran</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F210205%2Fdb8a7dc4-f88f-468b-a35f-c75fe756aeb7.jpeg</url>
      <title>DEV Community: Yash Hayaran</title>
      <link>https://dev.to/yashhayaran</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yashhayaran"/>
    <language>en</language>
    <item>
      <title>Design folder change monitor in C++ (Windows)</title>
      <dc:creator>Yash Hayaran</dc:creator>
      <pubDate>Tue, 03 May 2022 16:10:03 +0000</pubDate>
      <link>https://dev.to/yashhayaran/design-folder-change-monitor-in-c-windows-2g97</link>
      <guid>https://dev.to/yashhayaran/design-folder-change-monitor-in-c-windows-2g97</guid>
      <description>&lt;h2&gt;
  
  
  What's a hot folder?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Hot folder&lt;/strong&gt; is like any other folder. Makes it &lt;strong&gt;hot&lt;/strong&gt; is when a program (which we gonna code in C++) monitors the change. &lt;/p&gt;

&lt;h2&gt;
  
  
  Who uses it?
&lt;/h2&gt;

&lt;p&gt;Software input can be file. File's source could be hardware like camera generating images or third-party software. Module watches a folder for change, to consume the input. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to design?
&lt;/h2&gt;

&lt;p&gt;Let's think! About what all we need:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We may heard about observer design pattern. Observers will get notified when a Subject updates. Observer design pattern fits well for this situation. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Watcher - Object responsible to receive events from OS about folder changes. Say watching the folder. Watcher is Subject, because Watcher behaves like subject, which gets update, and will notify the observers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update Listener - Object hold the list of changes received from Subject. Update Listener is Observer. Module listens to subject and adds the upcoming changes to thread safe collection. This is needed, why? Because assume a scenario where producer producing the N files per second, but when you don't have any listener like module then you may miss the events while processing the previous. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Handler - Object instantiate receiver and listener object respectively. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's start!
&lt;/h2&gt;

&lt;p&gt;First we define our two basic interfaces 'ISubject' and 'IObject'&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;template &amp;lt;typename T&amp;gt;
class ISubject
{
    public:
        ISubject()          = default;
        virtual ~ISubject() = default;
        virtual AddObserver(const IObserver&amp;lt;T&amp;gt;* pObserver) = 0;
        virtual RemoveObserver(const IObserver&amp;lt;T&amp;gt;* pObserver) = 0;
        virtual NotifyObservers(const T&amp;amp; data) = 0;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;template &amp;lt;typename T&amp;gt;
class IObserver
{
    public:
        IObserver()          = default;
        virtual ~IObserver() = default;
        virtual Uptdate(const T&amp;amp; data) = 0;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DirectoryWatcher : public ISubject
{
    protected:
        std::string _strDirectory;
        HANDLE      _pChangeHandle;

        bool IsHandleValid();
        void Reset(const std::string&amp;amp; strDirectory);

    public: 
        DirectoryWatcher();
        ~DirectoryWatcher();

        bool Start(const std::string&amp;amp; strDirectory);
        void Stop();
        void AddObserver(const IObserver&amp;lt;std::string&amp;gt;* pObserver);
        void RemoveObserver(const IObserver&amp;lt;std::string&amp;gt;* pObserver);
        void NotifyObservers(const T&amp;amp; data);
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UpdateListener : public IObserver
{
    protected:
        code_machina::BlockingCollection&amp;lt;std::string&amp;gt; _changeCollection;

    public: 
        UpdateListener();
        ~UpdateListener();

        bool Start(const std::string&amp;amp; strDirectory);
        void Stop();
        void AddObserver(const IObserver&amp;lt;std::string&amp;gt;* pObserver);
        void RemoveObserver(const IObserver&amp;lt;std::string&amp;gt;* pObserver);
        void NotifyObservers(const T&amp;amp; data);
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please comment for more info!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>tutorial</category>
      <category>design</category>
    </item>
  </channel>
</rss>
