~jpakkane/dbus-cpp/depfix

« back to all changes in this revision

Viewing changes to include/dbus/org/freedesktop/dbus/types/struct.h

  • Committer: Thomas Voß
  • Date: 2013-05-21 09:49:21 UTC
  • Revision ID: thomas.voss@canonical.com-20130521094921-iac4thmiozeshjj7
Initial commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef DBUS_ORG_FREEDESKTOP_DBUS_TYPES_STRUCT_H_
 
19
#define DBUS_ORG_FREEDESKTOP_DBUS_TYPES_STRUCT_H_
 
20
 
 
21
#include "org/freedesktop/dbus/codec.h"
 
22
 
 
23
namespace org
 
24
{
 
25
namespace freedesktop
 
26
{
 
27
namespace dbus
 
28
{
 
29
namespace types
 
30
{
 
31
template<typename T>
 
32
struct Struct
 
33
{
 
34
    T value;
 
35
};
 
36
}
 
37
namespace helper
 
38
{
 
39
template<typename T>
 
40
struct TypeMapper<org::freedesktop::dbus::types::Struct<T>>
 
41
{
 
42
    constexpr static ArgumentType type_value()
 
43
    {
 
44
        return ArgumentType::structure;
 
45
    }
 
46
 
 
47
    constexpr static bool is_basic_type()
 
48
    {
 
49
        return false;
 
50
    }
 
51
 
 
52
    constexpr static bool requires_signature()
 
53
    {
 
54
        return false;
 
55
    }
 
56
 
 
57
    static std::string signature()
 
58
    {
 
59
        static const std::string s =
 
60
            DBUS_STRUCT_BEGIN_CHAR_AS_STRING +
 
61
            TypeMapper<T>::signature() +
 
62
            DBUS_STRUCT_END_CHAR_AS_STRING;
 
63
        return s;
 
64
    }
 
65
};
 
66
}
 
67
 
 
68
template<typename T>
 
69
struct Codec<types::Struct<T>>
 
70
{
 
71
    static void encode_argument(DBusMessageIter* out, const types::Struct<T>& arg)
 
72
    {
 
73
        DBusMessageIter sub;
 
74
        if (!dbus_message_iter_open_container(
 
75
                    out,
 
76
                    DBUS_TYPE_STRUCT,
 
77
                    nullptr,
 
78
                    std::addressof(sub)))
 
79
            throw std::runtime_error("Problem opening container");
 
80
 
 
81
        Codec<T>::encode_argument(std::addressof(sub), arg.value);
 
82
 
 
83
        if (!dbus_message_iter_close_container(out, std::addressof(sub)))
 
84
            throw std::runtime_error("Problem closing container");
 
85
    }
 
86
 
 
87
    static void decode_argument(DBusMessageIter* in, types::Struct<T>& out)
 
88
    {
 
89
        DBusMessageIter sub;
 
90
        dbus_message_iter_recurse(in, std::addressof(sub));
 
91
        Codec<T>::decode_argument(std::addressof(sub), out.value);
 
92
    }
 
93
};
 
94
}
 
95
}
 
96
}
 
97
#endif // DBUS_ORG_FREEDESKTOP_DBUS_TYPES_STRUCT_H_