L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ipc_string
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2014 Alexander Warg <alexander.warg@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7#pragma once
8
9#include "types"
10#include "ipc_basics"
11#include "ipc_array"
12
13namespace L4 { namespace Ipc {
14
15template<typename CHAR = char const, typename LEN = unsigned long>
16struct String : Array<CHAR, LEN>
17{
18 static LEN strlength(CHAR *d) { LEN l = 0; while (d[l]) ++l; return l; }
19 String() {}
20 String(CHAR *d) : Array<CHAR, LEN>(strlength(d) + 1, d) {}
21 String(LEN len, CHAR *d) : Array<CHAR, LEN>(len, d) {}
22 void copy_in(CHAR const *s)
23 {
24 if (this->length < 1)
25 return;
26
27 LEN i;
28 for (i = 0; i < this->length - 1 && s[i]; ++i)
29 this->data[i] = s[i];
30 this->length = i + 1;
31 this->data[i] = CHAR();
32 }
33};
34
35#if __cplusplus >= 201103L
36template< typename CHAR = char, typename LEN_TYPE = unsigned long,
37 LEN_TYPE MAX = (L4_UTCB_GENERIC_DATA_SIZE *
38 sizeof(l4_umword_t)) / sizeof(CHAR) >
39using String_in_buf = Array_in_buf<CHAR, LEN_TYPE, MAX>;
40#endif
41
42namespace Msg {
43template<typename A, typename LEN>
44struct Clnt_xmit< String<A, LEN> > : Clnt_xmit< Array<A, LEN> > {};
45
46template<typename A, typename LEN, typename CLASS>
47struct Svr_val_ops< String<A, LEN>, Dir_in, CLASS >
48: Svr_val_ops< Array_ref<A, LEN>, Dir_in, CLASS >
49{
50 typedef Svr_val_ops< Array_ref<A, LEN>, Dir_in, CLASS > Base;
51 typedef typename Base::svr_type svr_type;
52 using Base::to_svr;
53 static int to_svr(char *msg, unsigned offset, unsigned limit,
54 svr_type &a, Dir_in dir, Cls_data cls)
55 {
56 int r = Base::to_svr(msg, offset, limit, a, dir, cls);
57 if (r < 0)
58 return r;
59
60 // correct clients send at least the zero terminator
61 if (a.length < 1)
62 return -L4_EMSGTOOSHORT;
63
64 typedef typename L4::Types::Remove_const<A>::type elem_type;
65 const_cast<elem_type*>(a.data)[a.length - 1] = A();
66 return r;
67 }
68};
69
70template<typename A, typename LEN>
71struct Clnt_xmit<String<A, LEN> &> : Clnt_xmit<Array<A, LEN> &>
72{
73 typedef Array<A, LEN> &type;
74
75 using Clnt_xmit<type>::from_msg;
76 static int from_msg(char *msg, unsigned offset, unsigned limit, long ret,
77 Array<A, LEN> &a, Dir_out dir, Cls_data cls)
78 {
79 int r = Clnt_xmit<type>::from_msg(msg, offset, limit, ret, a, dir, cls);
80 if (r < 0)
81 return r;
82
83 // check for a bad servers
84 if (a.length < 1)
85 return -L4_EMSGTOOSHORT;
86
87 a.data[a.length - 1] = A();
88 return r;
89 };
90};
91
92template<typename A, typename LEN>
93struct Clnt_xmit<String<A, LEN> *> : Clnt_xmit<String<A, LEN> &> {};
94
95template<typename A, typename LEN, typename CLASS>
96struct Svr_val_ops<String<A, LEN>, Dir_out, CLASS>
97: Svr_val_ops<Array_ref<A, LEN>, Dir_out, CLASS>
98{};
99
100template<typename A, typename LEN>
101struct Is_valid_rpc_type<String<A, LEN> const *> : L4::Types::False {};
102template<typename A, typename LEN>
103struct Is_valid_rpc_type<String<A, LEN> const &> : L4::Types::False {};
104
105} // namespace Msg
106
107}}
unsigned long l4_umword_t
Unsigned machine word.
Definition l4int.h:40
@ L4_EMSGTOOSHORT
Message too short.
Definition err.h:56
L4 low-level kernel interface.
Array()
Make array.
Definition ipc_array:84
False meta value.
Definition types:296