-
Notifications
You must be signed in to change notification settings - Fork 57
Basic rules for algorithm development
gitbaum edited this page Jul 22, 2011
·
1 revision
When implementing a new algorithm, a few basic rules must be followed. These rules are basically described in the concept [wiki:design/concepts/basic_algorithm Basic Algorithm], and particularly consist of the following rules:
-
init()/destruct(): Each algorithm must offer an
init()
and adestruct()
method. The init is for re-setting the algorithm by re-initing all state variables, as if the algorithm has just been started. -
Pointers to facets: An algorithm is only allowed to hold pointers to facets - and not to construct a facet within the algorithm. Each facet provides the type
self_pointer_t
, which must be used to define the pointer to the facet. This must be done due to efficiency reasons (e.g., on systems where no instances to facets must be created, these pointers can be optimized away). -
init(parameters): If Os facets are used, an algorithm must also provide an
init()
method which gets references to the Os facets as parameter, and where these facets are assigned to the correspondingFacet::self_pointer_t
. - Return Values from Os Model: Each return value that corresponds to an error value must be taken from the Os model.
Example:
template<typename OsModel_P, typename Radio_P = typename OsModel_P::Radio, typename Timer_P = typename OsModel_P::Timer, typename Debug_P = typename OsModel_P::Debug> class MyAlgorithm { public: typedef OsModel_P OsModel; typedef Radio_P Radio; typedef Timer_P Timer; typedef Debug_P Debug; int init() { // ... return OsModel::SUCCESS; } int destruct() { // ... return OsModel::SUCCESS; } int init( Radio& radio, Timer& timer, Debug& debug ) { radio_ = &radio; timer_ = &timer; debug_ = &debug; return OsModel::SUCCESS; } private: typename Radio::self_pointer_t radio_; typename Timer::self_pointer_t timer_; typename Debug::self_pointer_t debug_; };