Skip to content

Commit

Permalink
framework: add direct_set/get for default_setter/getter
Browse files Browse the repository at this point in the history
Signed-off-by: Wataru Ishida <wataru.ishid@gmail.com>
  • Loading branch information
ishidawataru committed Apr 26, 2021
1 parent 94b079c commit 22e3bcd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
27 changes: 16 additions & 11 deletions tools/framework/config.hpp
Expand Up @@ -8,7 +8,6 @@
#include <vector>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <functional>
#include <mutex>

Expand Down Expand Up @@ -331,18 +330,24 @@ namespace tai::framework {
return m_config.size();
}

template<tai_object_type_t S>
friend std::ostream& operator<<(std::ostream& os, const Config<S> &config);
tai_status_t direct_set(S_Attribute src) {
return _set(src, false, false, nullptr, true);
}

const tai_attribute_value_t* direct_get(tai_attr_id_t id) {
return _get(id, false, true);
}

private:

const tai_attribute_value_t* _get(tai_attr_id_t id, bool no_default = false) const {
const tai_attribute_value_t* _get(tai_attr_id_t id, bool no_default = false, bool direct = false) const {
auto info = m_info.find(id);
if ( info == m_info.end() ) {
if ( !direct && info == m_info.end() ) {
return nullptr;
}
auto it = m_config.find(id);
if ( it == m_config.end() ) {
if ( no_default ) {
if ( no_default || direct ) {
return nullptr;
}
if ( info->second.defaultvalue != nullptr ) {
Expand All @@ -357,23 +362,23 @@ namespace tai::framework {
}

// readonly : if true, allow readonly attribute to be set
tai_status_t _set(S_Attribute src, bool readonly, bool without_hook, FSMState* fsm = nullptr) {
tai_status_t _set(S_Attribute src, bool readonly, bool without_hook, FSMState* fsm = nullptr, bool direct = false) {
auto info = m_info.find(src->id());
if ( info == m_info.end() ) {
if ( !direct && info == m_info.end() ) {
TAI_DEBUG("no meta: 0x%x", src->id());
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}

if ( !readonly && info->second.meta->isreadonly) {
if ( !direct && !readonly && info->second.meta->isreadonly) {
TAI_WARN("read only: 0x%x", src->id());
return TAI_STATUS_INVALID_ATTR_VALUE_0;
}

if ( fsm != nullptr && info->second.fsm != FSM_STATE_INIT ) {
if ( !direct && fsm != nullptr && info->second.fsm != FSM_STATE_INIT ) {
*fsm = info->second.fsm;
}

if ( !without_hook && info->second.setter != nullptr ) {
if ( !direct && !without_hook && info->second.setter != nullptr ) {
auto ret = info->second.setter(src->raw(), fsm, m_user);
if ( ret != TAI_STATUS_SUCCESS || info->second.no_store ) {
return ret;
Expand Down
15 changes: 9 additions & 6 deletions tools/framework/examples/basic/basic.hpp
Expand Up @@ -127,9 +127,13 @@ namespace tai::basic {
auto attribute = &attrs[i];
auto meta = tai_metadata_get_attr_metadata(T, attribute->id);
if ( meta == nullptr ) {
TAI_ERROR("no metadata for attribute 0x%x", attribute->id);
return info[i].status;
}
m_config[attribute->id] = std::make_shared<Attribute>(meta, *attribute);
auto ret = tai::framework::Object<T>::config().direct_set(std::make_shared<Attribute>(meta, *attribute));
if ( ret != TAI_STATUS_SUCCESS ) {
return convert_tai_error_to_list(ret, info[i].index);
}
}
return TAI_STATUS_SUCCESS;
}
Expand All @@ -138,9 +142,10 @@ namespace tai::basic {
for ( auto i = 0; i< count; i++ ) {
auto attribute = &attrs[i];
auto meta = tai_metadata_get_attr_metadata(T, attribute->id);
auto it = m_config.find(attribute->id);
if ( it != m_config.end() ) {
auto ret = tai_metadata_deepcopy_attr_value(meta, it->second->raw(), attribute);
auto value = tai::framework::Object<T>::config().direct_get(attribute->id);
if ( value != nullptr ) {
tai_attribute_t in = {attribute->id, *value};
auto ret = tai_metadata_deepcopy_attr_value(meta, &in, attribute);
if ( ret != TAI_STATUS_SUCCESS ) {
return info[i].status;
}
Expand All @@ -158,8 +163,6 @@ namespace tai::basic {
}
return TAI_STATUS_SUCCESS;
}

std::map<tai_attr_id_t, S_Attribute> m_config;
};

class Module : public Object<TAI_OBJECT_TYPE_MODULE> {
Expand Down
18 changes: 18 additions & 0 deletions tools/framework/examples/basic/custom/module.h
@@ -0,0 +1,18 @@
#ifndef __TAI_BASIC_MODULE__
#define __TAI_BASIC_MODULE__

#include <tai.h>

typedef enum _basic_module_attr_t
{
/**
* @brief Custom attribute example
*
* @type bool
* @flags CREATE_AND_SET
*/
TAI_MODULE_ATTR_CUSTOM = TAI_MODULE_ATTR_CUSTOM_RANGE_START,

} basic_module_attr_t;

#endif

0 comments on commit 22e3bcd

Please sign in to comment.