[540] | 1 | /*! |
---|
| 2 | \file declare_ref_func.hpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \date 02 Dec 2014 |
---|
| 5 | \since 02 Dec 2014 |
---|
| 6 | |
---|
[750] | 7 | \brief Macros to add functions used to solve references in class CDomain, CAxis and CField |
---|
[540] | 8 | */ |
---|
| 9 | #ifndef __XIOS_DECLARE_REF_FUNC_HPP__ |
---|
| 10 | #define __XIOS_DECLARE_REF_FUNC_HPP__ |
---|
| 11 | |
---|
[750] | 12 | // Declarations |
---|
[540] | 13 | |
---|
[770] | 14 | #define DECLARE_REF_FUNC(type, name_) \ |
---|
[750] | 15 | public: \ |
---|
[771] | 16 | void solveRefInheritance(bool apply = true); \ |
---|
| 17 | void removeRefInheritance(); \ |
---|
[750] | 18 | bool hasDirect##type##Reference(void) const; \ |
---|
| 19 | C##type* getDirect##type##Reference(void) const; \ |
---|
[962] | 20 | const StdString get##type##OutputName(void) const; \ |
---|
[922] | 21 | void setAttributesReference(bool apply = true); \ |
---|
[941] | 22 | bool hasRefTo(C##type* ref) const; \ |
---|
[777] | 23 | \ |
---|
| 24 | private: \ |
---|
[941] | 25 | std::vector<C##type*> refObjects; |
---|
[540] | 26 | |
---|
[750] | 27 | // Definitions |
---|
| 28 | |
---|
[770] | 29 | #define DEFINE_REF_FUNC(type, name_) \ |
---|
[540] | 30 | void C##type::solveRefInheritance(bool apply) \ |
---|
[750] | 31 | { \ |
---|
[777] | 32 | std::set<C##type*> tmpRefObjects; \ |
---|
[750] | 33 | C##type* refer_ptr = this; \ |
---|
[941] | 34 | std::vector<C##type*>().swap(refObjects); \ |
---|
[777] | 35 | refObjects.push_back(this); \ |
---|
[750] | 36 | \ |
---|
| 37 | while (refer_ptr->hasDirect##type##Reference()) \ |
---|
| 38 | { \ |
---|
[777] | 39 | tmpRefObjects.insert(refer_ptr); \ |
---|
[751] | 40 | \ |
---|
[750] | 41 | refer_ptr = refer_ptr->getDirect##type##Reference(); \ |
---|
| 42 | \ |
---|
[777] | 43 | if (tmpRefObjects.end() != tmpRefObjects.find(refer_ptr)) \ |
---|
[750] | 44 | { \ |
---|
[751] | 45 | ERROR("void C" #type "::solveRefInheritance(bool apply)", \ |
---|
[770] | 46 | << "Circular dependency stopped for " #name_ " object " \ |
---|
[751] | 47 | << "with id = \"" << refer_ptr->getId() << "\"."); \ |
---|
[750] | 48 | } \ |
---|
| 49 | \ |
---|
[777] | 50 | refObjects.push_back(refer_ptr); \ |
---|
[750] | 51 | SuperClassAttribute::setAttributes(refer_ptr, apply); \ |
---|
| 52 | } \ |
---|
| 53 | } \ |
---|
| 54 | \ |
---|
[777] | 55 | void C##type::setAttributesReference(bool apply) \ |
---|
[775] | 56 | { \ |
---|
[777] | 57 | for (int i = 1; i < refObjects.size(); ++i) \ |
---|
[820] | 58 | refObjects[i]->setAttributes(refObjects[i-1], apply); \ |
---|
| 59 | if (refObjects.size() > 1) \ |
---|
| 60 | refObjects[refObjects.size()-1]->removeRefInheritance(); \ |
---|
[775] | 61 | } \ |
---|
| 62 | \ |
---|
[540] | 63 | void C##type::removeRefInheritance() \ |
---|
[750] | 64 | { \ |
---|
[770] | 65 | if (!this->name_##_ref.isEmpty()) \ |
---|
| 66 | this->name_##_ref.reset(); \ |
---|
[750] | 67 | } \ |
---|
| 68 | \ |
---|
[771] | 69 | bool C##type::hasDirect##type##Reference(void) const \ |
---|
[750] | 70 | { \ |
---|
[771] | 71 | return !this->name_##_ref.isEmpty(); \ |
---|
[750] | 72 | } \ |
---|
| 73 | \ |
---|
| 74 | C##type* C##type::getDirect##type##Reference(void) const \ |
---|
| 75 | { \ |
---|
[770] | 76 | if (this->name_##_ref.isEmpty()) \ |
---|
[771] | 77 | ERROR("C" #type "* C" #type "::getDirect" #type "Reference(void)", \ |
---|
| 78 | << "The " #name_ " with id = '" << getId() << "'" \ |
---|
| 79 | << " has no " #name_ "_ref."); \ |
---|
[750] | 80 | \ |
---|
[770] | 81 | if (!C##type::has(this->name_##_ref)) \ |
---|
[752] | 82 | ERROR("C" #type "* C" #type "::getDirect" #type "Reference(void)", \ |
---|
[770] | 83 | << this->name_##_ref \ |
---|
| 84 | << " refers to an unknown " #name_ " id."); \ |
---|
[750] | 85 | \ |
---|
[770] | 86 | return C##type::get(this->name_##_ref); \ |
---|
[750] | 87 | } \ |
---|
| 88 | \ |
---|
[962] | 89 | const StdString C##type::get##type##OutputName(void) const \ |
---|
[770] | 90 | { \ |
---|
| 91 | if (!this->name.isEmpty()) \ |
---|
[922] | 92 | return this->name; \ |
---|
[921] | 93 | else if (hasDirect##type##Reference()) \ |
---|
| 94 | { \ |
---|
| 95 | const C##type* refer_ptr = this, *tmp_ptr; \ |
---|
| 96 | StdString nameRef = this->name_##_ref; \ |
---|
[962] | 97 | std::set<const C##type*> tmpRefObjects; \ |
---|
[921] | 98 | while (refer_ptr->hasAutoGeneratedId() && \ |
---|
[922] | 99 | (C##type::has(nameRef))) \ |
---|
| 100 | { \ |
---|
[962] | 101 | tmpRefObjects.insert(refer_ptr); \ |
---|
[921] | 102 | tmp_ptr = refer_ptr; \ |
---|
| 103 | refer_ptr = tmp_ptr->getDirect##type##Reference(); \ |
---|
[922] | 104 | if (refer_ptr->hasDirect##type##Reference()) \ |
---|
| 105 | nameRef = refer_ptr->name_##_ref; \ |
---|
[962] | 106 | if (tmpRefObjects.end() != tmpRefObjects.find(refer_ptr)) \ |
---|
| 107 | { \ |
---|
| 108 | ERROR("const StdString& C" #type "::get" #type "OutputName(void) const ", \ |
---|
| 109 | << "Circular dependency stopped for " #name_ " object " \ |
---|
| 110 | << "with id = \"" << refer_ptr->getId() << "\"."); \ |
---|
| 111 | } \ |
---|
[922] | 112 | } \ |
---|
[962] | 113 | return nameRef; \ |
---|
[921] | 114 | } \ |
---|
[770] | 115 | else \ |
---|
| 116 | return getId(); \ |
---|
| 117 | } \ |
---|
[941] | 118 | \ |
---|
| 119 | bool C##type::hasRefTo(C##type* ref) const \ |
---|
| 120 | { \ |
---|
| 121 | bool found = false; \ |
---|
| 122 | for (int idx = 0; idx < refObjects.size(); ++idx) \ |
---|
| 123 | if (ref == refObjects[idx]) { found = true; break; } \ |
---|
| 124 | return found; \ |
---|
| 125 | } |
---|
[540] | 126 | |
---|
| 127 | #endif // __XIOS_DECLARE_REF_FUNC_HPP__ |
---|