OpenSplice ISO C++ 2 DCPS  v6.x
ISO C++ 2 OpenSplice Data Distribution Service Data-Centric Publish-Subscribe API
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  */
39 // Defines all the types, functions and attributes required for a Reference type
40 // without default ctor.
41 //
42 
43 #define OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE) \
44  public: \
45  typedef typename ::dds::core::smart_ptr_traits< DELEGATE >::ref_type DELEGATE_REF_T; \
46  typedef typename ::dds::core::smart_ptr_traits< DELEGATE >::weak_ref_type DELEGATE_WEAK_REF_T; \
47  \
48  private:\
49  const typename ::dds::core::Reference< DELEGATE >::DELEGATE_REF_T& impl() const \
50  { return ::dds::core::Reference< DELEGATE >::impl_; }\
51  typename ::dds::core::Reference< DELEGATE >::DELEGATE_REF_T& impl() \
52  { return ::dds::core::Reference< DELEGATE >::impl_; }\
53  \
54  public: \
55  typedef BASE< DELEGATE > BASE_T;\
56  \
57  public: \
58  TYPE(const dds::core::null_type&) : dds::core::Reference< DELEGATE >(static_cast<DELEGATE*>(NULL)) { } \
59  \
60  TYPE& operator=(const dds::core::null_type& rhs) {\
61  *this = TYPE(dds::core::null); \
62  return *this; \
63  } \
64  \
65  public:\
66  TYPE(const DELEGATE_REF_T& ref) \
67  : dds::core::Reference< DELEGATE >(ref)\
68  { }
69 
70 #define OMG_DDS_IMPLICIT_REF_BASE(TYPE) \
71  public:\
72  template <typename H__> \
73  TYPE(const H__& h) \
74  { \
75  if (h.is_nil()) { \
76  /* We got a null object and are not really able to do a typecheck here. */ \
77  /* So, just set a null object. */ \
78  *this = dds::core::null; \
79  } else { \
80  this->::dds::core::Reference< DELEGATE_T >::impl_ = OSPL_CXX11_STD_MODULE::dynamic_pointer_cast< DELEGATE_T >(h.delegate()); \
81  if (h.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
82  throw dds::core::IllegalOperationError(std::string("Attempted invalid cast: ") + typeid(h).name() + " to " + typeid(*this).name()); \
83  } \
84  } \
85  }\
86  \
87  template <typename T__> \
88  TYPE& \
89  operator=(const T__& rhs) {\
90  if (this != (TYPE*)&rhs) { \
91  if (rhs.is_nil()) { \
92  /* We got a null object and are not really able to do a typecheck here. */ \
93  /* So, just set a null object. */ \
94  *this = dds::core::null; \
95  } else { \
96  TYPE other(rhs); \
97  /* Dont have to copy when the delegate impl is the same. */ \
98  if (other.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
99  *this = other; \
100  } \
101  } \
102  } \
103  return *this; \
104  }
105 
106 #define OMG_DDS_EXPLICIT_REF_BASE_DECL(TYPE, FROM) \
107  public:\
108  TYPE(const FROM& h); \
109  \
110  TYPE& \
111  operator=(const FROM& rhs);
112 
113 #define OMG_DDS_EXPLICIT_REF_BASE(TYPE, FROM) \
114  public:\
115  /* We need to support both const and non-const conversion explicitly, because it could */ \
116  /* be that the class has a constructor that takes a single non-const template argument. */ \
117  /* That non-const templated constructor is a closer match than TYPE(const FROM& h); when */ \
118  /* creating the TYPE class from a non-const FROM object. */ \
119  TYPE(FROM& h) { this->explicit_conversion(const_cast<const FROM&>(h)); } \
120  TYPE(const FROM& h) { this->explicit_conversion(h); } \
121  private:\
122  void explicit_conversion(const FROM& h) \
123  { \
124  if (h.is_nil()) { \
125  /* We got a null object and are not really able to do a typecheck here. */ \
126  /* So, just set a null object. */ \
127  *this = dds::core::null; \
128  } else { \
129  this->::dds::core::Reference<DELEGATE_T>::impl_ = OSPL_CXX11_STD_MODULE::dynamic_pointer_cast<DELEGATE_T>(h.delegate()); \
130  if (h.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
131  throw dds::core::IllegalOperationError(std::string("Attempted invalid cast: ") + typeid(h).name() + " to " + typeid(*this).name()); \
132  } \
133  } \
134  }\
135  \
136  public:\
137  TYPE& \
138  operator=(const FROM& rhs) {\
139  if (this != (TYPE*)&rhs) { \
140  if (rhs.is_nil()) { \
141  /* We got a null object and are not really able to do a typecheck here. */ \
142  /* So, just set a null object. */ \
143  *this = dds::core::null; \
144  } else { \
145  TYPE other(rhs); \
146  /* Dont have to copy when the delegate is the same. */ \
147  if (other.delegate() != this->::dds::core::Reference< DELEGATE_T >::impl_) { \
148  *this = other; \
149  } \
150  } \
151  } \
152  return *this; \
153  }
154 
155 
157 // Declares a reference type equipped with a default ctor.
158 //
159 #define OMG_DDS_REF_TYPE_PROTECTED_DC(TYPE, BASE, DELEGATE) \
160 public: \
161  typedef DELEGATE DELEGATE_T; \
162  OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE_T) \
163 protected: \
164  TYPE() { }
165 
166 #define OMG_DDS_REF_TYPE_PROTECTED_DC_T(TYPE, BASE, T_PARAM, DELEGATE) \
167 public: \
168  typedef DELEGATE<T_PARAM> DELEGATE_T; \
169  OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE_T) \
170  protected: \
171  TYPE() { }
172 
173 #define OMG_DDS_REF_TYPE_NO_DC(TYPE, BASE, DELEGATE) \
174 public: \
175  typedef DELEGATE DELEGATE_T; \
176  OMG_DDS_REF_TYPE_BASE(TYPE, BASE, DELEGATE_T)
177 
178 #define OMG_DDS_REF_TYPE_DELEGATE_C(TYPE, BASE, DELEGATE) \
179  OMG_DDS_REF_TYPE_PROTECTED_DC(TYPE, BASE, DELEGATE) \
180  public: \
181  TYPE(DELEGATE_T *impl) : dds::core::Reference< DELEGATE_T >(impl) { }
182 
183 #endif /* OMG_DDS_CORE_REFMACROS_HPP_ */