~sethj/ubuntu/wily/unity/fix-for-1445595

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
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2011-2013 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Tim Penhey <tim.penhey@canonical.com>
 *              Marco Trevisan <marco.trevisan@canonical.com>
 */

#ifndef UNITY_VARIANT_H
#define UNITY_VARIANT_H

#include <string>
#include <glib.h>
#include <vector>
#include <map>

namespace unity
{
namespace glib
{

class Variant;
typedef std::map<std::string, Variant> HintsMap;
GHashTable* hashtable_from_hintsmap(HintsMap const& hints);
HintsMap const& hintsmap_from_hashtable(GHashTable* hashtable, HintsMap& hints);

struct StealRef {};

class Variant
{
public:
  Variant();
  Variant(GVariant*);
  Variant(GVariant*, StealRef const&);

  Variant(HintsMap const& hints);
  explicit Variant(std::nullptr_t);
  explicit Variant(std::string const&);
  explicit Variant(const char*);
  explicit Variant(unsigned char);
  explicit Variant(int16_t);
  explicit Variant(uint16_t);
  explicit Variant(int32_t);
  explicit Variant(uint32_t);
  explicit Variant(int64_t);
  explicit Variant(uint64_t);
#if __WORDSIZE != 64
  explicit Variant(long);
  explicit Variant(unsigned long);
#endif
  explicit Variant(bool);
  explicit Variant(double);
  explicit Variant(float);

  Variant(Variant const&);
  ~Variant();

  std::string GetString() const;
  unsigned char GetByte() const;
  int16_t GetInt16() const;
  uint16_t GetUInt16() const;
  int32_t GetInt32() const;
  uint32_t GetUInt32() const;
  int64_t GetInt64() const;
  uint64_t GetUInt64() const;
  bool GetBool() const;
  double GetDouble() const;
  float GetFloat() const;
  Variant GetVariant() const;

  bool ASVToHints(HintsMap& hints) const;

  template <typename T>
  static inline Variant FromVector(std::vector<T> const& values)
  {
    if (values.empty())
      return g_variant_new_array(G_VARIANT_TYPE_VARIANT, nullptr, 0);

    GVariantBuilder builder;
    g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
    for (auto const& value : values)
      g_variant_builder_add_value(&builder, Variant(value));

    return g_variant_builder_end(&builder);
  }

  void swap(Variant&);
  Variant& operator=(GVariant*);
  Variant& operator=(Variant);
  Variant& operator=(HintsMap const&);
  Variant& operator=(std::nullptr_t);
  Variant& operator=(std::string const&);
  Variant& operator=(const char*);
  Variant& operator=(unsigned char);
  Variant& operator=(int16_t);
  Variant& operator=(uint16_t);
  Variant& operator=(int32_t);
  Variant& operator=(uint32_t);
  Variant& operator=(int64_t);
  Variant& operator=(uint64_t);
#if __WORDSIZE != 64
  Variant& operator=(long);
  Variant& operator=(unsigned long);
#endif
  Variant& operator=(bool);
  Variant& operator=(double);
  Variant& operator=(float);
  operator GVariant*() const;
  operator bool() const;

private:
  GVariant* variant_;
};

std::ostream& operator<<(std::ostream &os, GVariant* v);
std::ostream& operator<<(std::ostream &os, Variant const&);

} // glib namespace
} // unity namespace

#endif