kodi
Alternative.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "Exception.h"
12 
13 namespace XBMCAddon
14 {
15  enum WhichAlternative { none, first, second };
16 
17  template<typename T1, typename T2> class Alternative
18  {
19  public:
20  private:
21  WhichAlternative pos = none;
22  T1 d1;
23  T2 d2;
24 
25  public:
26  Alternative() = default;
27 
28  inline WhichAlternative which() const { return pos; }
29 
30  inline T1& former()
31  {
32  if (pos == second)// first and none is ok
33  throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
34  if (pos == none)
35  d1 = T1();
36  pos = first;
37  return d1;
38  }
39 
40  inline const T1& former() const
41  {
42  if (pos != first)
43  throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
44  return d1;
45  }
46 
47  inline T2& later()
48  {
49  if (pos == first)
50  throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
51  if (pos == none)
52  d2 = T2();
53  pos = second;
54  return d2;
55  }
56 
57  inline const T2& later() const
58  {
59  if (pos != second)
60  throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
61  return d2;
62  }
63 
64  inline operator T1& () { return former(); }
65  inline operator const T1& () const { return former(); }
66  inline operator T2& () { return later(); }
67  inline operator const T2& () const { return later(); }
68  };
69 }
70 
Definition: ThreadsTest1.cpp:132
Defining LOG_LIFECYCLE_EVENTS will log all instantiations, deletions and also reference countings (in...
Definition: Addon.cpp:25
Definition: LibInputPointer.h:13
Definition: Alternative.h:17