fw4spl
core/fwCamp/include/fwCamp/macros.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef __FWCAMP_MACROS_HPP__
8 #define __FWCAMP_MACROS_HPP__
9 
10 #ifndef CAMP_COMPILATION
11 
12 #define fwCampAutoDeclareMacro( desc, export)
13 #define fwCampAutoDeclareDataMacro( desc, export)
14 #define fwCampMakeFriendDataMacro(desc)
15 
16 #else
17 
18 #include <fwCore/macros.hpp>
19 
20 #include <boost/preprocessor/seq/fold_left.hpp>
21 
22 #include <camp/camptype.hpp>
23 #include <camp/class.hpp>
24 #include <camp/enum.hpp>
25 
26 #define __FWCAMP_CAT(_s_, _state_, _elem_) BOOST_PP_CAT(_state_, _elem_)
27 #define __FWCAMP_NAMESPACE_CAT(_s_, _state_, _elem_) _state_::_elem_
28 
29 #define __FWCAMP_FUNC_SUFFIX(desc) \
30  BOOST_PP_SEQ_FOLD_LEFT( __FWCAMP_CAT, BOOST_PP_SEQ_HEAD(desc), BOOST_PP_SEQ_TAIL(desc))
31 
32 #define __FWCAMP_NAMESPACE_NAME(desc) \
33  ::BOOST_PP_SEQ_FOLD_LEFT( __FWCAMP_NAMESPACE_CAT, BOOST_PP_SEQ_HEAD(desc), BOOST_PP_SEQ_TAIL(desc))
34 
35 #define __FWCAMP_DECLARE_FUNC_NAME(desc) BOOST_PP_CAT(fwCampDeclare, __FWCAMP_FUNC_SUFFIX(desc))
36 #define __FWCAMP_DECLARE_LOCAL_FUNC_NAME(desc) BOOST_PP_CAT(localDeclare, __FWCAMP_FUNC_SUFFIX(desc))
37 
38 #define __FWCAMP_CLASS_BUILDER_TYPE(desc) \
39  camp::ClassBuilder < __FWCAMP_NAMESPACE_NAME(desc) >
40 
41 #define __FWCAMP_AUTO_TYPE_NONCOPYABLE(type, registerFunc) \
42  CAMP_TYPE_NONCOPYABLE( type)
43 
44 #define __FWCAMP__AUTO__DECLARE__MACRO(desc) \
45  void __FWCAMP_DECLARE_FUNC_NAME(desc)(__FWCAMP_CLASS_BUILDER_TYPE(desc) &); \
46  inline void __FWCAMP_DECLARE_LOCAL_FUNC_NAME(desc)() \
47  { \
48  __FWCAMP_CLASS_BUILDER_TYPE(desc) builder = \
49  camp::Class::declare< __FWCAMP_NAMESPACE_NAME(desc) >(BOOST_PP_STRINGIZE( __FWCAMP_NAMESPACE_NAME( \
50  desc) )); \
51  __FWCAMP_DECLARE_FUNC_NAME(desc)(builder); \
52  } \
53  __FWCAMP_AUTO_TYPE_NONCOPYABLE(__FWCAMP_NAMESPACE_NAME(desc), __FWCAMP_DECLARE_LOCAL_FUNC_NAME(desc))
54 
55 #define __FWCAMP__REG_NAME(desc) \
56  BOOST_PP_CAT(__FWCAMP_FUNC_SUFFIX(desc), Reg)
57 
58 #define __FWCAMP__USEROBJREG(desc) \
59  ::fwCamp::UserObjectRegistrar < __FWCAMP_NAMESPACE_NAME(desc) >
60 //----------------------------------------------------------------------------
61 
68 #define fwCampMakeFriendDataMacro(desc) \
69  friend void ::__FWCAMP_DECLARE_FUNC_NAME(desc)(__FWCAMP_CLASS_BUILDER_TYPE(desc) &builder); \
70 
71 //----------------------------------------------------------------------------
72 
73 #define fwCampMakeFriendMacro(desc) \
74  friend void ::__FWCAMP_DECLARE_FUNC_NAME(desc)(__FWCAMP_CLASS_BUILDER_TYPE(desc) &builder); \
75 
76 //----------------------------------------------------------------------------
77 
78 #define fwCampDeclareMacro(classname) \
79  CAMP_TYPE_NONCOPYABLE(classname)
80 
81 //----------------------------------------------------------------------------
82 
83 #define fwCampAutoDeclareDataMacro( desc, export) \
84  namespace fwCamp \
85  { \
86  template<typename T> \
87  struct UserObjectRegistrar; \
88  } \
89  fwCorePredeclare(desc) \
90  __FWCAMP__AUTO__DECLARE__MACRO(desc)
91 
92 //----------------------------------------------------------------------------
93 
94 #define fwCampAutoDeclareMacro( desc, export) \
95  fwCorePredeclare(desc) \
96  export __FWCAMP__AUTO__DECLARE__MACRO(desc)
97 
98 //----------------------------------------------------------------------------
99 
100 #define fwCampAutoDeclareEnumMacro(desc) \
101  void __FWCAMP_DECLARE_FUNC_NAME(desc)(camp::EnumBuilder &); \
102  inline void __FWCAMP_DECLARE_LOCAL_FUNC_NAME(desc)() \
103  { \
104  camp::EnumBuilder builder = \
105  camp::Enum::declare< __FWCAMP_NAMESPACE_NAME(desc) >(BOOST_PP_STRINGIZE( __FWCAMP_NAMESPACE_NAME(desc) )); \
106  __FWCAMP_DECLARE_FUNC_NAME(desc)(builder); \
107  } \
108  CAMP_TYPE(__FWCAMP_NAMESPACE_NAME(desc))
109 
110 //----------------------------------------------------------------------------
111 
112 #define fwCampImplementMacro(desc) \
113  void __FWCAMP_DECLARE_FUNC_NAME(desc)(__FWCAMP_CLASS_BUILDER_TYPE(desc) &builder)
114 
115 //----------------------------------------------------------------------------
116 
117 #define fwCampImplementDataMacro(desc) \
118  static __FWCAMP__USEROBJREG(desc) __FWCAMP__REG_NAME(desc)(BOOST_PP_STRINGIZE( __FWCAMP_NAMESPACE_NAME(desc) )); \
119  void __FWCAMP_DECLARE_FUNC_NAME(desc)(__FWCAMP_CLASS_BUILDER_TYPE(desc) &builder)
120 
121 //----------------------------------------------------------------------------
122 
127 #define fwCampImplementEnumMacro(desc) \
128  void __FWCAMP_DECLARE_FUNC_NAME(desc)(camp::EnumBuilder& builder)
129 
135 #define fwCampDeclareAccessor(object, attribut) \
136  namespace camp { \
137  namespace detail { \
138 \
139  template <> \
140  class Accessor1< __FWCAMP_NAMESPACE_NAME(object), SPTR( __FWCAMP_NAMESPACE_NAME(attribut) )& > \
141  { \
142  public: \
143  typedef SPTR ( __FWCAMP_NAMESPACE_NAME (attribut) )& ReturnType; \
144  typedef ObjectTraits< SPTR( __FWCAMP_NAMESPACE_NAME(attribut) )& > Traits; \
145  typedef Traits::DataType DataType; \
146  typedef __FWCAMP_NAMESPACE_NAME (object) ClassType; \
147 \
148  enum \
149  { \
150  canRead = true, \
151  canWrite = true \
152  }; \
153 \
154  template <typename F> \
155  Accessor1(F getter) : \
156  m_getter(getter) \
157  { \
158  } \
159 \
160  ReturnHelper< ReturnType>::Type get(ClassType& objectptr) const \
161  { \
162  ReturnType ptr = m_getter(objectptr); \
163  return ReturnHelper< ReturnType>::get(ptr); \
164  } \
165 \
166  bool set(ClassType& objectptr, const Value &value) const \
167  { \
168  ReturnType ptr = m_getter(objectptr); \
169  ptr = __FWCAMP_NAMESPACE_NAME(attribut) ::dynamicCast(((value.to< DataType* >()))->getSptr()); \
170  return true; \
171  } \
172 \
173  private: \
174 \
175  boost::function< ReturnType(ClassType&)> m_getter; \
176  }; \
177 \
178  } \
179  }
180 
181 #endif
182 
183 #endif // __FWCAMP_MACROS_HPP__
184 
This file defines fwCore base macros.