/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-parameter" #pragma clang diagnostic ignored "-Wunused-variable" #pragma clang diagnostic ignored "-Wunused-value" #define C2_LOG_VERBOSE #include #include #include #include #include #include #include #include #include #include std::ostream& operator<<(std::ostream& os, const _C2FieldId &i); std::ostream& operator<<(std::ostream& os, const C2ParamField &i); /* ---------------------------- C2SupportedRange ---------------------------- */ /** * Helper class for supported values range calculations. */ template::value> struct _C2TypedSupportedRangeHelper { /** * type of range size: a - b if a >= b and a and b are of type T */ typedef typename std::make_unsigned::type DiffType; /** * calculate (high - low) mod step */ static DiffType mod(T low, T high, T step) { return DiffType(high - low) % DiffType(step); } }; template struct _C2TypedSupportedRangeHelper { typedef T DiffType; static DiffType mod(T low, T high, T step) { return fmod(high - low, step); } }; template C2SupportedRange::C2SupportedRange(const C2FieldSupportedValues &values) { if (values.type == C2FieldSupportedValues::RANGE) { _mMin = values.range.min.ref(); _mMax = values.range.max.ref(); _mStep = values.range.step.ref(); _mNum = values.range.num.ref(); _mDenom = values.range.denom.ref(); } else { _mMin = MAX_VALUE; _mMax = MIN_VALUE; _mStep = MIN_STEP; _mNum = 0; _mDenom = 0; } } template bool C2SupportedRange::contains(T value) const { // value must fall between min and max if (value < _mMin || value > _mMax) { return false; } // simple ranges contain all values between min and max if (isSimpleRange()) { return true; } // min is always part of the range if (value == _mMin) { return true; } // stepped ranges require (val - min) % step to be zero if (isArithmeticSeries()) { return _C2TypedSupportedRangeHelper::mod(_mMin, value, _mStep) == 0; } // pure geometric series require (val / min) to be integer multiple of (num/denom) if (isGeometricSeries()) { if (value <= 0) { return false; } double log2base = log2(_mNum / _mDenom); double power = llround(log2(value / double(_mMin)) / log2base); // TODO: validate that result falls within precision (other than round) return value == T(_mMin * pow(_mNum / _mDenom, power) + MIN_STEP / 2); } // multiply-accumulate series require validating by walking through the series if (isMacSeries()) { double lastValue = _mMin; double base = _mNum / _mDenom; while (true) { // this cast is safe as _mMin <= lastValue <= _mMax if (T(lastValue + MIN_STEP / 2) == value) { return true; } double nextValue = fma(lastValue, base, _mStep); if (nextValue <= lastValue || nextValue > _mMax) { return false; // series is no longer monotonic or within range } lastValue = nextValue; }; } // if we are here, this must be an invalid range return false; } template C2SupportedRange C2SupportedRange::limitedTo(const C2SupportedRange &limit) const { // TODO - this only works for simple ranges return C2SupportedRange(std::max(_mMin, limit._mMin), std::min(_mMax, limit._mMax), std::max(_mStep, limit._mStep)); } template class C2SupportedRange; template class C2SupportedRange; template class C2SupportedRange; template class C2SupportedRange; //template class C2SupportedRange; template class C2SupportedRange; template class C2SupportedRange; //template class C2SupportedRange; template class C2SupportedRange; /* -------------------------- C2SupportedFlags -------------------------- */ /** * Ordered supported flag set for a field of a given type. */ // float flags are not supported, but define a few methods to support generic supported values code template<> bool C2SupportedFlags::contains(float value) const { return false; } template<> const std::vector C2SupportedFlags::flags() const { return std::vector(); } template<> C2SupportedFlags C2SupportedFlags::limitedTo(const C2SupportedFlags &limit) const { std::vector values; return C2SupportedFlags(std::move(values)); } template<> float C2SupportedFlags::min() const { return 0; } template bool C2SupportedFlags::contains(T value) const { // value must contain the minimal mask T minMask = min(); if (~value & minMask) { return false; } value &= ~minMask; // otherwise, remove flags from value and see if we arrive at 0 for (const C2Value::Primitive &v : _mValues) { if (value == 0) { break; } if ((~value & v.ref()) == 0) { value &= ~v.ref(); } } return value == 0; } template const std::vector C2SupportedFlags::flags() const { std::vector vals(c2_max(_mValues.size(), 1u) - 1); if (!_mValues.empty()) { std::transform(_mValues.cbegin() + 1, _mValues.cend(), vals.begin(), [](const C2Value::Primitive &p)->T { return p.ref(); }); } return vals; } template C2SupportedFlags C2SupportedFlags::limitedTo(const C2SupportedFlags &limit) const { std::vector values = _mValues; // make a copy T minMask = min() | limit.min(); // minimum mask must be covered by both this and other if (limit.contains(minMask) && contains(minMask)) { values[0] = minMask; // keep only flags that are covered by limit std::remove_if(values.begin(), values.end(), [&limit, minMask](const C2Value::Primitive &v) -> bool { T value = v.ref() | minMask; return value == minMask || !limit.contains(value); }); // we also need to do it vice versa for (const C2Value::Primitive &v : _mValues) { T value = v.ref() | minMask; if (value != minMask && contains(value)) { values.emplace_back((ValueType)value); } } } return C2SupportedFlags(std::move(values)); } template T C2SupportedFlags::min() const { if (!_mValues.empty()) { return _mValues.front().template ref(); } else { return T(0); } } template class C2SupportedFlags; template class C2SupportedFlags; template class C2SupportedFlags; template class C2SupportedFlags; //template class C2SupportedFlags; template class C2SupportedFlags; template class C2SupportedFlags; //template class C2SupportedFlags; /* -------------------------- C2SupportedValueSet -------------------------- */ /** * Ordered supported value set for a field of a given type. */ template bool C2SupportedValueSet::contains(T value) const { return std::find_if(_mValues.cbegin(), _mValues.cend(), [value](const C2Value::Primitive &p) -> bool { return value == p.ref(); }) != _mValues.cend(); } template C2SupportedValueSet C2SupportedValueSet::limitedTo(const C2SupportedValueSet &limit) const { std::vector values = _mValues; // make a copy std::remove_if(values.begin(), values.end(), [&limit](const C2Value::Primitive &v) -> bool { return !limit.contains(v.ref()); }); return C2SupportedValueSet(std::move(values)); } template C2SupportedValueSet C2SupportedValueSet::limitedTo(const C2SupportedRange &limit) const { std::vector values = _mValues; // make a copy std::remove_if(values.begin(), values.end(), [&limit](const C2Value::Primitive &v) -> bool { return !limit.contains(v.ref()); }); return C2SupportedValueSet(std::move(values)); } template C2SupportedValueSet C2SupportedValueSet::limitedTo(const C2SupportedFlags &limit) const { std::vector values = _mValues; // make a copy std::remove_if(values.begin(), values.end(), [&limit](const C2Value::Primitive &v) -> bool { return !limit.contains(v.ref()); }); return C2SupportedValueSet(std::move(values)); } template const std::vector C2SupportedValueSet::values() const { std::vector vals(_mValues.size()); std::transform(_mValues.cbegin(), _mValues.cend(), vals.begin(), [](const C2Value::Primitive &p) -> T { return p.ref(); }); return vals; } template class C2SupportedValueSet; template class C2SupportedValueSet; template class C2SupportedValueSet; template class C2SupportedValueSet; //template class C2SupportedValueSet; template class C2SupportedValueSet; template class C2SupportedValueSet; //template class C2SupportedValueSet; template class C2SupportedValueSet; /* ---------------------- C2FieldSupportedValuesHelper ---------------------- */ template struct C2FieldSupportedValuesHelper::Impl { Impl(const C2FieldSupportedValues &values) : _mType(values.type), _mRange(values), _mValues(values), _mFlags(values) { } bool supports(T value) const; private: typedef typename _C2FieldValueHelper::ValueType ValueType; C2FieldSupportedValues::type_t _mType; C2SupportedRange _mRange; C2SupportedValueSet _mValues; C2SupportedValueSet _mFlags; // friend std::ostream& operator<< (std::ostream& os, const C2FieldSupportedValuesHelper::Impl &i); // friend std::ostream& operator<<(std::ostream& os, const Impl &i); std::ostream& streamOut(std::ostream& os) const; }; template bool C2FieldSupportedValuesHelper::Impl::supports(T value) const { switch (_mType) { case C2FieldSupportedValues::RANGE: return _mRange.contains(value); case C2FieldSupportedValues::VALUES: return _mValues.contains(value); case C2FieldSupportedValues::FLAGS: return _mFlags.contains(value); default: return false; } } template C2FieldSupportedValuesHelper::C2FieldSupportedValuesHelper(const C2FieldSupportedValues &values) : _mImpl(std::make_unique::Impl>(values)) { } template C2FieldSupportedValuesHelper::~C2FieldSupportedValuesHelper() = default; template bool C2FieldSupportedValuesHelper::supports(T value) const { return _mImpl->supports(value); } template class C2FieldSupportedValuesHelper; template class C2FieldSupportedValuesHelper; template class C2FieldSupportedValuesHelper; template class C2FieldSupportedValuesHelper; //template class C2FieldSupportedValuesHelper; template class C2FieldSupportedValuesHelper; template class C2FieldSupportedValuesHelper; //template class C2FieldSupportedValuesHelper; template class C2FieldSupportedValuesHelper; /* ----------------------- C2ParamFieldValuesBuilder ----------------------- */ template struct C2ParamFieldValuesBuilder::Impl { Impl(const C2ParamField &field) : _mParamField(field), _mType(type_t::RANGE), _mDefined(false), _mRange(C2SupportedRange::Any()), _mValues(C2SupportedValueSet::None()), _mFlags(C2SupportedFlags::None()) { } /** * Get C2ParamFieldValues from this builder. */ operator C2ParamFieldValues() const { if (!_mDefined) { return C2ParamFieldValues(_mParamField); } switch (_mType) { case type_t::EMPTY: case type_t::VALUES: return C2ParamFieldValues(_mParamField, (C2FieldSupportedValues)_mValues); case type_t::RANGE: return C2ParamFieldValues(_mParamField, (C2FieldSupportedValues)_mRange); case type_t::FLAGS: return C2ParamFieldValues(_mParamField, (C2FieldSupportedValues)_mFlags); default: // TRESPASS // should never get here return C2ParamFieldValues(_mParamField); } } /** Define the supported values as the currently supported values of this builder. */ void any() { _mDefined = true; } /** Restrict (and thus define) the supported values to none. */ void none() { _mDefined = true; _mType = type_t::VALUES; _mValues.clear(); } /** Restrict (and thus define) the supported values to |value| alone. */ void equalTo(T value) { return limitTo(C2SupportedValueSet::OneOf({value})); } /** Restrict (and thus define) the supported values to a value set. */ void limitTo(const C2SupportedValueSet &limit) { if (!_mDefined) { C2_LOG(VERBOSE) << "NA.limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; // shortcut for first limit applied _mDefined = true; _mValues = limit; _mType = _mValues.isEmpty() ? type_t::EMPTY : type_t::VALUES; } else { switch (_mType) { case type_t::EMPTY: case type_t::VALUES: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mValues) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mValues = _mValues.limitedTo(limit); _mType = _mValues.isEmpty() ? type_t::EMPTY : type_t::VALUES; break; case type_t::RANGE: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mRange) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mValues = limit.limitedTo(_mRange); _mType = _mValues.isEmpty() ? type_t::EMPTY : type_t::VALUES; break; case type_t::FLAGS: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mRange) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mValues = limit.limitedTo(_mFlags); _mType = _mValues.isEmpty() ? type_t::EMPTY : type_t::VALUES; break; default: C2_LOG(FATAL); // should not be here } // TODO: support flags } C2_LOG(VERBOSE) << " = " << _mType << ":" << C2FieldSupportedValuesHelper(_mValues); } /** Restrict (and thus define) the supported values to a flag set. */ void limitTo(const C2SupportedFlags &limit) { if (!_mDefined) { C2_LOG(VERBOSE) << "NA.limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; // shortcut for first limit applied _mDefined = true; _mFlags = limit; _mType = _mFlags.isEmpty() ? type_t::EMPTY : type_t::FLAGS; } else { switch (_mType) { case type_t::EMPTY: case type_t::VALUES: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mValues) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mValues = _mValues.limitedTo(limit); _mType = _mValues.isEmpty() ? type_t::EMPTY : type_t::VALUES; C2_LOG(VERBOSE) << " = " << _mType << ":" << C2FieldSupportedValuesHelper(_mValues); break; case type_t::FLAGS: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mFlags) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mFlags = _mFlags.limitedTo(limit); _mType = _mFlags.isEmpty() ? type_t::EMPTY : type_t::FLAGS; C2_LOG(VERBOSE) << " = " << _mType << ":" << C2FieldSupportedValuesHelper(_mFlags); break; case type_t::RANGE: C2_LOG(FATAL) << "limiting ranges to flags is not supported"; _mType = type_t::EMPTY; break; default: C2_LOG(FATAL); // should not be here } } } void limitTo(const C2SupportedRange &limit) { if (!_mDefined) { C2_LOG(VERBOSE) << "NA.limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; // shortcut for first limit applied _mDefined = true; _mRange = limit; _mType = _mRange.isEmpty() ? type_t::EMPTY : type_t::RANGE; C2_LOG(VERBOSE) << " = " << _mType << ":" << C2FieldSupportedValuesHelper(_mRange); } else { switch (_mType) { case type_t::EMPTY: case type_t::VALUES: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mValues) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mValues = _mValues.limitedTo(limit); _mType = _mValues.isEmpty() ? type_t::EMPTY : type_t::VALUES; C2_LOG(VERBOSE) << " = " << _mType << ":" << C2FieldSupportedValuesHelper(_mValues); break; case type_t::FLAGS: C2_LOG(FATAL) << "limiting flags to ranges is not supported"; _mType = type_t::EMPTY; break; case type_t::RANGE: C2_LOG(VERBOSE) << "(" << C2FieldSupportedValuesHelper(_mRange) << ").limitTo(" << C2FieldSupportedValuesHelper(limit) << ")"; _mRange = _mRange.limitedTo(limit); C2_DCHECK(_mValues.isEmpty()); _mType = _mRange.isEmpty() ? type_t::EMPTY : type_t::RANGE; C2_LOG(VERBOSE) << " = " << _mType << ":" << C2FieldSupportedValuesHelper(_mRange); break; default: C2_LOG(FATAL); // should not be here } } } private: void instantiate() __unused { (void)_mValues.values(); // instantiate non-const values() } void instantiate() const __unused { (void)_mValues.values(); // instantiate const values() } typedef C2FieldSupportedValues::type_t type_t; C2ParamField _mParamField; type_t _mType; bool _mDefined; C2SupportedRange _mRange; C2SupportedValueSet _mValues; C2SupportedFlags _mFlags; }; template C2ParamFieldValuesBuilder::operator C2ParamFieldValues() const { return (C2ParamFieldValues)(*_mImpl.get()); } template C2ParamFieldValuesBuilder::C2ParamFieldValuesBuilder(const C2ParamField &field) : _mImpl(std::make_unique::Impl>(field)) { } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::any() { _mImpl->any(); return *this; } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::none() { _mImpl->none(); return *this; } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::equalTo(T value) { _mImpl->equalTo(value); return *this; } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::limitTo(const C2SupportedValueSet &limit) { _mImpl->limitTo(limit); return *this; } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::limitTo(const C2SupportedFlags &limit) { _mImpl->limitTo(limit); return *this; } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::limitTo(const C2SupportedRange &limit) { _mImpl->limitTo(limit); return *this; } template C2ParamFieldValuesBuilder::C2ParamFieldValuesBuilder(const C2ParamFieldValuesBuilder &other) : _mImpl(std::make_unique::Impl>(*other._mImpl.get())) { } template C2ParamFieldValuesBuilder &C2ParamFieldValuesBuilder::operator=( const C2ParamFieldValuesBuilder &other) { _mImpl = std::make_unique::Impl>(*other._mImpl.get()); return *this; } template C2ParamFieldValuesBuilder::~C2ParamFieldValuesBuilder() = default; template class C2ParamFieldValuesBuilder; template class C2ParamFieldValuesBuilder; template class C2ParamFieldValuesBuilder; template class C2ParamFieldValuesBuilder; //template class C2ParamFieldValuesBuilder; template class C2ParamFieldValuesBuilder; template class C2ParamFieldValuesBuilder; //template class C2ParamFieldValuesBuilder; template class C2ParamFieldValuesBuilder; /* ------------------------- C2SettingResultBuilder ------------------------- */ C2SettingConflictsBuilder::C2SettingConflictsBuilder() : _mConflicts() { } C2SettingConflictsBuilder::C2SettingConflictsBuilder(C2ParamFieldValues &&conflict) { _mConflicts.emplace_back(std::move(conflict)); } C2SettingConflictsBuilder& C2SettingConflictsBuilder::with(C2ParamFieldValues &&conflict) { _mConflicts.emplace_back(std::move(conflict)); return *this; } std::vector C2SettingConflictsBuilder::retrieveConflicts() { return std::move(_mConflicts); } /* ------------------------- C2SettingResult/sBuilder ------------------------- */ C2SettingResult C2SettingResultBuilder::ReadOnly(const C2ParamField ¶m) { return C2SettingResult { C2SettingResult::READ_ONLY, { param }, { } }; } C2SettingResult C2SettingResultBuilder::BadValue(const C2ParamField ¶mField, bool isInfo) { return { isInfo ? C2SettingResult::INFO_BAD_VALUE : C2SettingResult::BAD_VALUE, { paramField }, { } }; } C2SettingResult C2SettingResultBuilder::Conflict( C2ParamFieldValues &¶mFieldValues, C2SettingConflictsBuilder &conflicts, bool isInfo) { C2_CHECK(!conflicts.empty()); if (isInfo) { return C2SettingResult { C2SettingResult::INFO_CONFLICT, std::move(paramFieldValues), conflicts.retrieveConflicts() }; } else { return C2SettingResult { C2SettingResult::CONFLICT, std::move(paramFieldValues), conflicts.retrieveConflicts() }; } } C2SettingResultsBuilder::C2SettingResultsBuilder(C2SettingResult &&result) : _mStatus(C2_BAD_VALUE) { _mResults.emplace_back(new C2SettingResult(std::move(result))); } C2SettingResultsBuilder C2SettingResultsBuilder::plus(C2SettingResultsBuilder&& results) { for (std::unique_ptr &r : results._mResults) { _mResults.emplace_back(std::move(r)); } results._mResults.clear(); // TODO: mStatus return std::move(*this); } c2_status_t C2SettingResultsBuilder::retrieveFailures( std::vector>* const failures) { for (std::unique_ptr &r : _mResults) { failures->emplace_back(std::move(r)); } _mResults.clear(); return _mStatus; } C2SettingResultsBuilder::C2SettingResultsBuilder(c2_status_t status) : _mStatus(status) { // status must be one of OK, BAD_STATE, TIMED_OUT or CORRUPTED // mainly: BLOCKING, BAD_INDEX, BAD_VALUE and NO_MEMORY requires a setting attempt } #pragma clang diagnostic pop /* ------------------------- C2FieldUtils ------------------------- */ struct C2_HIDE C2FieldUtils::_Inspector { /// returns the implementation object inline static std::shared_ptr GetImpl(const Info &info) { return info._mImpl; } }; /* ------------------------- C2FieldUtils::Info ------------------------- */ struct C2_HIDE C2FieldUtils::Info::Impl { C2FieldDescriptor field; std::shared_ptr parent; uint32_t index; uint32_t depth; uint32_t baseFieldOffset; uint32_t arrayOffset; uint32_t usedExtent; /// creates a copy of this object including copies of its parent chain Impl clone() const; /// creates a copy of a shared pointer to an object static std::shared_ptr Clone(const std::shared_ptr &); Impl(const C2FieldDescriptor &field_, std::shared_ptr parent_, uint32_t index_, uint32_t depth_, uint32_t baseFieldOffset_, uint32_t arrayOffset_, uint32_t usedExtent_) : field(field_), parent(parent_), index(index_), depth(depth_), baseFieldOffset(baseFieldOffset_), arrayOffset(arrayOffset_), usedExtent(usedExtent_) { } }; std::shared_ptr C2FieldUtils::Info::Impl::Clone(const std::shared_ptr &info) { if (info) { return std::make_shared(info->clone()); } return nullptr; } C2FieldUtils::Info::Impl C2FieldUtils::Info::Impl::clone() const { Impl res = Impl(*this); res.parent = Clone(res.parent); return res; } C2FieldUtils::Info::Info(std::shared_ptr impl) : _mImpl(impl) { } size_t C2FieldUtils::Info::arrayOffset() const { return _mImpl->arrayOffset; } size_t C2FieldUtils::Info::arraySize() const { return extent() * size(); } size_t C2FieldUtils::Info::baseFieldOffset() const { return _mImpl->baseFieldOffset; }; size_t C2FieldUtils::Info::depth() const { return _mImpl->depth; } size_t C2FieldUtils::Info::extent() const { return _mImpl->usedExtent; } size_t C2FieldUtils::Info::index() const { return _mImpl->index; } bool C2FieldUtils::Info::isArithmetic() const { switch (_mImpl->field.type()) { case C2FieldDescriptor::BLOB: case C2FieldDescriptor::CNTR32: case C2FieldDescriptor::CNTR64: case C2FieldDescriptor::FLOAT: case C2FieldDescriptor::INT32: case C2FieldDescriptor::INT64: case C2FieldDescriptor::STRING: case C2FieldDescriptor::UINT32: case C2FieldDescriptor::UINT64: return true; default: return false; } } bool C2FieldUtils::Info::isFlexible() const { return _mImpl->field.extent() == 0; } C2String C2FieldUtils::Info::name() const { return _mImpl->field.name(); } const C2FieldUtils::Info::NamedValuesType &C2FieldUtils::Info::namedValues() const { return _mImpl->field.namedValues(); } size_t C2FieldUtils::Info::offset() const { return _C2ParamInspector::GetOffset(_mImpl->field); } C2FieldUtils::Info C2FieldUtils::Info::parent() const { return Info(_mImpl->parent); }; size_t C2FieldUtils::Info::size() const { return _C2ParamInspector::GetSize(_mImpl->field); } C2FieldUtils::Info::type_t C2FieldUtils::Info::type() const { return _mImpl->field.type(); } /* ------------------------- C2FieldUtils::Iterator ------------------------- */ struct C2_HIDE C2FieldUtils::Iterator::Impl : public _C2ParamInspector { Impl() = default; virtual ~Impl() = default; /// implements object equality virtual bool equals(const std::shared_ptr &other) const { return other != nullptr && mHead == other->mHead; }; /// returns the info pointed to by this iterator virtual value_type get() const { return Info(mHead); } /// increments this iterator virtual void increment() { // note: this cannot be abstract as we instantiate this for List::end(). increment to end() // instead. mHead.reset(); } protected: Impl(std::shared_ptr head) : mHead(head) { } std::shared_ptr mHead; ///< current field }; C2FieldUtils::Iterator::Iterator(std::shared_ptr impl) : mImpl(impl) { } C2FieldUtils::Iterator::value_type C2FieldUtils::Iterator::operator*() const { return mImpl->get(); } C2FieldUtils::Iterator& C2FieldUtils::Iterator::operator++() { mImpl->increment(); return *this; } bool C2FieldUtils::Iterator::operator==(const Iterator &other) const { return mImpl->equals(other.mImpl); } /* ------------------------- C2FieldUtils::List ------------------------- */ struct C2_HIDE C2FieldUtils::List::Impl { virtual std::shared_ptr begin() const = 0; /// returns an iterator to the end of the list virtual std::shared_ptr end() const { return std::make_shared(); } virtual ~Impl() = default; }; C2FieldUtils::List::List(std::shared_ptr impl) : mImpl(impl) { } C2FieldUtils::Iterator C2FieldUtils::List::begin() const { return C2FieldUtils::Iterator(mImpl->begin()); } C2FieldUtils::Iterator C2FieldUtils::List::end() const { return C2FieldUtils::Iterator(mImpl->end()); } /* ------------------------- C2FieldUtils::enumerateFields ------------------------- */ namespace { /** * Iterator base class helper that allows descending into the field hierarchy. */ struct C2FieldUtilsFieldsIteratorHelper : public C2FieldUtils::Iterator::Impl { virtual ~C2FieldUtilsFieldsIteratorHelper() override = default; /// returns the base-field's offset of the parent field (or the param offset if no parent) static inline uint32_t GetParentBaseFieldOffset( const std::shared_ptr parent) { return parent == nullptr ? sizeof(C2Param) : parent->baseFieldOffset; } /// returns the offset of the parent field (or the param) static inline uint32_t GetParentOffset(const std::shared_ptr parent) { return parent == nullptr ? sizeof(C2Param) : GetOffset(parent->field); } protected: C2FieldUtilsFieldsIteratorHelper( std::shared_ptr reflector, uint32_t paramSize, std::shared_ptr head = nullptr) : C2FieldUtils::Iterator::Impl(head), mParamSize(paramSize), mReflector(reflector) { } /// returns a leaf info object at a specific index for a child field std::shared_ptr makeLeaf( const C2FieldDescriptor &field, uint32_t index) { uint32_t parentOffset = GetParentOffset(mHead); uint32_t arrayOffset = parentOffset + GetOffset(field); uint32_t usedExtent = field.extent() ? : (std::max(arrayOffset, mParamSize) - arrayOffset) / GetSize(field); return std::make_shared( OffsetFieldDescriptor(field, parentOffset + index * GetSize(field)), mHead /* parent */, index, mHead == nullptr ? 0 : mHead->depth + 1, GetParentBaseFieldOffset(mHead) + GetOffset(field), arrayOffset, usedExtent); } /// returns whether this struct index have been traversed to get to this field bool visited(C2Param::CoreIndex index) const { for (const std::shared_ptr &sd : mHistory) { if (sd->coreIndex() == index) { return true; } } return false; } uint32_t mParamSize; std::shared_ptr mReflector; std::vector> mHistory; // structure types visited }; /** * Iterator implementing enumerateFields() that visits each base field. */ struct C2FieldUtilsFieldsIterator : public C2FieldUtilsFieldsIteratorHelper { /// enumerate base fields of a parameter C2FieldUtilsFieldsIterator(const C2Param ¶m, std::shared_ptr reflector) : C2FieldUtilsFieldsIteratorHelper(reflector, param.size()) { descendInto(param.coreIndex()); } /// enumerate base fields of a field C2FieldUtilsFieldsIterator(std::shared_ptr impl) : C2FieldUtilsFieldsIteratorHelper(impl->mReflector, impl->mParamSize, impl->mHead) { mHistory = impl->mHistory; if (mHead->field.type() & C2FieldDescriptor::STRUCT_FLAG) { C2Param::CoreIndex index = { mHead->field.type() &~C2FieldDescriptor::STRUCT_FLAG }; if (!visited(index)) { descendInto(index); } } } virtual ~C2FieldUtilsFieldsIterator() override = default; /// Increments this iterator by visiting each base field. virtual void increment() override { // don't go past end if (mHead == nullptr || _mFields.empty()) { return; } // descend into structures if (mHead->field.type() & C2FieldDescriptor::STRUCT_FLAG) { C2Param::CoreIndex index = { mHead->field.type() &~C2FieldDescriptor::STRUCT_FLAG }; // do not recurse into the same structs if (!visited(index) && descendInto(index)) { return; } } // ascend after the last field in the current struct while (!mHistory.empty() && _mFields.back() == mHistory.back()->end()) { mHead = mHead->parent; mHistory.pop_back(); _mFields.pop_back(); } // done if history is now empty if (_mFields.empty()) { // we could be traversing a sub-tree so clear head mHead.reset(); return; } // move to the next field in the current struct C2StructDescriptor::field_iterator next = _mFields.back(); mHead->field = OffsetFieldDescriptor(*next, GetParentOffset(mHead->parent)); mHead->index = 0; // reset index just in case for correctness mHead->baseFieldOffset = GetParentBaseFieldOffset(mHead->parent) + GetOffset(*next); mHead->arrayOffset = GetOffset(mHead->field); mHead->usedExtent = mHead->field.extent() ? : (std::max(mHead->arrayOffset, mParamSize) - mHead->arrayOffset) / GetSize(mHead->field); ++_mFields.back(); } private: /// If the current field is a known, valid (untraversed) structure, it modifies this iterator /// to point to the first field of the structure and returns true. Otherwise, it does not /// modify this iterator and returns false. bool descendInto(C2Param::CoreIndex index) { std::unique_ptr descUnique = mReflector->describe(index); // descend into known structs (as long as they have at least one field) if (descUnique && descUnique->begin() != descUnique->end()) { std::shared_ptr desc(std::move(descUnique)); mHistory.emplace_back(desc); C2StructDescriptor::field_iterator first = desc->begin(); mHead = makeLeaf(*first, 0 /* index */); _mFields.emplace_back(++first); return true; } return false; } /// next field pointers for each depth. /// note: _mFields may be shorted than mHistory, if iterating at a depth std::vector _mFields; }; /** * Iterable implementing enumerateFields(). */ struct C2FieldUtilsFieldIterable : public C2FieldUtils::List::Impl { /// returns an iterator to the beginning of the list virtual std::shared_ptr begin() const override { return std::make_shared(*_mParam, _mReflector); }; C2FieldUtilsFieldIterable(const C2Param ¶m, std::shared_ptr reflector) : _mParam(¶m), _mReflector(reflector) { } private: const C2Param *_mParam; std::shared_ptr _mReflector; }; } C2FieldUtils::List C2FieldUtils::enumerateFields( const C2Param ¶m, const std::shared_ptr &reflector) { return C2FieldUtils::List(std::make_shared(param, reflector)); } /* ------------------------- C2FieldUtils::enumerate siblings ------------------------- */ namespace { struct C2FieldUtilsCousinsIterator : public C2FieldUtils::Iterator::Impl { C2FieldUtilsCousinsIterator( const std::shared_ptr &info, size_t level) // clone info chain as this iterator will change it : C2FieldUtils::Iterator::Impl(C2FieldUtils::Info::Impl::Clone(info)) { if (level == 0) { return; } // store parent chain (up to level) for quick access std::shared_ptr node = mHead; size_t ix = 0; for (; ix < level && node; ++ix) { node->index = 0; _mPath.emplace_back(node); node = node->parent; } setupPath(ix); } virtual ~C2FieldUtilsCousinsIterator() override = default; /// Increments this iterator by visiting each index. virtual void increment() override { size_t ix = 0; while (ix < _mPath.size()) { if (++_mPath[ix]->index < _mPath[ix]->usedExtent) { setupPath(ix + 1); return; } _mPath[ix++]->index = 0; } mHead.reset(); } private: /// adjusts field offsets along the path up to the specific level - 1. /// This in-fact has to be done down the path from parent to child as child fields must /// fall within parent fields. void setupPath(size_t level) { C2_CHECK_LE(level, _mPath.size()); uint32_t oldArrayOffset = level ? _mPath[level - 1]->arrayOffset : 0 /* unused */; while (level) { --level; C2FieldUtils::Info::Impl &path = *_mPath[level]; uint32_t size = GetSize(path.field); uint32_t offset = path.arrayOffset + size * path.index; SetOffset(path.field, offset); if (level) { // reset child's array offset to fall within current index, but hold onto the // original value of the arrayOffset so that we can adjust subsequent children. // This is because the modulo is only defined within the current array. uint32_t childArrayOffset = offset + (_mPath[level - 1]->arrayOffset - oldArrayOffset) % size; oldArrayOffset = _mPath[level - 1]->arrayOffset; _mPath[level - 1]->arrayOffset = childArrayOffset; } } } std::vector> _mPath; }; /** * Iterable implementing enumerateFields(). */ struct C2FieldUtilsCousinsIterable : public C2FieldUtils::List::Impl { /// returns an iterator to the beginning of the list virtual std::shared_ptr begin() const override { return std::make_shared(_mHead, _mLevel); }; C2FieldUtilsCousinsIterable(const C2FieldUtils::Info &field, uint32_t level) : _mHead(C2FieldUtils::_Inspector::GetImpl(field)), _mLevel(level) { } private: std::shared_ptr _mHead; size_t _mLevel; }; } C2FieldUtils::List C2FieldUtils::enumerateCousins(const C2FieldUtils::Info &field, uint32_t level) { return C2FieldUtils::List(std::make_shared(field, level)); } /* ------------------------- C2FieldUtils::locateField ------------------------- */ namespace { /** * Iterator implementing locateField(). */ struct C2FieldUtilsFieldLocator : public C2FieldUtilsFieldsIteratorHelper { C2FieldUtilsFieldLocator( C2Param::CoreIndex index, const _C2FieldId &field, uint32_t paramSize, std::shared_ptr reflector) : C2FieldUtilsFieldsIteratorHelper(reflector, paramSize), _mField(field) { while (descendInto(index)) { if ((mHead->field.type() & C2FieldDescriptor::STRUCT_FLAG) == 0) { break; } index = C2Param::CoreIndex(mHead->field.type() &~ C2FieldDescriptor::STRUCT_FLAG); } } void increment() { mHead = _mTail; _mTail = nullptr; } private: /// If the current field is a known, valid (untraversed) structure, it modifies this iterator /// to point to the field at the beginning/end of the given field of the structure and returns /// true. Otherwise, including if no such field exists in the structure, it does not modify this /// iterator and returns false. bool descendInto(C2Param::CoreIndex index) { // check that the boundaries of the field to be located are still within the same parent // field if (mHead != _mTail) { return false; } std::unique_ptr descUnique = mReflector->describe(index); // descend into known structs (as long as they have at least one field) if (descUnique && descUnique->begin() != descUnique->end()) { std::shared_ptr desc(std::move(descUnique)); mHistory.emplace_back(desc); uint32_t parentOffset = GetParentOffset(mHead); // locate field using a dummy field descriptor C2FieldDescriptor dummy = { C2FieldDescriptor::BLOB, 1 /* extent */, "name", GetOffset(_mField) - parentOffset, GetSize(_mField) }; // locate first field where offset is greater than dummy offset (which is one past) auto it = std::upper_bound( desc->cbegin(), desc->cend(), dummy, [](const C2FieldDescriptor &a, const C2FieldDescriptor &b) -> bool { return _C2ParamInspector::GetOffset(a) < _C2ParamInspector::GetOffset(b); }); if (it == desc->begin()) { // field is prior to first field return false; } --it; const C2FieldDescriptor &field = *it; // check that dummy end-offset is within this field uint32_t structSize = std::max(mParamSize, parentOffset) - parentOffset; if (GetEndOffset(dummy) > GetEndOffset(field, structSize)) { return false; } uint32_t startIndex = (GetOffset(dummy) - GetOffset(field)) / GetSize(field); uint32_t endIndex = (GetEndOffset(dummy) - GetOffset(field) + GetSize(field) - 1) / GetSize(field); if (endIndex > startIndex) { // Field size could be zero, in which case end index is still on start index. // However, for all other cases, endIndex was rounded up to the next index, so // decrement it. --endIndex; } std::shared_ptr startLeaf = makeLeaf(field, startIndex); if (endIndex == startIndex) { _mTail = startLeaf; mHead = startLeaf; } else { _mTail = makeLeaf(field, endIndex); mHead = startLeaf; } return true; } return false; } _C2FieldId _mField; std::shared_ptr _mTail; }; /** * Iterable implementing locateField(). */ struct C2FieldUtilsFieldLocation : public C2FieldUtils::List::Impl { /// returns an iterator to the beginning of the list virtual std::shared_ptr begin() const override { return std::make_shared( _mIndex, _mField, _mParamSize, _mReflector); }; C2FieldUtilsFieldLocation( const C2ParamField &pf, std::shared_ptr reflector) : _mIndex(C2Param::CoreIndex(_C2ParamInspector::GetIndex(pf))), _mField(_C2ParamInspector::GetField(pf)), _mParamSize(0), _mReflector(reflector) { } C2FieldUtilsFieldLocation( const C2Param ¶m, const _C2FieldId &field, std::shared_ptr reflector) : _mIndex(param.coreIndex()), _mField(field), _mParamSize(param.size()), _mReflector(reflector) { } private: C2Param::CoreIndex _mIndex; _C2FieldId _mField; uint32_t _mParamSize; std::shared_ptr _mReflector; }; } std::vector C2FieldUtils::locateField( const C2ParamField &pf, const std::shared_ptr &reflector) { C2FieldUtils::List location = { std::make_shared(pf, reflector) }; return std::vector(location.begin(), location.end()); } std::vector C2FieldUtils::locateField( const C2Param ¶m, const _C2FieldId &field, const std::shared_ptr &reflector) { C2FieldUtils::List location = { std::make_shared(param, field, reflector) }; return std::vector(location.begin(), location.end()); }