softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontaineragent_dbus_common.h
1 
2 #pragma once
3 #include <iostream>
4 #include "glibmm.h"
5 #include "giomm.h"
6 
7 
9  public:
10  template<typename T>
11  static void unwrapList(std::vector<T> &list, const Glib::VariantContainerBase &wrapped) {
12  for (uint i = 0; i < wrapped.get_n_children (); i++) {
13  Glib::Variant<T> item;
14  wrapped.get_child(item, i);
15  list.push_back(item.get());
16  }
17  }
18 
19  static std::vector<Glib::ustring> stdStringVecToGlibStringVec(const std::vector<std::string> &strv) {
20  std::vector<Glib::ustring> newStrv;
21  for (uint i = 0; i < strv.size(); i++) {
22  newStrv.push_back(strv[i]);
23  }
24 
25  return newStrv;
26  }
27 
28  static std::vector<std::string> glibStringVecToStdStringVec(const std::vector<Glib::ustring> &strv) {
29  std::vector<std::string> newStrv;
30  for (uint i = 0; i < strv.size(); i++) {
31  newStrv.push_back(strv[i]);
32  }
33 
34  return newStrv;
35  }
36 
37  static std::map<std::string, std::string> glibStringMapToStdStringMap(const std::map<Glib::ustring, Glib::ustring> &strm) {
38  std::map<std::string, std::string> newStrm;
39  for (std::pair<Glib::ustring, Glib::ustring> pair : strm) {
40  std::pair<std::string, std::string> newPair(pair.first, pair.second);
41  newStrm.insert(newPair);
42  }
43 
44  return newStrm;
45  }
46 
47  template <typename T0, typename T1>
48  static std::vector<T1> mapVector(const std::vector<T0> &vector0, std::function<T1(T0)> mapFunction) {
49  std::vector<T1> vector1;
50  for (T0 &e : vector0) {
51  vector1.push_back(mapFunction(e));
52  }
53 
54  return vector1;
55  }
56 };
58 public:
59  SoftwareContainerAgentMessageHelper (const Glib::RefPtr<Gio::DBus::MethodInvocation> msg) :
60  m_message(msg) {}
61 
62  const Glib::RefPtr<Gio::DBus::MethodInvocation> getMessage() {
63  return m_message;
64  }
65 
66 void returnValue()
67 {
68  Glib::VariantContainerBase empty;
69  m_message->return_value(empty);
70 }
71 
72 template <typename T0>
73 void returnValue(T0 p0)
74 {
75  std::vector<Glib::VariantBase> vlist;
76  vlist.push_back(Glib::Variant<T0>::create(p0));
77 
78  m_message->return_value(Glib::Variant<Glib::VariantBase>::create_tuple(vlist));
79 }
80 
81 template <typename T0,typename T1>
82 void returnValue(T0 p0, T1 p1)
83 {
84  std::vector<Glib::VariantBase> vlist;
85  vlist.push_back(Glib::Variant<T0>::create(p0));
86  vlist.push_back(Glib::Variant<T1>::create(p1));
87 
88  m_message->return_value(Glib::Variant<Glib::VariantBase>::create_tuple(vlist));
89 }
90 
91 void returnError(const std::string &errorMessage)
92 {
93  Gio::DBus::Error error(Gio::DBus::Error::FAILED, errorMessage);
94  m_message->return_error(error);
95 }
96 
97 private:
98  Glib::RefPtr<Gio::DBus::MethodInvocation> m_message;
99 };