what happens when calling KDirWatch::addDir() multiple times with the same path?

David Faure faure at kde.org
Sat Dec 2 19:50:01 UTC 2017


On samedi 2 décembre 2017 18:08:44 CET René J.V. Bertin wrote:
> On Saturday December 02 2017 17:36:03 David Faure wrote:
> > The goal of what? That categorized and off-by-default debug message?
> 
> No, why the code would be doing this:

"This" what?

> > So this is reference counted. Nice, heh?
> 
> Sure, nifty. I thought it was doing something like that, but why would you
> want to reference-count this kind of feature?

So that two pieces of code that don't know each other (e.g. KDirModel and 
KDevelop's own code...) don't interfere with each other.
They can both register/unregister for the same dir based on what they need, 
without removeDir from one code breaking the other code that still needs to 
watch the dir.

> It seems that a simple off/on
> kind of reference counting (none, one-or-more if you prefer) should be
> enough here. Or maybe I should ask: why is there no way to remove a watch
> on a file completely? 

Just let both pieces of code call removeDir.

> I can think of a scenario where multiple things in an
> app would be watching a given file so you wouldn't want to release the
> watch until the last "thing" releases its watch, unless someone deletes the
> file.

That's exactly what the refcounting is about.

> > > As a result I feel I need to wrap each call to KDW::addDir(foo) with a
> > > check of KDW::contains(foo).
> > 
> > No you don't need to do that.
> 
> Let me be a bit more specific. Of course I don't need to do this if all I
> want is to set a watch. But if watches are ref-counted that suggests at
> least that you have to remove an item as many times as you've added it to
> the watcher.

Yes. That's called writing symmetric code ;)

> So if I want to be able to remove watches without having to keep
> track of how many times I set one, I do need those wrappers.  In my case
> watches are set by a single function which loads directories and can thus
> be called lots of times 

Doesn't that function know that it already loaded that directory?
I don't know what wrapper you have in mind.
But yeah if the function has no memory then using KDirWatch's memory ( with 
contains()) is a possibility. It just seems awfully weird to me that the app 
wouldn't remember which dirs it loaded already...

> but they're basically unset only when directories
> are deleted (which usually happens only once ;)).

A directory being deleted will inform KDirWatch which will mark it as 
NonExistent. It will notice if the directory gets created, but apart from that 
I don't expect trouble. Why removeDir() at all, then?

-- 
David Faure, faure at kde.org, http://www.davidfaure.fr
Working on KDE Frameworks 5





More information about the Kde-frameworks-devel mailing list