2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file rpc_function.h
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the header file for RPC function
22 #ifndef DPL_RPC_FUNCTION_H
23 #define DPL_RPC_FUNCTION_H
25 #include <dpl/exception.h>
26 #include <dpl/binary_queue.h>
27 #include <dpl/scoped_array.h>
28 #include <dpl/string.h>
29 #include <dpl/serialization.h>
34 class RPCFunction : public IStream
37 BinaryQueue m_buffer; ///< Serialized RPC function call as a binary queue
43 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
44 DECLARE_EXCEPTION_TYPE(Base, ParseFailed)
56 * @param buffer Binary queue to copy initialization data from
58 RPCFunction(const BinaryQueue &buffer)
60 m_buffer.AppendCopyFrom(buffer);
66 virtual ~RPCFunction()
70 * Append argument to call
72 * @param[in] arg Template based argument to append
74 * @warning Carefully add any pointers to buffer because of template nature
77 template<typename Type>
78 void AppendArg(const Type &arg)
80 size_t argSize = sizeof(arg);
81 m_buffer.AppendCopy(&argSize, sizeof(argSize));
82 m_buffer.AppendCopy(&arg, sizeof(arg));
86 * Append @a std::string argument to call
88 * @param[in] arg String to append to function call
91 void AppendArg(const std::string &arg)
93 size_t argSize = arg.size();
94 m_buffer.AppendCopy(&argSize, sizeof(argSize));
95 m_buffer.AppendCopy(arg.c_str(), argSize);
99 * Append @a DPL::String argument to call
101 * @param[in] arg String to append to function call
104 void AppendArg(const String &arg)
106 std::string localStdString = ToUTF8String(arg);
107 AppendArg(localStdString);
111 * Consume argument from call. Arguments are retrieved in non-reversed order
112 * (same as they were pushed onto RPC function argument stack)
114 * @param[out] arg Reference to output template based argument
115 * @warning Carefully add any pointers to buffer because of template nature
119 template<typename Type>
120 void ConsumeArg(Type &arg)
124 size_t argSize = sizeof(arg);
125 m_buffer.FlattenConsume(&argSize, sizeof(argSize));
127 if (argSize != sizeof(arg)) {
128 ThrowMsg(Exception::ParseFailed, "Stream parse CRC failed");
131 m_buffer.FlattenConsume(&arg, sizeof(arg));
133 Catch(BinaryQueue::Exception::OutOfData)
135 ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
140 * Consume @a std::string argument from call. Arguments are retrieved in
142 * (same as they were pushed onto RPC function argument stack)
144 * @param[out] arg Reference to output @a std::string argument
147 void ConsumeArg(std::string &arg)
151 std::string::size_type size;
152 m_buffer.FlattenConsume(&size, sizeof(size));
153 ScopedArray<char> str(new char[size]);
154 m_buffer.FlattenConsume(str.Get(), size);
155 arg = std::string(str.Get(), str.Get() + size);
157 Catch(BinaryQueue::Exception::OutOfData)
159 ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
164 * Consume @a DPL::String argument from call. Arguments are converted to
167 * @param[out] arg Reference to output @a DPL::String argument
170 void ConsumeArg(String &arg)
172 std::string consumedStdString;
173 ConsumeArg(consumedStdString);
174 arg = FromUTF8String(consumedStdString);
178 * Serialize all function parameters into single binary queue
180 * @return Serialized binary queue representation of RPC function
182 BinaryQueue Serialize() const
188 * Reads binary data from serialized stream
190 * @param num number of bytes to read
191 * @param bytes buffer for read data
193 virtual void Read(size_t num, void * bytes)
195 m_buffer.FlattenConsume(bytes, num);
199 * Writes binary data to serialized stream
201 * @param num number of bytes to write to serialization buffer
202 * @param bytes buffer for data to write
204 virtual void Write(size_t num, const void * bytes)
206 m_buffer.AppendCopy(bytes, num);
212 #endif // DPL_RPC_FUNCTION_H