File indexing completed on 2024-05-19 05:42:29

0001 // ct_lvtshr_graphstorage.h                                          -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #ifndef INCLUDED_CT_LVTSHR_GRAPHSTORAGE
0021 #define INCLUDED_CT_LVTSHR_GRAPHSTORAGE
0022 
0023 #include <lvtshr_export.h>
0024 
0025 #include <ct_lvtshr_graphenums.h>
0026 #include <deque>
0027 #include <functional>
0028 
0029 // I'm not proud of this code.
0030 namespace Codethink::lvtgrps {
0031 struct EdgeCollection;
0032 class LakosRelation;
0033 class LakosEntity;
0034 } // namespace Codethink::lvtgrps
0035 
0036 namespace Codethink::lvtqtc {
0037 struct EdgeCollection;
0038 class LakosRelation;
0039 class LakosEntity;
0040 } // namespace Codethink::lvtqtc
0041 
0042 namespace Codethink::lvtshr {
0043 
0044 using EdgeCollection = lvtqtc::EdgeCollection;
0045 using LakosRelation = lvtqtc::LakosRelation;
0046 using LakosEntity = lvtqtc::LakosEntity;
0047 
0048 template<typename Relation>
0049 Relation filterEdgeFromCollection(std::vector<Relation>& relations,
0050                                   Relation relation,
0051                                   // This trait should be called only on Qt, because this function needs to delete
0052                                   // the relation if not used.
0053                                   // TODO: this is a hack, should be removed as soon as we can.
0054                                   std::function<void(Relation)> deleter)
0055 // This function removes / deletes a vertex if it already exists on the relation, and returns the same entry.
0056 // if it does not find the relation, returns a nullpointer.
0057 {
0058     for (auto& entry : relations) {
0059         if (relation->from() != entry->from() || relation->to() != entry->to()) {
0060             continue;
0061         }
0062 
0063         /*
0064          * If a relation of type UsesInTheInterface and same
0065          * direction already exists, then don't add the
0066          * UsesInTheImplementation relation
0067          */
0068         if (relation->relationType() == lvtshr::LakosRelationType::UsesInTheImplementation
0069             && entry->relationType() == lvtshr::LakosRelationType::UsesInTheInterface) {
0070             deleter(relation);
0071             return entry;
0072         }
0073 
0074         /*
0075          * If a relation of type UsesInTheImplementation and already
0076          * exists, and a relation of type UsesInTheInterface with the
0077          * same direction is being inserted, then overwrite the
0078          * UsesInTheImplementation relation
0079          */
0080         if (relation->relationType() == lvtshr::LakosRelationType::UsesInTheImplementation
0081             && entry->relationType() == lvtshr::LakosRelationType::UsesInTheInterface) {
0082             deleter(entry);
0083             entry = relation;
0084             return entry;
0085         }
0086 
0087         /*
0088          * Don't add a new relation if a relation of the
0089          * same type and direction already exists
0090          */
0091         if (entry->relationType() == relation->relationType()) {
0092             deleter(relation);
0093             return entry;
0094         }
0095     }
0096     return nullptr;
0097 }
0098 
0099 } // end namespace Codethink::lvtshr
0100 
0101 #endif