ice_conversions_templates.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 #include <optional>
6 #include <vector>
7 
8 #include <Ice/Handle.h>
9 
10 
11 /**
12  This file offers overloads of toIce() and fromIce() functions for STL
13  container types.
14 
15  To use these overloads for custom types
16  `myns::MyClass` (the C++ class, i.e. the business object (BO)) and
17  `myns::dto::MyClass` (the ice class, i.e. the data transfer object (DTO))
18  (see the `IceConversionsTest for a working example):
19 
20  1. Implement free functions toIce() and fromIce() of the form:
21 
22  In the header (ice_conversions.h):
23  @code
24  namespace myns
25  {
26  void toIce(dto::MyClass& dto, const MyClass& bo);
27  void fromIce(const dto::MyClass& dto, MyClass& bo);
28  }
29  @endcode
30 
31  In the source file (ice_conversions.cpp):
32  @code
33  #include "ice_conversions.h"
34 
35  #include <ArmarXCore/core/ice_conversions.h>
36 
37 
38  using armarx::toIce;
39  using armarx::fromIce;
40 
41  void myns::toIce(dto::MyClass& dto, const MyClass& bo)
42  {
43  toIce(dto.memberA, bo.memberB);
44  toIce(dto.memberB, bo.memberB);
45  }
46  void myns::fromIce(const dto::MyClass& dto, MyClass& bo)
47  {
48  fromIce(dto.memberA, bo.memberA);
49  fromIce(dto.memberB, bo.memberB);
50  }
51  @endcode
52 
53  2. Include the `ice_conversions.h` for `MyClass` as well as the templates
54  (this file, i.e. <ArmarXCore/core/ice_conversions.h>) in the .cpp file
55  where you want to do the conversions.
56 
57  3. Call the conversion functions in these ways:
58 
59  @code
60 
61  void foo()
62  {
63  MyClass bo;
64  dto::MyClass dto;
65 
66  // Works without namespace (via Argument Depdenent Lookup, ADL):
67  toIce(dto, bo);
68  fromIce(dto, bo);
69 
70  // The templates must be explicitly imported into the current namespace
71  // (even when `using namespace armarx`):
72  using armarx::toIce;
73  using armarx::fromIce;
74 
75  bo = fromIce<MyClass>(dto);
76  dto = toIce<dto::MyClass>(bo);
77 
78  std::vector<MyClass> bos;
79  std::vector<dto::MyClass> dtos;
80 
81  fromIce(dtos, bos);
82  toIce(dtos, bos);
83  }
84  @endcode
85  */
86 
87 
88 namespace armarx
89 {
90 
91  // Same type
92 
93  template <class T>
94  void toIce(T& dto, const T& bo);
95  template <class T>
96  void fromIce(const T& dto, T& bo);
97 
98 
99  // General return version.
100 
101  template <class DtoT, class BoT>
102  DtoT toIce(const BoT& bo);
103  template <class BoT, class DtoT>
104  BoT fromIce(const DtoT& dto);
105 
106 
107  // Ice Handle
108 
109  template <class DtoT, class BoT>
110  void toIce(::IceInternal::Handle<DtoT>& dto, const BoT& bo);
111  template <class DtoT, class BoT>
112  void fromIce(const ::IceInternal::Handle<DtoT>& dto, BoT& bo);
113 
114 
115  // std::unique_ptr
116 
117  template <class DtoT, class BoT>
118  void toIce(DtoT& dto, const std::unique_ptr<BoT>& boPointer);
119  template <class DtoT, class BoT>
120  void fromIce(const DtoT& dto, std::unique_ptr<BoT>& boPointer);
121 
122 
123  // Ice Handle <-> std::unique_ptr
124 
125  template <class DtoT, class BoT>
126  void toIce(::IceInternal::Handle<DtoT>& dto, const std::unique_ptr<BoT>& boPointer);
127  template <class DtoT, class BoT>
128  void fromIce(const ::IceInternal::Handle<DtoT>& dto, std::unique_ptr<BoT>& boPointer);
129 
130 
131  // Ice Handle <-> std::optional
132 
133  template <class DtoT, class BoT>
134  void toIce(::IceInternal::Handle<DtoT>& dto, const std::optional<BoT>& bo);
135  template <class DtoT, class BoT>
136  void fromIce(const ::IceInternal::Handle<DtoT>& dto, std::optional<BoT>& bo);
137 
138 
139  // std::vector
140 
141  template <class DtoT, class BoT>
142  void toIce(std::vector<DtoT>& dtos, const std::vector<BoT>& bos);
143  template <class DtoT, class BoT>
144  void fromIce(const std::vector<DtoT>& dtos, std::vector<BoT>& bos);
145 
146 
147  // std::map
148 
149  template <class DtoKeyT, class IceValueT, class BoKeyT, class BoValueT>
150  void toIce(std::map<DtoKeyT, IceValueT>& dtoMap,
151  const std::map<BoKeyT, BoValueT>& boMap);
152  template <class DtoKeyT, class IceValueT, class BoKeyT, class BoValueT>
153  void fromIce(const std::map<DtoKeyT, IceValueT>& dtoMap,
154  std::map<BoKeyT, BoValueT>& boMap);
155 
156 }
157 
158 #include "ice_conversions_templates.tpp"
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::toIce
void toIce(std::map< IceKeyT, IceValueT > &iceMap, const boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:15
armarx::fromIce
void fromIce(const std::map< IceKeyT, IceValueT > &iceMap, boost::container::flat_map< CppKeyT, CppValueT > &cppMap)
Definition: ice_conversions_boost_templates.h:26
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::aron::bo
const std::optional< BoT > & bo
Definition: aron_conversions.h:166