genode/repos/os/include/file_system/open_node.h

117 lines
2.8 KiB
C
Raw Normal View History

/*
* \brief Representation of an open file system node within the component
* \author Christian Prochaska
* \date 2017-06-09
*/
/*
* Copyright (C) 2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _OPEN_NODE_H_
#define _OPEN_NODE_H_
/* Genode includes */
#include <file_system/listener.h>
#include <file_system_session/file_system_session.h>
namespace File_system {
/*
* \param NODE component-specific node type
*/
template <typename NODE> class Open_node;
}
template <typename NODE>
class File_system::Open_node : public File_system::Node
{
private:
Genode::Id_space<File_system::Node>::Element _element;
Genode::Weak_ptr<NODE> _node;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Genode::Constructible<File_system::Listener> _listener { };
Listener::Version const _version_when_opened { _node_version(_node) };
/*
* Flag to track whether the underlying file-system node was
* modified via this 'Open_node'. That is, if closing the 'Open_node'
* should notify listeners of the file.
*/
bool _was_written = false;
static Listener::Version const _node_version(Genode::Weak_ptr<NODE> node)
{
Genode::Locked_ptr<NODE> locked_node { node };
if (locked_node.valid())
return locked_node->curr_version();
else
return Listener::Version { 0 };
}
public:
Open_node(Genode::Weak_ptr<NODE> node,
Genode::Id_space<File_system::Node> &id_space)
: _element(*this, id_space), _node(node) { }
~Open_node()
{
Genode::Locked_ptr<NODE> node { _node };
if (_listener.constructed()) {
if (node.valid())
node->remove_listener(&*_listener);
_listener.destruct();
}
/*
* Notify remaining listeners about the changed file
*/
if (_was_written && node.valid()) {
node->notify_listeners();
}
}
Genode::Weak_ptr<NODE>&node() { return _node; }
File_system::Listener &listener() { return *_listener; }
Genode::Id_space<File_system::Node>::Id id() { return _element.id(); }
/**
* Register packet stream sink to be notified of node changes
*/
void register_notify(File_system::Sink &sink)
{
Genode::Locked_ptr<NODE> node { _node };
/*
* If there was already a handler registered for the node,
* remove the old handler.
*/
if (_listener.constructed()) {
if (node.valid())
node->remove_listener(&*_listener);
_listener.destruct();
}
/*
* Register new handler
*/
if (node.valid()) {
_listener.construct(sink, id(), _version_when_opened);
node->add_listener(&*_listener);
}
}
void mark_as_written() { _was_written = true; }
void unmark_as_written() { _was_written = false; }
};
#endif /* _OPEN_NODE_H_ */