4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sensor_log.h>
29 #define UNDEFINED_TYPE -2
31 static std::atomic<uint64_t> sequence(0);
33 message::message(size_t capacity)
35 , m_capacity(capacity)
36 , m_msg(new(std::nothrow) char[sizeof(char) *capacity])
39 m_header.id = sequence++;
40 m_header.type = UNDEFINED_TYPE;
41 m_header.length = m_size;
45 message::message(const void *msg, size_t sz)
51 m_header.id = sequence++;
52 m_header.type = UNDEFINED_TYPE;
53 m_header.length = m_size;
57 message::message(const message &msg)
59 , m_capacity(msg.m_capacity)
60 , m_msg(new(std::nothrow) char[(sizeof(char) * msg.m_capacity)])
63 ::memcpy(&m_header, &msg.m_header, sizeof(message_header));
64 ::memcpy(m_msg, msg.m_msg, msg.m_size);
67 message::message(int error)
73 m_header.id = sequence++;
74 m_header.type = UNDEFINED_TYPE;
81 if (m_msg && ref_cnt == 0) {
87 void message::enclose(const void *msg, const size_t sz)
95 ::memcpy(reinterpret_cast<char *>(m_msg), msg, sz);
100 void message::enclose(int error)
102 m_header.err = error;
107 void message::disclose(void *msg)
112 ::memcpy(msg, m_msg, m_size);
115 uint32_t message::type(void)
117 return m_header.type;
120 void message::set_type(uint32_t msg_type)
122 m_header.type = msg_type;
125 size_t message::size(void)
130 /* TODO: remove ref/unref and use reference counting automatically */
131 void message::ref(void)
136 void message::unref(void)
140 if (ref_cnt > 0 || !m_msg)
148 int message::ref_count(void)
153 message_header *message::header(void)
158 char *message::body(void)