OSSIA
Open Scenario System for Interactive Application
behavior_variant_impl.hpp
1 
2 template <>
3 inline const std::shared_ptr<ossia::curve_abstract>*
4 behavior_variant_type::target() const
5 {
6  if(m_type == Type0)
7  return &m_impl.m_value0;
8  return nullptr;
9 }
10 template <>
11 inline const std::vector<ossia::behavior>* behavior_variant_type::target() const
12 {
13  if(m_type == Type1)
14  return &m_impl.m_value1;
15  return nullptr;
16 }
17 template <>
18 inline std::shared_ptr<ossia::curve_abstract>* behavior_variant_type::target()
19 {
20  if(m_type == Type0)
21  return &m_impl.m_value0;
22  return nullptr;
23 }
24 template <>
25 inline std::vector<ossia::behavior>* behavior_variant_type::target()
26 {
27  if(m_type == Type1)
28  return &m_impl.m_value1;
29  return nullptr;
30 }
31 template <>
32 inline const std::shared_ptr<ossia::curve_abstract>& behavior_variant_type::get() const
33 {
34  if(m_type == Type0)
35  return m_impl.m_value0;
36  throw std::runtime_error("behavior_variant: bad type");
37 }
38 template <>
39 inline const std::vector<ossia::behavior>& behavior_variant_type::get() const
40 {
41  if(m_type == Type1)
42  return m_impl.m_value1;
43  throw std::runtime_error("behavior_variant: bad type");
44 }
45 template <>
46 inline std::shared_ptr<ossia::curve_abstract>& behavior_variant_type::get()
47 {
48  if(m_type == Type0)
49  return m_impl.m_value0;
50  throw std::runtime_error("behavior_variant: bad type");
51 }
52 template <>
53 inline std::vector<ossia::behavior>& behavior_variant_type::get()
54 {
55  if(m_type == Type1)
56  return m_impl.m_value1;
57  throw std::runtime_error("behavior_variant: bad type");
58 }
59 template <typename Visitor>
60 auto apply_nonnull(Visitor&& functor, const behavior_variant_type& var)
61 {
62  switch(var.m_type)
63  {
64  case behavior_variant_type::Type::Type0:
65  return functor(var.m_impl.m_value0);
66  case behavior_variant_type::Type::Type1:
67  return functor(var.m_impl.m_value1);
68  default:
69  throw std::runtime_error("behavior_variant: bad type");
70  }
71 }
72 template <typename Visitor>
73 auto apply_nonnull(Visitor&& functor, behavior_variant_type& var)
74 {
75  switch(var.m_type)
76  {
77  case behavior_variant_type::Type::Type0:
78  return functor(var.m_impl.m_value0);
79  case behavior_variant_type::Type::Type1:
80  return functor(var.m_impl.m_value1);
81  default:
82  throw std::runtime_error("behavior_variant: bad type");
83  }
84 }
85 template <typename Visitor>
86 auto apply_nonnull(Visitor&& functor, behavior_variant_type&& var)
87 {
88  switch(var.m_type)
89  {
90  case behavior_variant_type::Type::Type0:
91  return functor(std::move(var.m_impl.m_value0));
92  case behavior_variant_type::Type::Type1:
93  return functor(std::move(var.m_impl.m_value1));
94  default:
95  throw std::runtime_error("behavior_variant: bad type");
96  }
97 }
98 template <typename Visitor>
99 auto apply(Visitor&& functor, const behavior_variant_type& var)
100 {
101  switch(var.m_type)
102  {
103  case behavior_variant_type::Type::Type0:
104  return functor(var.m_impl.m_value0);
105  case behavior_variant_type::Type::Type1:
106  return functor(var.m_impl.m_value1);
107  default:
108  return functor();
109  }
110 }
111 template <typename Visitor>
112 auto apply(Visitor&& functor, behavior_variant_type& var)
113 {
114  switch(var.m_type)
115  {
116  case behavior_variant_type::Type::Type0:
117  return functor(var.m_impl.m_value0);
118  case behavior_variant_type::Type::Type1:
119  return functor(var.m_impl.m_value1);
120  default:
121  return functor();
122  }
123 }
124 template <typename Visitor>
125 auto apply(Visitor&& functor, behavior_variant_type&& var)
126 {
127  switch(var.m_type)
128  {
129  case behavior_variant_type::Type::Type0:
130  return functor(std::move(var.m_impl.m_value0));
131  case behavior_variant_type::Type::Type1:
132  return functor(std::move(var.m_impl.m_value1));
133  default:
134  return functor();
135  }
136 }
137 
138 inline void behavior_variant_type::destruct_impl()
139 {
140  switch(m_type)
141  {
142  case Type::Type0:
143  m_impl.m_value0.~shared_ptr();
144  break;
145  case Type::Type1:
146  m_impl.m_value1.~vector();
147  break;
148  default:
149  break;
150  }
151 }
152 
153 inline int behavior_variant_type::which() const
154 {
155  return m_type;
156 }
157 
158 inline behavior_variant_type::operator bool() const
159 {
160  return m_type != npos;
161 }
162 
163 inline behavior_variant_type::behavior_variant_type()
164  : m_type{Npos}
165 {
166 }
167 
168 inline behavior_variant_type::~behavior_variant_type()
169 {
170  destruct_impl();
171 }
172 
173 inline behavior_variant_type::behavior_variant_type(
174  const std::shared_ptr<ossia::curve_abstract>& v)
175  : m_type{Type0}
176 {
177  new(&m_impl.m_value0) std::shared_ptr<ossia::curve_abstract>{v};
178 }
179 
180 inline behavior_variant_type::behavior_variant_type(
181  std::shared_ptr<ossia::curve_abstract>&& v)
182  : m_type{Type0}
183 {
184  new(&m_impl.m_value0) std::shared_ptr<ossia::curve_abstract>{std::move(v)};
185 }
186 
187 inline behavior_variant_type::behavior_variant_type(
188  const std::vector<ossia::behavior>& v)
189  : m_type{Type1}
190 {
191  new(&m_impl.m_value1) std::vector<ossia::behavior>{v};
192 }
193 
194 inline behavior_variant_type::behavior_variant_type(std::vector<ossia::behavior>&& v)
195  : m_type{Type1}
196 {
197  new(&m_impl.m_value1) std::vector<ossia::behavior>{std::move(v)};
198 }
199 
200 inline behavior_variant_type::behavior_variant_type(const behavior_variant_type& other)
201  : m_type{other.m_type}
202 {
203  switch(m_type)
204  {
205  case Type::Type0:
206  new(&m_impl.m_value0)
207  std::shared_ptr<ossia::curve_abstract>{other.m_impl.m_value0};
208  break;
209  case Type::Type1:
210  new(&m_impl.m_value1) std::vector<ossia::behavior>{other.m_impl.m_value1};
211  break;
212  default:
213  break;
214  }
215 }
216 
217 inline behavior_variant_type::behavior_variant_type(
218  behavior_variant_type&& other) noexcept
219  : m_type{other.m_type}
220 {
221  switch(m_type)
222  {
223  case Type::Type0:
224  new(&m_impl.m_value0)
225  std::shared_ptr<ossia::curve_abstract>{std::move(other.m_impl.m_value0)};
226  break;
227  case Type::Type1:
228  new(&m_impl.m_value1)
229  std::vector<ossia::behavior>{std::move(other.m_impl.m_value1)};
230  break;
231  default:
232  break;
233  }
234 }
235 
236 inline behavior_variant_type&
237 behavior_variant_type::operator=(const behavior_variant_type& other)
238 {
239  destruct_impl();
240  m_type = other.m_type;
241  switch(m_type)
242  {
243  case Type::Type0:
244  new(&m_impl.m_value0)
245  std::shared_ptr<ossia::curve_abstract>{other.m_impl.m_value0};
246  break;
247  case Type::Type1:
248  new(&m_impl.m_value1) std::vector<ossia::behavior>{other.m_impl.m_value1};
249  break;
250  default:
251  break;
252  }
253  return *this;
254 }
255 
256 inline behavior_variant_type&
257 behavior_variant_type::operator=(behavior_variant_type&& other) noexcept
258 {
259  destruct_impl();
260  m_type = other.m_type;
261  switch(m_type)
262  {
263  case Type::Type0:
264  new(&m_impl.m_value0)
265  std::shared_ptr<ossia::curve_abstract>{std::move(other.m_impl.m_value0)};
266  break;
267  case Type::Type1:
268  new(&m_impl.m_value1)
269  std::vector<ossia::behavior>{std::move(other.m_impl.m_value1)};
270  break;
271  default:
272  break;
273  }
274  return *this;
275 }