summaryrefslogtreecommitdiffstats
path: root/xbmc/interfaces/legacy/Alternative.h
blob: a7156882dcbd61ac94585777f94a04fd0de5b3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "Exception.h"

namespace XBMCAddon
{
  enum WhichAlternative { none, first, second };

  template<typename T1, typename T2> class Alternative
  {
  public:
  private:
    WhichAlternative pos = none;
    T1 d1;
    T2 d2;

  public:
    Alternative() = default;

    inline WhichAlternative which() const { return pos; }

    inline T1& former()
    {
      if (pos == second)// first and none is ok
        throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
      if (pos == none)
        d1 = T1();
      pos = first;
      return d1;
    }

    inline const T1& former() const
    {
      if (pos != first)
        throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
      return d1;
    }

    inline T2& later()
    {
      if (pos == first)
        throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
      if (pos == none)
        d2 = T2();
      pos = second;
      return d2;
    }

    inline const T2& later() const
    {
      if (pos != second)
        throw WrongTypeException("Access of XBMCAddon::Alternative as incorrect type");
      return d2;
    }

    inline operator T1& () { return former(); }
    inline operator const T1& () const { return former(); }
    inline operator T2& () { return later(); }
    inline operator const T2& () const { return later(); }
  };
}