source: XIOS/trunk/src/declare_ref_func.hpp @ 878

Last change on this file since 878 was 820, checked in by mhnguyen, 8 years ago

Fixing duplicate *_ref on reading attributes from file

+) Remove *_ref from *_ref root of the reference tree

Test
+) On Curie
+) Test passes

  • Property svn:executable set to *
File size: 5.9 KB
Line 
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_)                       \
15public:                                                     \
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                                                                       \
23private:                                                               \
24  std::vector<C##type*> refObjects;                                    \
25
26// Definitions
27
28#define DEFINE_REF_FUNC(type, name_)                                   \
29void C##type::solveRefInheritance(bool apply)                          \
30{                                                                      \
31  std::set<C##type*> tmpRefObjects;                                    \
32  C##type* refer_ptr = this;                                           \
33  refObjects.push_back(this);                                          \
34                                                                       \
35  while (refer_ptr->hasDirect##type##Reference())                      \
36  {                                                                    \
37    tmpRefObjects.insert(refer_ptr);                                   \
38                                                                       \
39    refer_ptr = refer_ptr->getDirect##type##Reference();               \
40                                                                       \
41    if (tmpRefObjects.end() != tmpRefObjects.find(refer_ptr))          \
42    {                                                                  \
43      ERROR("void C" #type "::solveRefInheritance(bool apply)",        \
44            << "Circular dependency stopped for " #name_ " object "    \
45            << "with id = \"" << refer_ptr->getId() << "\".");         \
46    }                                                                  \
47                                                                       \
48    refObjects.push_back(refer_ptr);                                   \
49    SuperClassAttribute::setAttributes(refer_ptr, apply);              \
50  }                                                                    \
51}                                                                      \
52                                                                       \
53void C##type::setAttributesReference(bool apply)                       \
54{                                                                      \
55  for (int i = 1; i < refObjects.size(); ++i)                          \
56    refObjects[i]->setAttributes(refObjects[i-1], apply);              \
57  if (refObjects.size() > 1)                                           \
58    refObjects[refObjects.size()-1]->removeRefInheritance();           \
59}                                                                      \
60                                                                       \
61void C##type::removeRefInheritance()                                   \
62{                                                                      \
63  if (!this->name_##_ref.isEmpty())                                    \
64    this->name_##_ref.reset();                                         \
65}                                                                      \
66                                                                       \
67bool C##type::hasDirect##type##Reference(void) const                   \
68{                                                                      \
69  return !this->name_##_ref.isEmpty();                                 \
70}                                                                      \
71                                                                       \
72C##type* C##type::getDirect##type##Reference(void) const               \
73{                                                                      \
74  if (this->name_##_ref.isEmpty())                                     \
75    ERROR("C" #type "* C" #type "::getDirect" #type "Reference(void)", \
76          << "The " #name_ " with id = '" << getId() << "'"            \
77          << " has no " #name_ "_ref.");                               \
78                                                                       \
79  if (!C##type::has(this->name_##_ref))                                \
80    ERROR("C" #type "* C" #type "::getDirect" #type "Reference(void)", \
81          << this->name_##_ref                                         \
82          << " refers to an unknown " #name_ " id.");                  \
83                                                                       \
84  return C##type::get(this->name_##_ref);                              \
85}                                                                      \
86                                                                       \
87const StdString& C##type::get##type##OutputName(void) const            \
88{                                                                      \
89  if (!this->name.isEmpty())                                           \
90    return this->name;                                                 \
91  else if (hasAutoGeneratedId() && hasDirect##type##Reference())       \
92    return this->name_##_ref;                                          \
93  else                                                                 \
94    return getId();                                                    \
95}                                                                      \
96
97#endif // __XIOS_DECLARE_REF_FUNC_HPP__
Note: See TracBrowser for help on using the repository browser.