hanzz2007 commented on issue #6093:
URL: https://github.com/apache/incubator-tvm/issues/6093#issuecomment-662554484
Yes, move can solve. And we should add a default constructor and a rvref
constructor to AttrInitEntry
```c++
template <typename T>
struct AttrInitEntry {
// The attributes
using TSelf = AttrInitEntry<T>;
// The type key
const char* type_key_;
// field name
const char* key_;
// internal value.
T* value_;
// whether the value is missing.
bool value_missing_{true};
AttrInitEntry() = default;
AttrInitEntry(AttrInitEntry&& other) {
type_key_ = other.type_key_;
key_ = other.key_;
value_ = other.value_;
value_missing_ = other.value_missing_;
other.value_missing_ = false; // note! avoid throw
}
// If the value is still missing in destruction time throw an error.
~AttrInitEntry() DMLC_THROW_EXCEPTION {
if (value_missing_) {
std::ostringstream os;
os << type_key_ << ": Cannot find required field \'" << key_ << "\'
during initialization";
throw AttrError(os.str());
}
}
// override fields.
// This function sets the lower bound of the attribute
TSelf& set_lower_bound(DMLC_ATTRIBUTE_UNUSED const T& begin) {
if (this->value_missing_) return *this;
const T& val = *value_;
if (begin > val) {
std::ostringstream os;
os << type_key_ << "." << key_ << ": "
<< "value " << val << " is smaller than the lower bound " << begin;
throw AttrError(os.str());
}
return *this;
}
// This function sets the upper bound of the attribute
TSelf& set_upper_bound(DMLC_ATTRIBUTE_UNUSED const T& end) {
if (this->value_missing_) return *this;
const T& val = *value_;
if (val > end) {
std::ostringstream os;
os << type_key_ << "." << key_ << ": "
<< "value " << val << " is bigger than the upper bound " << end;
throw AttrError(os.str());
}
return *this;
}
// set default when
TSelf& set_default(DMLC_ATTRIBUTE_UNUSED const T& value) {
if (!value_missing_) return *this;
*value_ = value;
value_missing_ = false;
return *this;
}
TSelf& describe(DMLC_ATTRIBUTE_UNUSED const char* str) { return *this; }
};
template <typename FFind>
class AttrInitVisitor {
public:
// Counter of number of matched attributes during visit.
// This is used to decide if there is additional unmatched attributes.
size_t hit_count_{0};
// constructor
AttrInitVisitor(const char* type_key, FFind ffind) : type_key_(type_key),
ffind_(ffind) {}
template <typename T>
AttrInitEntry<T> operator()(const char* key, T* value) {
AttrInitEntry<T> opt;
TVMArgValue val;
opt.type_key_ = type_key_;
opt.key_ = key;
opt.value_ = value;
if (ffind_(key, &val)) {
SetValue(value, val);
opt.value_missing_ = false;
++hit_count_;
} else {
opt.value_missing_ = true;
}
return std::move(opt); // Note! force move
}
private:
// the type key
const char* type_key_;
FFind ffind_;
};
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]