Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
to-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_TO_FDSTORE_VISITOR_HPP
26 #define CARGO_FD_TO_FDSTORE_VISITOR_HPP
27 
28 #include "cargo-fd/fdstore.hpp"
29 #include "cargo/is-visitable.hpp"
30 #include "cargo/is-like-tuple.hpp"
31 #include "cargo/types.hpp"
32 #include "cargo/visit-fields.hpp"
33 
34 #include <array>
35 #include <cstring>
36 #include <string>
37 #include <vector>
38 #include <map>
39 #include <utility>
40 
41 
42 namespace cargo {
43 
45 
46 public:
48  : mStore(fd)
49  {
50  }
51 
52  ToFDStoreVisitor(const ToFDStoreVisitor&) = default;
53 
55 
56  template<typename T>
57  void visit(const std::string&, const T& value)
58  {
59  writeInternal(value);
60  }
61 
62 private:
64 
65  void writeInternal(const std::string& value)
66  {
67  writeInternal(value.size());
68  mStore.write(value.c_str(), value.size());
69  }
70 
71  void writeInternal(const char* &value)
72  {
73  size_t size = std::strlen(value);
74  writeInternal(size);
75  mStore.write(value, size);
76  }
77 
79  {
80  mStore.sendFD(fd.value);
81  }
82 
83  template<typename T, typename std::enable_if<std::is_enum<T>::value, int>::type = 0>
84  void writeInternal(const T& value)
85  {
86  writeInternal(static_cast<const typename std::underlying_type<T>::type>(value));
87  }
88 
89  template<typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
90  void writeInternal(const T& value)
91  {
92  mStore.write(&value, sizeof(T));
93  }
94 
95  template<typename T, typename std::enable_if<isVisitable<T>::value, int>::type = 0>
96  void writeInternal(const T& value)
97  {
98  ToFDStoreVisitor visitor(*this);
99  value.accept(visitor);
100  }
101 
102  template<typename T>
103  void writeInternal(const std::vector<T>& values)
104  {
105  writeInternal(values.size());
106  for (const T& value: values) {
107  writeInternal(value);
108  }
109  }
110 
111  template<typename T, std::size_t N>
112  void writeInternal(const std::array<T, N>& values)
113  {
114  for (const T& value: values) {
115  writeInternal(value);
116  }
117  }
118 
119  template<typename V>
120  void writeInternal(const std::map<std::string, V>& values)
121  {
122  writeInternal(values.size());
123  for (const auto& value: values) {
124  writeInternal(value);
125  }
126  }
127 
128  template<typename T, typename std::enable_if<isLikeTuple<T>::value, int>::type = 0>
129  void writeInternal(const T& values)
130  {
131  visitFields(values, this, std::string());
132  }
133 
134 };
135 
136 } // namespace cargo
137 
138 #endif // CARGO_FD_TO_FDSTORE_VISITOR_HPP
Definition: fdstore.hpp:36
Internal configuration helper.
void writeInternal(const cargo::FileDescriptor &fd)
Definition: to-fdstore-visitor.hpp:78
Definition: to-fdstore-visitor.hpp:44
FDStore mStore
Definition: to-fdstore-visitor.hpp:63
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 sendFD(int fd, const unsigned int timeoutMS=maxTimeout)
Definition: fdstore.cpp:168
ToFDStoreVisitor(int fd)
Definition: to-fdstore-visitor.hpp:47
Tuple or pair checker.
Types declarations.
void writeInternal(const char *&value)
Definition: to-fdstore-visitor.hpp:71
ToFDStoreVisitor & operator=(const ToFDStoreVisitor &)=delete
void write(const void *bufferPtr, const size_t size, const unsigned int timeoutMS=maxTimeout)
Write data using the file descriptor.
Definition: fdstore.cpp:104
void writeInternal(const std::map< std::string, V > &values)
Definition: to-fdstore-visitor.hpp:120
void writeInternal(const std::array< T, N > &values)
Definition: to-fdstore-visitor.hpp:112
void writeInternal(const std::string &value)
Definition: to-fdstore-visitor.hpp:65
void writeInternal(const std::vector< T > &values)
Definition: to-fdstore-visitor.hpp:103
void visit(const std::string &, const T &value)
Definition: to-fdstore-visitor.hpp:57
int value
Definition: types.hpp:34
void writeInternal(const T &values)
Definition: to-fdstore-visitor.hpp:129
void writeInternal(const T &value)
Definition: to-fdstore-visitor.hpp:84
Declaration of a class for writing and reading data from a file descriptor.
void visitFields(T &t, F f, A...args)
Definition: visit-fields.hpp:58