Relations.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_RELATIONS_H
  13. #define SE_INCL_RELATIONS_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Lists.h>
  18. // Object representing a link at the member of relation domain.
  19. class CRelationSrc : public CListHead {
  20. public:
  21. // implementation:
  22. // interface:
  23. // Construction/destruction.
  24. ENGINE_API CRelationSrc(void);
  25. ENGINE_API ~CRelationSrc(void);
  26. void Clear(void);
  27. };
  28. // Object representing a link at the member of relation codomain.
  29. class CRelationDst : public CListHead {
  30. public:
  31. // implementation:
  32. // interface:
  33. // Construction/destruction.
  34. ENGINE_API CRelationDst(void);
  35. ENGINE_API ~CRelationDst(void);
  36. void Clear(void);
  37. };
  38. // Object representing a link between a domain member and a codomain member.
  39. class CRelationLnk {
  40. public:
  41. // implementation:
  42. CRelationSrc *rl_prsSrc; // domain member
  43. CRelationDst *rl_prdDst; // codomain member
  44. CListNode rl_lnSrc; // node in list of links in domain member
  45. CListNode rl_lnDst; // node in list of links in codomain member
  46. // interface:
  47. // Construction/destruction.
  48. CRelationLnk(void);
  49. ~CRelationLnk(void);
  50. // Get the domain member of this pair.
  51. ENGINE_API CRelationSrc &GetSrc(void);
  52. // Get the codomain member of this pair.
  53. ENGINE_API CRelationDst &GetDst(void);
  54. };
  55. // Global functions for creating relations.
  56. void ENGINE_API AddRelationPair(CRelationSrc &rsSrc, CRelationDst &rdDst);
  57. void ENGINE_API AddRelationPairTailTail(CRelationSrc &rsSrc, CRelationDst &rdDst);
  58. void ENGINE_API AddRelationPairHeadHead(CRelationSrc &rsSrc, CRelationDst &rdDst);
  59. // make 'for' construct for walking a list in domain member
  60. #define FOREACHSRCLINK(head, iter) \
  61. for ( LISTITER(CRelationLnk, rl_lnSrc) iter(head); !iter.IsPastEnd(); iter.MoveToNext() )
  62. // make 'for' construct for walking a list in codomain member
  63. #define FOREACHDSTLINK(head, iter) \
  64. for ( LISTITER(CRelationLnk, rl_lnDst) iter(head); !iter.IsPastEnd(); iter.MoveToNext() )
  65. // get a domain member related to a codomain member through a link
  66. #define DST(plink, dstclass, dstmember) \
  67. ( (dstclass *) ( ((UBYTE *)(&(plink->GetDst()))) - offsetof(dstclass, dstmember) ) )
  68. // get a codomain member that a domain member is related to through a link
  69. #define SRC(plink, srcclass, srcmember) \
  70. ( (srcclass *) ( ((UBYTE *)(&(plink->GetSrc()))) - offsetof(srcclass, srcmember) ) )
  71. // make 'for' construct for walking all codomain members related to a domain member
  72. #define FOREACHDSTOFSRC(srchead, dstclass, dstmember, pdst) \
  73. FOREACHSRCLINK(srchead, pdst##_iter) { \
  74. dstclass *pdst = DST(pdst##_iter, dstclass, dstmember);
  75. // make 'for' construct for walking all domain members related to a codomain member
  76. #define FOREACHSRCOFDST(dsthead, srcclass, srcmember, psrc) \
  77. FOREACHDSTLINK(dsthead, psrc##_iter) { \
  78. srcclass *psrc = SRC(psrc##_iter, srcclass, srcmember);
  79. #define ENDFOR }
  80. #endif /* include-once check. */