Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
from-fdstore-visitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jan Olszak (j.olszak@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef CARGO_FD_FROM_FDSTORE_VISITOR_HPP
26 #define CARGO_FD_FROM_FDSTORE_VISITOR_HPP
27 
28 #include "cargo-fd/fdstore.hpp"
29 #include "cargo/is-visitable.hpp"
30 #include "cargo/types.hpp"
31 #include "cargo/visit-fields.hpp"
32 
33 #include <array>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 namespace cargo {
39 
41 public:
42  explicit FromFDStoreVisitor(int fd)
43  : mStore(fd)
44  {
45  }
46 
47  FromFDStoreVisitor(const FromFDStoreVisitor&) = default;
48 
50 
51  template<typename T>
52  void visit(const std::string&, T& value)
53  {
54  readInternal(value);
55  }
56 
57 private:
59 
60  void readInternal(std::string& value)
61  {
62  size_t size;
63  readInternal(size);
64  value.resize(size);
65  mStore.read(&value.front(), size);
66  }
67 
68  void readInternal(char* &value)
69  {
70  size_t size;
71  readInternal(size);
72 
73  value = new char[size + 1];
74  mStore.read(value, size);
75  value[size] = '\0';
76  }
77 
79  {
80  fd = mStore.receiveFD();
81  }
82 
83  template<typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
84  void readInternal(T& value)
85  {
86  mStore.read(&value, sizeof(T));
87  }
88 
89  template<typename T, typename std::enable_if<isVisitable<T>::value, int>::type = 0>
90  void readInternal(T& value)
91  {
92  FromFDStoreVisitor visitor(*this);
93  value.accept(visitor);
94  }
95 
96  template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
97  void readInternal(T& value)
98  {
99  readInternal(*reinterpret_cast<typename std::underlying_type<T>::type*>(&value));
100  }
101 
102  template<typename T>
103  void readInternal(std::vector<T>& values)
104  {
105  size_t vectorSize;
106  readInternal(vectorSize);
107  values.resize(vectorSize);
108 
109  for (T& value : values) {
110  readInternal(value);
111  }
112  }
113 
114  template<typename T, std::size_t N>
115  void readInternal(std::array<T, N>& values)
116  {
117  for (T& value : values) {
118  readInternal(value);
119  }
120  }
121 
122  template<typename V>
123  void readInternal(std::map<std::string, V>& values)
124  {
125  size_t mapSize;
126  readInternal(mapSize);
127 
128  for (size_t i = 0; i < mapSize; ++i) {
129  std::pair<std::string, V> val;
130  readInternal(val);
131  values.insert(std::move(val));
132  }
133  }
134 
135  template<typename T, typename std::enable_if<isLikeTuple<T>::value, int>::type = 0>
136  void readInternal(T& values)
137  {
138  visitFields(values, this, std::string());
139  }
140 };
141 
142 } // namespace cargo
143 
144 #endif // CARGO_FD_FROM_FDSTORE_VISITOR_HPP
Definition: fdstore.hpp:36
void readInternal(char *&value)
Definition: from-fdstore-visitor.hpp:68
Internal configuration helper.
void visit(const std::string &, T &value)
Definition: from-fdstore-visitor.hpp:52
void readInternal(std::vector< T > &values)
Definition: from-fdstore-visitor.hpp:103
void readInternal(T &value)
Definition: from-fdstore-visitor.hpp:84
void readInternal(std::string &value)
Definition: from-fdstore-visitor.hpp:60
Whenever possible, this type will be serialized using Linux file descriptor passing.
Definition: types.hpp:33
Helper function for iterating tuples, pairs and arrays.
void readInternal(std::array< T, N > &values)
Definition: from-fdstore-visitor.hpp:115
void readInternal(cargo::FileDescriptor &fd)
Definition: from-fdstore-visitor.hpp:78
Types declarations.
Definition: from-fdstore-visitor.hpp:40
void read(void *bufferPtr, const size_t size, const unsigned int timeoutMS=maxTimeout)
Reads a value of the given type.
Definition: fdstore.cpp:134
FDStore mStore
Definition: from-fdstore-visitor.hpp:58
void readInternal(T &values)
Definition: from-fdstore-visitor.hpp:136
FromFDStoreVisitor(int fd)
Definition: from-fdstore-visitor.hpp:42
void readInternal(std::map< std::string, V > &values)
Definition: from-fdstore-visitor.hpp:123
Declaration of a class for writing and reading data from a file descriptor.
int receiveFD(const unsigned int timeoutMS=maxTimeout)
Definition: fdstore.cpp:228
FromFDStoreVisitor & operator=(const FromFDStoreVisitor &)=delete
void visitFields(T &t, F f, A...args)
Definition: visit-fields.hpp:58