genode/repos/os/include/gpio/component.h
Norman Feske eba9c15746 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
2018-01-17 12:14:35 +01:00

152 lines
3.7 KiB
C++

/*
* \brief GPIO-session component
* \author Stefan Kalkowski
* \date 2013-05-03
*/
/*
* Copyright (C) 2011-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 _INCLUDE__GPIO__COMPONENT_H_
#define _INCLUDE__GPIO__COMPONENT_H_
#include <base/printf.h>
#include <root/component.h>
#include <gpio_session/gpio_session.h>
#include <gpio/driver.h>
namespace Gpio {
class Session_component;
class Root;
};
class Gpio::Session_component : public Genode::Rpc_object<Gpio::Session>
{
private:
struct Irq_session_component : public Genode::Rpc_object<Genode::Irq_session>
{
Driver &_driver;
unsigned long _pin;
Irq_session_component(Driver &driver, unsigned long pin)
: _driver(driver), _pin(pin) { }
/***************************
** Irq_session interface **
***************************/
void ack_irq() override { _driver.ack_irq(_pin); }
void sigh(Genode::Signal_context_capability sigh) override {
_driver.register_signal(_pin, sigh); }
Info info() override {
return { .type = Info::Type::INVALID, .address = 0, .value = 0 }; }
};
Genode::Rpc_entrypoint &_ep;
Driver &_driver;
unsigned long _pin;
Irq_session_component _irq_component;
Genode::Irq_session_capability _irq_cap;
public:
Session_component(Genode::Rpc_entrypoint &ep,
Driver &driver,
unsigned long gpio_pin)
: _ep(ep), _driver(driver), _pin(gpio_pin),
_irq_component(_driver, _pin),
_irq_cap(_ep.manage(&_irq_component)) { }
~Session_component() { _ep.dissolve(&_irq_component); }
/*****************************
** Gpio::Session interface **
*****************************/
void direction(Direction d) { _driver.direction(_pin, (d == IN)); }
void write(bool level) { _driver.write(_pin, level); }
bool read() { return _driver.read(_pin); }
void debouncing(unsigned int us)
{
if (us) {
_driver.debounce_time(_pin, us);
_driver.debounce_enable(_pin, true);
} else
_driver.debounce_enable(_pin, false);
}
Genode::Irq_session_capability irq_session(Irq_type type)
{
switch (type) {
case HIGH_LEVEL:
_driver.high_detect(_pin);
break;
case LOW_LEVEL:
_driver.low_detect(_pin);
break;
case RISING_EDGE:
_driver.rising_detect(_pin);
break;
case FALLING_EDGE:
_driver.falling_detect(_pin);
};
_driver.irq_enable(_pin, true);
return _irq_cap;
}
};
class Gpio::Root : public Genode::Root_component<Gpio::Session_component>
{
private:
Genode::Rpc_entrypoint &_ep;
Driver &_driver;
protected:
Session_component *_create_session(const char *args)
{
unsigned long pin =
Genode::Arg_string::find_arg(args, "gpio").ulong_value(0);
Genode::size_t ram_quota =
Genode::Arg_string::find_arg(args, "ram_quota").ulong_value(0);
if (!_driver.gpio_valid(pin))
throw Genode::Service_denied();
if (ram_quota < sizeof(Session_component)) {
Genode::warning("insufficient dontated ram_quota "
"(", ram_quota, " bytes), "
"require ", sizeof(Session_component), " bytes");
throw Genode::Insufficient_ram_quota();
}
return new (md_alloc()) Session_component(_ep, _driver, pin);
}
public:
Root(Genode::Rpc_entrypoint *session_ep,
Genode::Allocator *md_alloc, Driver &driver)
: Genode::Root_component<Gpio::Session_component>(session_ep, md_alloc),
_ep(*session_ep), _driver(driver) { }
};
#endif /* _INCLUDE__GPIO__COMPONENT_H_ */