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