Skip to content

Commit

Permalink
Added storage of some properties inside the field classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
arobenko committed Oct 28, 2024
1 parent c2960bc commit 3d57557
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 36 deletions.
12 changes: 12 additions & 0 deletions lib/include/cc_tools_qt/ToolsField.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

#include "cc_tools_qt/Api.h"

#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QVariant>

#include <cassert>
#include <cstddef>
Expand Down Expand Up @@ -75,19 +77,29 @@ class CC_API ToolsField
const Members& getMembers() const;
void setMembers(Members&& members);

void forceReadOnly();
bool isReadOnly() const;

void forceHiddenSerialization();
bool isHiddenSerialization() const;

protected:
virtual const char* nameImpl() const = 0;
virtual std::size_t lengthImpl() const = 0;
virtual bool validImpl() const = 0;
virtual bool isReadOnlyImpl() const = 0;
virtual bool isHiddenSerializationImpl() const = 0;
virtual SerialisedSeq getSerialisedValueImpl() const = 0;
virtual bool setSerialisedValueImpl(const SerialisedSeq& value) = 0;
virtual void dispatchImpl(ToolsFieldHandler& handler) = 0;
virtual Ptr cloneImpl() = 0;
virtual bool canWriteImpl() const = 0;
virtual void resetImpl() = 0;


private:
Members m_members;
QVariantMap m_props;
};

using ToolsFieldPtr = ToolsField::Ptr;
Expand Down
42 changes: 6 additions & 36 deletions lib/include/cc_tools_qt/details/ToolsArrayListFieldImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ToolsArrayListFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsA
using Ptr = typename Base::Ptr;
using ActPtr = typename Base::ActPtr;
using PrefixFieldInfo = typename Base::PrefixFieldInfo;
using Members = typename Base::Members;

using WrapFieldCallbackFunc = std::function<ToolsFieldPtr (ElementType&)>;

Expand Down Expand Up @@ -263,35 +264,8 @@ class ToolsArrayListFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsA
void addFieldInternal(HasFeatureTag)
{
auto& col = Base::field().value();

auto& mems = Base::getMembers();

decltype(&col[0]) firstElemPtr = nullptr;
if (!col.empty()) {
firstElemPtr = &col[0];
}

col.push_back(ElementType());
if (!m_wrapFieldFunc) {
[[maybe_unused]] static constexpr bool Callback_is_not_set = false;
assert(Callback_is_not_set);
mems.clear();
return;
}

if (firstElemPtr == &col[0]) {
mems.push_back(m_wrapFieldFunc(col.back()));
assert(col.size() == mems.size());
return;
}

mems.clear();
mems.reserve(col.size());
for (auto& f : col) {
mems.push_back(m_wrapFieldFunc(f));
}

assert(col.size() == mems.size());
refreshMembersInternal(HasFeatureTag());
}

void addFieldInternal(NoFeatureTag)
Expand All @@ -308,12 +282,7 @@ class ToolsArrayListFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsA
}

storage.erase(storage.begin() + idx);
auto& mems = Base::getMembers();
mems.clear();
mems.reserve(storage.size());
for (auto& f : storage) {
mems.push_back(m_wrapFieldFunc(f));
}
refreshMembersInternal(HasFeatureTag());
}

void removeFieldInternal([[maybe_unused]] int idx, NoFeatureTag)
Expand All @@ -330,8 +299,7 @@ class ToolsArrayListFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsA
}

auto& storage = Base::field().value();
auto& mems = Base::getMembers();
mems.clear();
Members mems;
mems.reserve(storage.size());
for (auto& f : storage) {
mems.push_back(m_wrapFieldFunc(f));
Expand All @@ -340,6 +308,8 @@ class ToolsArrayListFieldImpl : public ToolsFieldBase<cc_tools_qt::field::ToolsA
assert(mems.back()->canWrite());
}
}

Base::setMembers(std::move(mems));
}

void refreshMembersInternal(NoFeatureTag)
Expand Down
10 changes: 10 additions & 0 deletions lib/include/cc_tools_qt/details/ToolsFieldBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class ToolsFieldBase : public TBase
return m_field.valid();
}

virtual bool isReadOnlyImpl() const override
{
return Field::hasFixedValue();
}

virtual bool isHiddenSerializationImpl() const override
{
return Field::hasEmptySerialization();
}

Field& field()
{
return m_field;
Expand Down
71 changes: 71 additions & 0 deletions lib/src/ToolsField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,30 @@
#include "cc_tools_qt/ToolsField.h"

#include <cassert>
#include <vector>
#include <utility>

namespace cc_tools_qt
{

namespace
{

const QString& readOnlyProp()
{
static const QString Str("cc.read_only");
return Str;
}

const QString& hiddenSerializationProp()
{
static const QString Str("cc.hidden_ser");
return Str;
}

} // namespace


ToolsField::ToolsField() = default;

ToolsField::~ToolsField() noexcept = default;
Expand Down Expand Up @@ -104,6 +124,7 @@ ToolsField::Ptr ToolsField::clone()
}
auto ptr = cloneImpl();
ptr->setMembers(std::move(clonedMembers));
ptr->m_props = m_props;
return ptr;
}

Expand All @@ -130,6 +151,56 @@ const ToolsField::Members& ToolsField::getMembers() const
void ToolsField::setMembers(Members&& members)
{
m_members = std::move(members);
for (auto& m : m_members) {
using CheckFunc = bool (ToolsField::*)() const;
using ForceFunc = void (ToolsField::*)();
static const std::vector<std::pair<CheckFunc, ForceFunc>> List = {
{&ToolsField::isReadOnly, &ToolsField::forceReadOnly},
};

assert(m);
for (auto& funcs : List) {
if ((m.get()->*funcs.first)()) {
(m.get()->*funcs.second)();
}
}
}
}

void ToolsField::forceReadOnly()
{
m_props[readOnlyProp()] = true;
for (auto& m : m_members) {
assert(m);
m->forceReadOnly();
}
}

bool ToolsField::isReadOnly() const
{
if (m_props.contains(readOnlyProp())) {
return true;
}

return isReadOnlyImpl();
}

void ToolsField::forceHiddenSerialization()
{
m_props[hiddenSerializationProp()] = true;
for (auto& m : m_members) {
assert(m);
m->forceHiddenSerialization();
}
}

bool ToolsField::isHiddenSerialization() const
{
if (m_props.contains(hiddenSerializationProp())) {
return true;
}

return isHiddenSerializationImpl();
}

} // namespace cc_tools_qt
Expand Down

0 comments on commit 3d57557

Please sign in to comment.