src/model/ComponentWatcher.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "ComponentWatcher.hpp" | ||
| 7 | #include "ComponentWatcher_Impl.hpp" | ||
| 8 | |||
| 9 | #include "Model.hpp" | ||
| 10 | #include "Model_Impl.hpp" | ||
| 11 | |||
| 12 | #include "ModelObject.hpp" | ||
| 13 | #include "ModelObject_Impl.hpp" | ||
| 14 | |||
| 15 | #include "ComponentData_Impl.hpp" | ||
| 16 | #include "../nano/nano_signal_slot.hpp" | ||
| 17 | |||
| 18 | #include "../utilities/core/Assert.hpp" | ||
| 19 | #include "../utilities/core/Compare.hpp" | ||
| 20 | |||
| 21 | namespace openstudio { | ||
| 22 | namespace model { | ||
| 23 | |||
| 24 | namespace detail { | ||
| 25 | |||
| 26 |
1/1✓ Branch 4 taken 870 times.
|
870 | ComponentWatcher_Impl::ComponentWatcher_Impl(ComponentData& componentData) : m_componentData(componentData) { |
| 27 | // get componentObjects and connect signals | ||
| 28 | 870 | ModelObject_ImplPtr implPtr; | |
| 29 |
3/3✓ Branch 1 taken 870 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 869 times.
|
870 | if (componentData.numComponentObjects() < 1) { |
| 30 |
13/13✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 19 taken 1 time.
✓ Branch 22 taken 1 time.
✓ Branch 25 taken 1 time.
✓ Branch 28 taken 1 time.
✓ Branch 31 taken 1 time.
✓ Branch 34 taken 1 time.
✓ Branch 38 taken 1 time.
✓ Branch 41 taken 1 time.
|
1 | LOG_AND_THROW("Invalid ComponentData object--no objects listed in contents."); |
| 31 | } | ||
| 32 |
3/3✓ Branch 1 taken 869 times.
✓ Branch 3 taken 1165 times.
✓ Branch 4 taken 869 times.
|
2034 | for (unsigned i = 0, n = componentData.numComponentObjects(); i < n; ++i) { |
| 33 | // get object | ||
| 34 |
2/2✓ Branch 1 taken 1165 times.
✓ Branch 4 taken 1165 times.
|
1165 | OptionalModelObject omo = componentData.getComponentObject(i); |
| 35 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1165 times.
|
1165 | if (!omo) { |
| 36 | ✗ | LOG_AND_THROW("Invalid ComponentData object--blank entry in contents list."); | |
| 37 | } | ||
| 38 |
2/2✓ Branch 1 taken 1165 times.
✓ Branch 4 taken 1165 times.
|
1165 | ModelObject object = *omo; |
| 39 |
1/1✓ Branch 1 taken 1165 times.
|
1165 | m_componentObjects.push_back(object); |
| 40 | // connect to object's signals | ||
| 41 | 1165 | implPtr = object.getImpl<ModelObject_Impl>(); | |
| 42 | // data change | ||
| 43 |
1/1✓ Branch 2 taken 1165 times.
|
1165 | implPtr.get()->ModelObject_Impl::onDataChange.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::dataChange>(this); |
| 44 | // name change | ||
| 45 |
1/1✓ Branch 2 taken 1165 times.
|
1165 | implPtr.get()->ModelObject_Impl::onNameChange.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::nameChange>(this); |
| 46 | // relationship change | ||
| 47 |
1/1✓ Branch 2 taken 1165 times.
|
1165 | implPtr.get()->ModelObject_Impl::onRelationshipChange.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::relationshipChange>(this); |
| 48 | // remove | ||
| 49 |
1/1✓ Branch 2 taken 1165 times.
|
1165 | implPtr.get()->ModelObject_Impl::onRemoveFromWorkspace.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::objectRemove>(this); |
| 50 | 1165 | } | |
| 51 | // connect signals for ComponentData, ComponentDataTags, and ComponentDataAttributes | ||
| 52 | 869 | implPtr = componentData.getImpl<ModelObject_Impl>(); | |
| 53 | // component data change | ||
| 54 |
1/1✓ Branch 2 taken 869 times.
|
869 | implPtr.get()->ModelObject_Impl::onDataChange.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::componentDataChange>(this); |
| 55 | // component data remove | ||
| 56 |
1/1✓ Branch 2 taken 869 times.
|
869 | implPtr.get()->ModelObject_Impl::onRemoveFromWorkspace.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::objectRemove>(this); |
| 57 | // connect to addWorkspaceObject signal | ||
| 58 |
1/1✓ Branch 1 taken 869 times.
|
869 | std::shared_ptr<Model_Impl> modelImplPtr = m_componentData.model().getImpl<Model_Impl>(); |
| 59 |
1/1✓ Branch 2 taken 869 times.
|
869 | modelImplPtr.get()->Model_Impl::addWorkspaceObject.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::objectAdd>(this); |
| 60 | 875 | } | |
| 61 | |||
| 62 | 6 | ComponentWatcher ComponentWatcher_Impl::componentWatcher() const { | |
| 63 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 5 taken 6 times.
|
6 | ComponentWatcher result(std::const_pointer_cast<ComponentWatcher_Impl>(shared_from_this())); |
| 64 | 6 | return result; | |
| 65 | } | ||
| 66 | |||
| 67 | 4 | ComponentData ComponentWatcher_Impl::componentData() const { | |
| 68 | 4 | return m_componentData; | |
| 69 | } | ||
| 70 | |||
| 71 | 3 | void ComponentWatcher_Impl::dataChange() { | |
| 72 | 3 | mf_changeComponentVersion(); | |
| 73 | 3 | } | |
| 74 | |||
| 75 | 3 | void ComponentWatcher_Impl::nameChange() { | |
| 76 | 3 | mf_changeComponentVersion(); | |
| 77 | 3 | } | |
| 78 | |||
| 79 | ✗ | void ComponentWatcher_Impl::componentDataChange() { | |
| 80 | ✗ | mf_changeComponentVersion(); | |
| 81 | ✗ | return; | |
| 82 | } | ||
| 83 | |||
| 84 | 1 | void ComponentWatcher_Impl::relationshipChange(int /*index*/, Handle newHandle, Handle oldHandle) { | |
| 85 |
2/4✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 6 not taken.
|
1 | OS_ASSERT(newHandle != oldHandle); |
| 86 | // if oldHandle is in the Component nullify the component | ||
| 87 |
1/1✓ Branch 1 taken 1 time.
|
1 | HandleVector hs = getHandles<ModelObject>(m_componentObjects); |
| 88 |
2/3✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
|
1 | if (std::find(hs.begin(), hs.end(), oldHandle) != hs.end()) { |
| 89 |
1/1✓ Branch 1 taken 1 time.
|
1 | mf_removeComponent(); |
| 90 | 1 | return; | |
| 91 | } | ||
| 92 | // otherwise, change version | ||
| 93 | ✗ | mf_changeComponentVersion(); | |
| 94 | ✗ | return; | |
| 95 | 1 | } | |
| 96 | |||
| 97 | 8 | void ComponentWatcher_Impl::objectRemove(const Handle& handleOfRemovedObject) { | |
| 98 | |||
| 99 | // no ComponentData means no Component to watch | ||
| 100 |
4/4✓ Branch 1 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 2 times.
|
8 | if (handleOfRemovedObject == m_componentData.handle()) { |
| 101 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
|
6 | this->obsolete.nano_emit(componentWatcher()); |
| 102 | 8 | return; | |
| 103 | } | ||
| 104 | |||
| 105 | // if removedObject is the primary componentObject, nullify the component. | ||
| 106 |
1/3✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
|
2 | OS_ASSERT(!m_componentObjects.empty()); |
| 107 |
3/4✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
|
2 | if (handleOfRemovedObject == m_componentObjects[0].handle()) { |
| 108 |
1/1✓ Branch 1 taken 2 times.
|
2 | mf_removeComponent(); |
| 109 | 2 | return; | |
| 110 | } | ||
| 111 | |||
| 112 | // if removedObject is a componentObject, remove from the vector and refresh | ||
| 113 | // component contents | ||
| 114 | ✗ | auto it = std::find_if(m_componentObjects.begin(), m_componentObjects.end(), [&handleOfRemovedObject](const ModelObject& mo) { | |
| 115 | ✗ | return handleEquals<ModelObject, Handle>(mo, handleOfRemovedObject); | |
| 116 | }); | ||
| 117 | ✗ | if (it != m_componentObjects.end()) { | |
| 118 | ✗ | OS_ASSERT(it != m_componentObjects.begin()); | |
| 119 | ✗ | m_componentObjects.erase(it); | |
| 120 | ✗ | mf_refreshComponentContents(false); | |
| 121 | ✗ | return; | |
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | 2439 | void ComponentWatcher_Impl::objectAdd(const WorkspaceObject& addedObject, const openstudio::IddObjectType& /*type*/, | |
| 126 | const openstudio::UUID& /*uuid*/) { | ||
| 127 |
2/2✓ Branch 1 taken 2439 times.
✓ Branch 4 taken 2439 times.
|
2439 | /*IddObjectType type =*/addedObject.iddObject().type(); |
| 128 | 2439 | return; | |
| 129 | } | ||
| 130 | |||
| 131 | 6 | void ComponentWatcher_Impl::mf_changeComponentVersion() { | |
| 132 | // disconnect componentDataChange slot to avoid endless loop | ||
| 133 | 6 | std::shared_ptr<ModelObject_Impl> implPtr = m_componentData.getImpl<ModelObject_Impl>(); | |
| 134 |
1/1✓ Branch 2 taken 6 times.
|
6 | implPtr.get()->onDataChange.disconnect<ComponentWatcher_Impl, &ComponentWatcher_Impl::componentDataChange>(this); |
| 135 | |||
| 136 | // change version UUID | ||
| 137 |
1/1✓ Branch 1 taken 6 times.
|
6 | m_componentData.createVersionUUID(); |
| 138 | |||
| 139 | // reconnect componentDataChange | ||
| 140 |
1/1✓ Branch 2 taken 6 times.
|
6 | implPtr.get()->ModelObject_Impl::onDataChange.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::componentDataChange>(this); |
| 141 | 6 | } | |
| 142 | |||
| 143 | ✗ | void ComponentWatcher_Impl::mf_refreshComponentContents(bool /*logWarnings*/) { | |
| 144 | // disconnect componentDataChange slot to avoid endless loop | ||
| 145 | ✗ | std::shared_ptr<ModelObject_Impl> implPtr = m_componentData.getImpl<ModelObject_Impl>(); | |
| 146 | ✗ | implPtr.get()->onDataChange.disconnect<ComponentWatcher_Impl, &ComponentWatcher_Impl::componentDataChange>(this); | |
| 147 | |||
| 148 | // \todo logWarnings | ||
| 149 | ✗ | m_componentData.clearExtensibleGroups(); | |
| 150 | ✗ | for (const ModelObject& object : m_componentObjects) { | |
| 151 | ✗ | m_componentData.getImpl<ComponentData_Impl>()->registerObject(object); | |
| 152 | } | ||
| 153 | |||
| 154 | // reconnect componentDataChange | ||
| 155 | ✗ | implPtr.get()->ModelObject_Impl::onDataChange.connect<ComponentWatcher_Impl, &ComponentWatcher_Impl::componentDataChange>(this); | |
| 156 | ✗ | } | |
| 157 | |||
| 158 | 3 | void ComponentWatcher_Impl::mf_removeComponent() { | |
| 159 |
1/1✓ Branch 1 taken 3 times.
|
3 | m_componentData.remove(); // triggers emit through slot objectRemove |
| 160 | 3 | } | |
| 161 | |||
| 162 | } // namespace detail | ||
| 163 | |||
| 164 |
3/4✓ Branch 2 taken 869 times.
✓ Branch 5 taken 869 times.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
|
870 | ComponentWatcher::ComponentWatcher(ComponentData& componentData) : m_impl(new detail::ComponentWatcher_Impl(componentData)) {} |
| 165 | |||
| 166 | 4 | ComponentData ComponentWatcher::componentData() const { | |
| 167 | 4 | return m_impl->componentData(); | |
| 168 | } | ||
| 169 | |||
| 170 | 6 | bool ComponentWatcher::operator==(const ComponentWatcher& other) { | |
| 171 | 6 | return m_impl == other.m_impl; | |
| 172 | } | ||
| 173 | |||
| 174 | ✗ | bool ComponentWatcher::operator!=(const ComponentWatcher& other) { | |
| 175 | ✗ | return (!operator==(other)); | |
| 176 | } | ||
| 177 | |||
| 178 | /// @cond | ||
| 179 | 869 | std::shared_ptr<detail::ComponentWatcher_Impl> ComponentWatcher::getImpl() const { | |
| 180 | 869 | return m_impl; | |
| 181 | } | ||
| 182 | |||
| 183 | 6 | ComponentWatcher::ComponentWatcher(std::shared_ptr<detail::ComponentWatcher_Impl> impl) : m_impl(std::move(impl)) {} | |
| 184 | /// @endcond | ||
| 185 | |||
| 186 | } // namespace model | ||
| 187 | } // namespace openstudio | ||
| 188 |