summary refs log tree commit diff stats
path: root/lib/binding.cpp
blob: 349cd6fbe0f5c519e81f0b7fb51b7cd259f4e82f (plain) (blame)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "binding.h"
#include <stdexcept>
#include <utility>

namespace verbly {

  binding::binding(const binding& other)
  {
    type_ = other.type_;

    switch (type_)
    {
      case type::integer:
      {
        integer_ = other.integer_;

        break;
      }

      case type::string:
      {
        new(&string_) std::string(other.string_);

        break;
      }

      case type::invalid:
      {
        break;
      }
    }
  }

  binding::binding(binding&& other) : binding()
  {
    swap(*this, other);
  }

  binding& binding::operator=(binding other)
  {
    swap(*this, other);

    return *this;
  }

  void swap(binding& first, binding& second)
  {
    using type = binding::type;

    type tempType = first.type_;
    int tempInteger;
    std::string tempString;

    switch (first.type_)
    {
      case type::integer:
      {
        tempInteger = first.integer_;

        break;
      }

      case type::string:
      {
        tempString = std::move(tempString);

        break;
      }

      case type::invalid:
      {
        break;
      }
    }

    first.~binding();

    first.type_ = second.type_;

    switch (second.type_)
    {
      case type::integer:
      {
        first.integer_ = second.integer_;

        break;
      }

      case type::string:
      {
        new(&first.string_) std::string(std::move(second.string_));

        break;
      }

      case type::invalid:
      {
        break;
      }
    }

    second.~binding();

    second.type_ = tempType;

    switch (tempType)
    {
      case type::integer:
      {
        second.integer_ = tempInteger;

        break;
      }

      case type::string:
      {
        new(&second.string_) std::string(std::move(tempString));

        break;
      }

      case type::invalid:
      {
        break;
      }
    }
  }

  binding::~binding()
  {
    switch (type_)
    {
      case type::string:
      {
        using string_type = std::string;
        string_.~string_type();

        break;
      }

      case type::integer:
      case type::invalid:
      {
        break;
      }
    }
  }

  binding::binding(int arg) :
    type_(type::integer),
    integer_(arg)
  {
  }

  int binding::getInteger() const
  {
    if (type_ != type::integer)
    {
      throw std::domain_error("binding::getInteger called on non-integer binding");
    }

    return integer_;
  }

  binding::binding(std::string arg) : type_(type::string)
  {
    new(&string_) std::string(arg);
  }

  std::string binding::getString() const
  {
    if (type_ != type::string)
    {
      throw std::domain_error("binding::getString called on non-string binding");
    }

    return string_;
  }

};