Skip to content

state_list: shift an element's whole subtree and data on erase/insert - #672

Merged
transfix merged 1 commit into
masterfrom
fix/state-list-subtree-copy
Oct 18, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/state-list-subtree-copy

Conversation

@transfix

Copy link
Copy Markdown
Owner

Found while doing #668.

Why

state_list::erase() and insert() move each later element by one index. They copied an element with dst.ptree(src.ptree()), which breaks for any element that has children:

  • The subtree landed in the wrong place. ptree() keys every node by its full path from the root, and ptree(pt) applies those keys relative to dst. So erasing element 0 of lst, where element 1 has a child x, wrote:

    • the value of lst.000001 to lst.000000.lst.000001;
    • x's value to lst.000000.lst.000001.x.

    Meanwhile lst.000000.x kept its old value. This was the same before and after state: json, traverse and reset without recursing once per level #668: the loader has always been key-based.

  • data() was never moved. The guard src.isData<boost::any>() is any_cast<boost::any>(data()), which fails for anything that is not itself a boost::any. So an element's data stayed at its old index.

The existing tests only use scalar elements, so nothing caught either problem.

What

  • copy_element(dst, src) (file-local in state_list.cpp):

    • writes src's value and data, and those of every node under it, at the same relative path under dst, creating nodes as needed;
    • then resets the nodes under dst that src lacks, with signals, so observers hear the clear.

    data() is written only when either side has some, so an element without data gets no extra dataChanged.

  • Nodes are kept, never dropped, the same way pop_back() keeps the element it clears. A state& into an element stays valid, and its connections (the sync adapter's included) keep working. Dropping nodes would leave the adapter holding connections for paths it never reattaches.

  • ptree(pt)/json(j) keep their semantics. Keys are absolute and applied relative to the target node, as ariadne's state_io.h documents ("restoring into a different node re-roots the saved absolute paths beneath it"). Every in-repo restore targets the root. The xmlrpc cvcstate_get_json/cvcstate_set_json help strings and a comment now say so, including that passing "" to set_json restores a get_json document in place.

    • xmlrpc is off in the local build (CVC_USING_XMLRPC=OFF), so that file was not compiled here. The change there is only string literals and a comment.

Tests

  • New StateListTest cases (3). On master they fail, including a bad_any_cast for the unshifted data; on this branch they pass:

    • EraseShiftsSubtreesAndData
    • InsertShiftsSubtreesAndData
    • ShiftingKeepsTheNodesAndSignals

    Between them they cover:

    • elements with different subtrees and data();
    • every value and data landing at the shifted index;
    • nodes the incoming element lacks being cleared;
    • no stray …test_list… nodes;
    • a held reference into an element staying the same node and hearing each change.
  • Locally on master + this commit (MSVC 14.38 Release, Windows):

    • All 4039 ctest cases pass except the known ImageMagick ones (image_test CMYK, cvc_cli_test Vol2Img/Img2Vol).
    • state_list_test 23/23, state_test 164/164, state_deep_tree_test 18/18, ariadne_loader_test 178/178.
    • pycvc (not built by PR CI): 20 of 22 scripts pass. The other two are pre-existing: test_pycvc.py hits the rawiv lock that volume: close the file after a successful rawiv write #663 fixes, and test_pycvc_lod.py crashes at exit after all its checks pass.
  • clang-format 18 is clean.

Not changed

A shift moves value and data(), as the old code meant to. It does not move:

  • comment, hidden or read-only flags;
  • link targets;
  • a typed valueTypeName.

erase() and insert() moved each element with dst.ptree(src.ptree()).
ptree() keys every node by its full path from the root and ptree(pt)
applies the keys relative to dst, so an element's subtree was written
under dst.<src's full path> (stray nodes) while dst's own children kept
their old values. data() was never moved either: the isData<boost::any>()
check before it is false for any data that is not itself a boost::any.

copy_element() now writes src's value and data, and those of each node
under it, at the same path under dst, and resets (with signals) the
nodes under dst that src lacks. Nodes are kept, as pop_back() keeps the
one it clears, so references into an element and observers of its
nodes stay valid.

The keys-are-absolute, applied-relative semantics of json()/ptree(pt)
are documented in ariadne's state_io.h and stay; the xmlrpc
cvcstate_get_json/cvcstate_set_json help strings now say so too.
@transfix
transfix merged commit ff002c0 into master Oct 18, 2026
15 checks passed
transfix added a commit that referenced this pull request Oct 18, 2026
…ate-exec-sliced-stdlib

Master is #658's a84d37e, already merged here, plus #672's three files
(state_list, its test, xmlrpc_server). The result is this branch's tree
with those three files from master; the diff against master is this PR's
17 files.
@transfix
transfix deleted the fix/state-list-subtree-copy branch October 18, 2026 04:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant