Cyclone ISO C++ API Reference Guide
refmacros.hpp
Go to the documentation of this file.
1 #ifndef OMG_DDS_CORE_REFMACROS_HPP_
2 #define OMG_DDS_CORE_REFMACROS_HPP_
3 
4 /* Copyright 2010, Object Management Group, Inc.
5  * Copyright 2010, PrismTech, Corp.
6  * Copyright 2010, Real-Time Innovations, Inc.
7  * All rights reserved.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #include <dds/core/macros.hpp>
23 #include <dds/core/ref_traits.hpp>
24 
26 // Macros
28 
29 #define DECLARE_TYPE_TRAITS(TYPE) \
30  typedef TYPE TYPE##_T;\
31  typedef typename ::dds::core::smart_ptr_traits<TYPE>::ref_type TYPE##_REF_T;\
32  typedef typename ::dds::core::smart_ptr_traits<TYPE>::weak_ref_type TYPE##_WEAK_REF_T;
33 
34 
35 /*
36  * This macro defines all the functions that Reference Types have to implement.
37  */
38 /*
39  *
40  *
41  */
43 // Defines all the types, functions and attributes required for a Reference type
44 // without default ctor.
45 //
46 
47 #define OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE) \
48  public: \
49  typedef typename ::dds::core::smart_ptr_traits< DELEGATE >::ref_type DELEGATE_REF_T; \
50  typedef typename ::dds::core::smart_ptr_traits< DELEGATE >::weak_ref_type DELEGATE_WEAK_REF_T; \
51  \
52  private:\
53  const typename ::dds::core::Reference< DELEGATE >::DELEGATE_REF_T& impl() const \
54  { return ::dds::core::Reference< DELEGATE >::impl_; }\
55  typename ::dds::core::Reference< DELEGATE >::DELEGATE_REF_T& impl() \
56  { return ::dds::core::Reference< DELEGATE >::impl_; }\
57  \
58  public: \
59  typedef BASE< DELEGATE > BASE_T;\
60  \
61  public: \
62  TYPE(const dds::core::null_type&) : dds::core::Reference< DELEGATE >(static_cast<DELEGATE*>(NULL)) { } \
63  \
64  TYPE& operator=(const dds::core::null_type& rhs) {\
65  (void)rhs; \
66  *this = TYPE(dds::core::null); \
67  return *this; \
68  } \
69  \
70  public:\
71  TYPE(const DELEGATE_REF_T& ref) \
72  : dds::core::Reference< DELEGATE >(ref)\
73  { }
74 
75 #define OMG_DDS_IMPLICIT_REF_BASE(TYPE) \
76  public:\
77  template <typename H__> \
78  TYPE(const H__& h) \
79  { \
80  if (h.is_nil()) { \
81  /* We got a null object and are not really able to do a typecheck here. */ \
82  /* So, just set a null object. */ \
83  *this = dds::core::null; \
84  } else { \
85  this->::dds::core::Reference< DELEGATE_T >::impl_ = OSPL_CXX11_STD_MODULE::dynamic_pointer_cast< DELEGATE_T >(h.delegate()); \
86  if (h.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
87  throw dds::core::IllegalOperationError(std::string("Attempted invalid cast: ") + typeid(h).name() + " to " + typeid(*this).name()); \
88  } \
89  } \
90  }\
91  \
92  template <typename T__> \
93  TYPE& \
94  operator=(const T__& rhs) {\
95  if (this != (TYPE*)&rhs) { \
96  if (rhs.is_nil()) { \
97  /* We got a null object and are not really able to do a typecheck here. */ \
98  /* So, just set a null object. */ \
99  *this = dds::core::null; \
100  } else { \
101  TYPE other(rhs); \
102  /* Dont have to copy when the delegate impl is the same. */ \
103  if (other.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
104  *this = other; \
105  } \
106  } \
107  } \
108  return *this; \
109  }
110 
111 #define OMG_DDS_EXPLICIT_REF_BASE_DECL(TYPE, FROM) \
112  public:\
113  TYPE(const FROM& h); \
114  \
115  TYPE& \
116  operator=(const FROM& rhs);
117 
118 #define OMG_DDS_EXPLICIT_REF_BASE(TYPE, FROM) \
119  public:\
120  /* We need to support both const and non-const conversion explicitly, because it could */ \
121  /* be that the class has a constructor that takes a single non-const template argument. */ \
122  /* That non-const templated constructor is a closer match than TYPE(const FROM& h); when */ \
123  /* creating the TYPE class from a non-const FROM object. */ \
124  TYPE(FROM& h) { this->explicit_conversion(const_cast<const FROM&>(h)); } \
125  TYPE(const FROM& h) { this->explicit_conversion(h); } \
126  private:\
127  void explicit_conversion(const FROM& h) \
128  { \
129  if (h.is_nil()) { \
130  /* We got a null object and are not really able to do a typecheck here. */ \
131  /* So, just set a null object. */ \
132  *this = dds::core::null; \
133  } else { \
134  this->::dds::core::Reference<DELEGATE_T>::impl_ = OSPL_CXX11_STD_MODULE::dynamic_pointer_cast<DELEGATE_T>(h.delegate()); \
135  if (h.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
136  throw dds::core::IllegalOperationError(std::string("Attempted invalid cast: ") + typeid(h).name() + " to " + typeid(*this).name()); \
137  } \
138  } \
139  }\
140  \
141  public:\
142  TYPE& \
143  operator=(const FROM& rhs) {\
144  if (this != (TYPE*)&rhs) { \
145  if (rhs.is_nil()) { \
146  /* We got a null object and are not really able to do a typecheck here. */ \
147  /* So, just set a null object. */ \
148  *this = dds::core::null; \
149  } else { \
150  TYPE other(rhs); \
151  /* Dont have to copy when the delegate is the same. */ \
152  if (other.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
153  *this = other; \
154  } \
155  } \
156  } \
157  return *this; \
158  }
159 
160 
162 // Declares a reference type equipped with a default ctor.
163 //
164 #define OMG_DDS_REF_TYPE_PROTECTED_DC(TYPE, BASE, DELEGATE) \
165 public: \
166  typedef DELEGATE DELEGATE_T; \
167  OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE_T) \
168 protected: \
169  TYPE() { }
170 
171 #define OMG_DDS_REF_TYPE_PROTECTED_DC_T(TYPE, BASE, T_PARAM, DELEGATE) \
172 public: \
173  typedef DELEGATE<T_PARAM> DELEGATE_T; \
174  OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE_T) \
175  protected: \
176  TYPE() { }
177 
178 #define OMG_DDS_REF_TYPE_NO_DC(TYPE, BASE, DELEGATE) \
179 public: \
180  typedef DELEGATE DELEGATE_T; \
181  OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE_T)
182 
183 #define OMG_DDS_REF_TYPE_DELEGATE_C(TYPE, BASE, DELEGATE) \
184  OMG_DDS_REF_TYPE_PROTECTED_DC(TYPE, BASE, DELEGATE) \
185  public: \
186  TYPE(DELEGATE_T *impl) : dds::core::Reference< DELEGATE_T >(impl) { }
187 
188 #endif /* OMG_DDS_CORE_REFMACROS_HPP_ */
ref_traits.hpp
macros.hpp