source: XIOS/dev/dev_olga/src/declare_ref_func.hpp @ 1204

Last change on this file since 1204 was 1158, checked in by oabramkina, 7 years ago

Two server levels: merging with trunk r1137.
There are bugs.

  • Property svn:executable set to *
File size: 8.4 KB
RevLine 
[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]15public:                                                     \
[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                                                                       \
24private:                                                               \
[941]25  std::vector<C##type*> refObjects;
[540]26
[750]27// Definitions
28
[770]29#define DEFINE_REF_FUNC(type, name_)                                   \
[540]30void 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  }                                                                    \
[1009]53  if (this->hasAttribute("name") && this->name.isEmpty())              \
54    this->name.setValue(this->get##type##OutputName());                \
[750]55}                                                                      \
56                                                                       \
[777]57void C##type::setAttributesReference(bool apply)                       \
[775]58{                                                                      \
[777]59  for (int i = 1; i < refObjects.size(); ++i)                          \
[820]60    refObjects[i]->setAttributes(refObjects[i-1], apply);              \
61  if (refObjects.size() > 1)                                           \
62    refObjects[refObjects.size()-1]->removeRefInheritance();           \
[775]63}                                                                      \
64                                                                       \
[540]65void C##type::removeRefInheritance()                                   \
[750]66{                                                                      \
[770]67  if (!this->name_##_ref.isEmpty())                                    \
68    this->name_##_ref.reset();                                         \
[750]69}                                                                      \
70                                                                       \
[771]71bool C##type::hasDirect##type##Reference(void) const                   \
[750]72{                                                                      \
[1099]73  return (!this->name_##_ref.isEmpty() &&                              \
74          C##type::has(this->name_##_ref));                            \
[750]75}                                                                      \
76                                                                       \
77C##type* C##type::getDirect##type##Reference(void) const               \
78{                                                                      \
[770]79  if (this->name_##_ref.isEmpty())                                     \
[771]80    ERROR("C" #type "* C" #type "::getDirect" #type "Reference(void)", \
81          << "The " #name_ " with id = '" << getId() << "'"            \
82          << " has no " #name_ "_ref.");                               \
[750]83                                                                       \
[770]84  if (!C##type::has(this->name_##_ref))                                \
[752]85    ERROR("C" #type "* C" #type "::getDirect" #type "Reference(void)", \
[770]86          << this->name_##_ref                                         \
87          << " refers to an unknown " #name_ " id.");                  \
[750]88                                                                       \
[770]89  return C##type::get(this->name_##_ref);                              \
[750]90}                                                                      \
91                                                                       \
[962]92const StdString C##type::get##type##OutputName(void) const            \
[770]93{                                                                      \
94  if (!this->name.isEmpty())                                           \
[922]95    return this->name;                                                 \
[921]96  else if (hasDirect##type##Reference())                               \
97  {                                                                    \
98    const C##type* refer_ptr = this, *tmp_ptr;                         \
99    StdString nameRef = this->name_##_ref;                             \
[1009]100    std::set<const C##type*> tmpRefObjects;                            \
[921]101    while (refer_ptr->hasAutoGeneratedId() &&                          \
[922]102          (C##type::has(nameRef)))                                     \
103    {                                                                  \
[962]104      tmpRefObjects.insert(refer_ptr);                                 \
[921]105      tmp_ptr = refer_ptr;                                             \
106      refer_ptr = tmp_ptr->getDirect##type##Reference();               \
[1009]107      if (refer_ptr->hasAutoGeneratedId() &&                           \
108          refer_ptr->hasDirect##type##Reference())                     \
[1158]109        nameRef = refer_ptr->name_##_ref;                              \
[1009]110      else {                                                           \
[1158]111        nameRef = refer_ptr->getId(); break;                           \
112      }                                                                \
113      if (tmpRefObjects.end() != tmpRefObjects.find(refer_ptr))        \
114      {                                                                \
[962]115        ERROR("const StdString& C" #type "::get" #type "OutputName(void) const ",      \
[1158]116              << "Circular dependency stopped for " #name_ " object "  \
117              << "with id = \"" << refer_ptr->getId() << "\".");       \
118      }                                                                \
[922]119    }                                                                  \
[962]120    return nameRef;                                                    \
[921]121  }                                                                    \
[770]122  else                                                                 \
123    return getId();                                                    \
124}                                                                      \
[941]125                                                                       \
126bool C##type::hasRefTo(C##type* ref) const                             \
127{                                                                      \
128  bool found = false;                                                  \
129  for (int idx = 0; idx < refObjects.size(); ++idx)                    \
130    if (ref == refObjects[idx]) { found = true; break; }                \
131  return found;                                                        \
132}
[540]133
134#endif // __XIOS_DECLARE_REF_FUNC_HPP__
Note: See TracBrowser for help on using the repository browser.