IMP  2.0.1
The Integrative Modeling Platform
link_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/rmf/link_macros.h
3  * \brief macros for display classes
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPRMF_LINK_MACROS_H
9 #define IMPRMF_LINK_MACROS_H
10 #include "links.h"
11 #include "associations.h"
12 #include <IMP/base/Pointer.h>
13 #include <RMF/FileHandle.h>
14 
15 #define IMP_DEFINE_INTERNAL_LINKERS(Name, name,args, cargs, \
16  create_args, create_cargs \
17  ) \
18  namespace { \
19  Name##SaveLink *get_##name##_save_link args { \
20  int index=IMP::rmf::get_save_linker_index(#name); \
21  if (!fh.get_has_associated_data(index)) { \
22  RMF::SetCurrentFrame scf(fh, RMF::ALL_FRAMES); \
23  IMP::rmf::SaveLinkAssociationType psl \
24  = new Name##SaveLink create_args; \
25  IMP::rmf::set_linker(fh, index, psl); \
26  } \
27  IMP::rmf::SaveLinkAssociationType ln \
28  = IMP::rmf::get_save_linker(fh, index); \
29  return dynamic_cast<Name##SaveLink*>(ln.get()); \
30  } \
31  Name##LoadLink *get_##name##_load_link cargs { \
32  int index=IMP::rmf::get_load_linker_index(#name); \
33  if (!fh.get_has_associated_data(index)) { \
34  RMF::SetCurrentFrame scf(fh, RMF::ALL_FRAMES); \
35  IMP::rmf::LoadLinkAssociationType psl \
36  = new Name##LoadLink create_cargs; \
37  IMP::rmf::set_linker(fh, index, psl); \
38  } \
39  IMP::rmf::LoadLinkAssociationType pt \
40  = IMP::rmf::get_load_linker(fh, index); \
41  return dynamic_cast<Name##LoadLink*>(pt.get()); \
42  } \
43  } \
44 
45 // TODO: is there any good reason for the difference between InType(s)
46 // and OutType(s) - probably not, need to consolidate
47 /**
48  declares functions for adding, creating and linking
49  RMF file objects with base calsses of type InType
50 
51  @param Name camelcase nice name to use for all function
52  @param name lowercase nice name
53  @param names plural nice name
54  @param InType type of base class
55  @param InTypes type of base class list
56  @param OutType type of base class, probably same as InType and will
57  be consolidated in the future
58  @param OutTypes type of base class, probably same as OutTypes and will
59  be consolidated in the future
60  @param cargs arguments to create_\#\#name
61  @param link_custom_documentation
62  */
63 #define IMP_DECLARE_LINKERS(Name, name, names, InType, InTypes, \
64  OutType, OutTypes, cargs, \
65  link_custom_documentation) \
66  /** Add objects to the file.
67  \note This does not save a configuration, make sure you use
68  save_frame() to do that. */ \
69  IMPRMFEXPORT void add_##names(RMF::FileHandle fh, \
70  const OutTypes& hs); \
71  /** Add objects to the file under the specified node.
72  \note This does not save a configuration, make sure you use
73  save_frame() to do that. */ \
74 IMPRMFEXPORT void add_##names(RMF::NodeHandle fh, \
75  const OutTypes& hs); \
76 /** Add a single Name object to the RMF file.
77  \note This does not save a configuration, make sure you use
78  save_frame() to do that. */ \
79  IMPRMFEXPORT void add_##name(RMF::FileHandle fh, OutType hs); \
80  /** Create Name objects from the RMF file.
81  \note This does not load a frame. Make sure you call
82  IMP::rmf::load_frame() before using.*/ \
83 IMPRMFEXPORT InTypes create_##names cargs; \
84 /** Link Name objects with the RMF file, possibly overwriting an
85  existing link for loading from the file. This does not alter the
86  object, but will affect the behaviour of functions like load_frame()
87  and save_frame().
88 
89  link_custom_documentation
90 */ \
91  IMPRMFEXPORT void link_##names(RMF::FileConstHandle fh, \
92  const InTypes &hs)
93 
94 
95 // TODO: is there any good reason for the difference between InType(s)
96 // and OutType(s) - probably not, need to consolidate
97 /**
98  Define functions for adding, creating and linking
99  RMF file objects with base calsses of type InType
100 
101  \see IMP_DECLARE_LINKERS()
102 
103  @param Name camelcase nice name to use for all function
104  @param name lowercase nice name
105  @param names plural nice name
106  @param InType type of base class
107  @param InTypes type of base class list
108  @param OutType type of base class, probably same as InType and will
109  be consolidated in the future
110  @param OutTypes type of base class, probably same as OutTypes and will
111  be consolidated in the future
112  @param cargs arguments to create_\#\#name
113  */
114 #define IMP_DEFINE_LINKERS(Name, name, names, InType, InTypes, \
115  OutType, OutTypes,args, cargs, \
116  create_args, create_cargs, create_cargs_from) \
117  IMP_DEFINE_INTERNAL_LINKERS(Name, name, args, cargs, \
118  create_args, create_cargs); \
119  void add_##names(RMF::FileHandle fh, \
120  const OutTypes& hs) { \
121  if (hs.empty()) return; \
122  Name##SaveLink* hsl= get_##name##_save_link(fh); \
123  RMF::SetCurrentFrame scf(fh, RMF::ALL_FRAMES); \
124  hsl->add(fh.get_root_node(), hs); \
125  } \
126  void add_##names(RMF::NodeHandle fh, \
127  const OutTypes& hs) { \
128  if (hs.empty()) return; \
129  Name##SaveLink* hsl= get_##name##_save_link(fh.get_file()); \
130  RMF::SetCurrentFrame scf(fh.get_file(), RMF::ALL_FRAMES); \
131  hsl->add(fh, hs); \
132  } \
133  void add_##name(RMF::FileHandle fh, OutType hs) { \
134  add_##names(fh, OutTypes(1, hs)); \
135  } \
136  InTypes create_##names cargs { \
137  Name##LoadLink* rsl= get_##name##_load_link create_cargs; \
138  InTypes ret; \
139  { \
140  /* to catch the type of everything.*/ \
141  RMF::SetCurrentFrame scf(fh, 0); \
142  ret= rsl->create(fh.get_root_node()); \
143  rsl->load(fh); \
144  } \
145  return ret; \
146  } \
147  void link_##names(RMF::FileConstHandle fh, \
148  const InTypes &hs) { \
149  if(hs.empty()) return; \
150  base::Pointer<Name##LoadLink> pll \
151  = get_##name##_load_link create_cargs_from; \
152  RMF::SetCurrentFrame scf(fh, RMF::ALL_FRAMES); \
153  pll->link(fh.get_root_node(), hs); \
154  }
155 
156 #endif /* IMPRMF_LINK_MACROS_H */