SET(COMMON_SOURCES
${COMMON_PATH}/common.cpp
+ ${COMMON_PATH}/containers/BinaryQueue.cpp
${COMMON_PATH}/log/log.cpp
)
/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
/*
* @file binary_queue.cpp
- * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
+ * @author Przemyslaw Dobrowolski <p.dobrowolsk@samsung.com>
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
* @version 1.0
* @brief This file is the implementation file of binary queue
*/
-#include <stddef.h>
-#include <dpl/binary_queue.h>
-#include <dpl/assert.h>
+
#include <algorithm>
-#include <malloc.h>
#include <cstring>
+#include <malloc.h>
#include <new>
+#include <stddef.h>
+
+#include <attributes/attributes.h>
+#include <exceptions/NullPointerException.h>
+#include <exceptions/OutOfDataException.h>
+#include "BinaryQueue.h"
-namespace SecurityServer {
-BinaryQueue::BinaryQueue() :
- m_size(0)
-{}
+namespace Cynara {
+BinaryQueue::BinaryQueue() : m_size(0) {
+}
-BinaryQueue::BinaryQueue(const BinaryQueue &other) :
- m_size(0)
-{
- AppendCopyFrom(other);
+BinaryQueue::BinaryQueue(const BinaryQueue &other) : m_size(0) {
+ appendCopyFrom(other);
}
-BinaryQueue::~BinaryQueue()
-{
- // Remove all remainig buckets
- Clear();
+BinaryQueue::~BinaryQueue() {
+ // Remove all remaining buckets
+ clear();
}
-const BinaryQueue &BinaryQueue::operator=(const BinaryQueue &other)
-{
+const BinaryQueue &BinaryQueue::operator=(const BinaryQueue &other) {
if (this != &other) {
- Clear();
- AppendCopyFrom(other);
+ clear();
+ appendCopyFrom(other);
}
return *this;
}
-void BinaryQueue::AppendCopyFrom(const BinaryQueue &other)
-{
+void BinaryQueue::appendCopyFrom(const BinaryQueue &other) {
// To speed things up, always copy as one bucket
void *bufferCopy = malloc(other.m_size);
- if (bufferCopy == NULL) {
+ if (bufferCopy == nullptr) {
throw std::bad_alloc();
}
try {
- other.Flatten(bufferCopy, other.m_size);
- AppendUnmanaged(bufferCopy, other.m_size, &BufferDeleterFree, NULL);
+ other.flatten(bufferCopy, other.m_size);
+ appendUnmanaged(bufferCopy, other.m_size, &bufferDeleterFree, nullptr);
} catch (const std::bad_alloc &) {
// Free allocated memory
free(bufferCopy);
}
}
-void BinaryQueue::AppendMoveFrom(BinaryQueue &other)
-{
+void BinaryQueue::appendMoveFrom(BinaryQueue &other) {
// Copy all buckets
std::copy(other.m_buckets.begin(),
other.m_buckets.end(), std::back_inserter(m_buckets));
other.m_size = 0;
}
-void BinaryQueue::AppendCopyTo(BinaryQueue &other) const
-{
- other.AppendCopyFrom(*this);
+void BinaryQueue::appendCopyTo(BinaryQueue &other) const {
+ other.appendCopyFrom(*this);
}
-void BinaryQueue::AppendMoveTo(BinaryQueue &other)
-{
- other.AppendMoveFrom(*this);
+void BinaryQueue::appendMoveTo(BinaryQueue &other) {
+ other.appendMoveFrom(*this);
}
-void BinaryQueue::Clear()
-{
- std::for_each(m_buckets.begin(), m_buckets.end(), &DeleteBucket);
+void BinaryQueue::clear() {
+ std::for_each(m_buckets.begin(), m_buckets.end(), &deleteBucket);
m_buckets.clear();
m_size = 0;
}
-void BinaryQueue::AppendCopy(const void* buffer, size_t bufferSize)
-{
+void BinaryQueue::appendCopy(const void* buffer, size_t bufferSize) {
// Create data copy with malloc/free
void *bufferCopy = malloc(bufferSize);
// Check if allocation succeded
- if (bufferCopy == NULL) {
+ if (bufferCopy == nullptr) {
throw std::bad_alloc();
}
try {
// Try to append new bucket
- AppendUnmanaged(bufferCopy, bufferSize, &BufferDeleterFree, NULL);
+ appendUnmanaged(bufferCopy, bufferSize, &bufferDeleterFree, nullptr);
} catch (const std::bad_alloc &) {
// Free allocated memory
free(bufferCopy);
}
}
-void BinaryQueue::AppendUnmanaged(const void* buffer,
+void BinaryQueue::appendUnmanaged(const void* buffer,
size_t bufferSize,
BufferDeleter deleter,
- void* userParam)
-{
+ void* userParam) {
// Do not attach empty buckets
if (bufferSize == 0) {
deleter(buffer, bufferSize, userParam);
m_size += bufferSize;
}
-size_t BinaryQueue::Size() const
-{
+size_t BinaryQueue::size() const {
return m_size;
}
-bool BinaryQueue::Empty() const
-{
+bool BinaryQueue::empty() const {
return m_size == 0;
}
-void BinaryQueue::Consume(size_t size)
-{
+void BinaryQueue::consume(size_t size) {
// Check parameters
if (size > m_size) {
- Throw(Exception::OutOfData);
+ throw OutOfDataException(m_size, size);
}
size_t bytesLeft = size;
m_size -= count;
if (m_buckets.front()->left == 0) {
- DeleteBucket(m_buckets.front());
+ deleteBucket(m_buckets.front());
m_buckets.pop_front();
}
}
}
-void BinaryQueue::Flatten(void *buffer, size_t bufferSize) const
-{
+void BinaryQueue::flatten(void *buffer, size_t bufferSize) const {
// Check parameters
if (bufferSize == 0) {
return;
}
if (bufferSize > m_size) {
- Throw(Exception::OutOfData);
+ throw OutOfDataException(m_size, bufferSize);
}
size_t bytesLeft = bufferSize;
void *ptr = buffer;
BucketList::const_iterator bucketIterator = m_buckets.begin();
- Assert(m_buckets.end() != bucketIterator);
// Flatten data
while (bytesLeft > 0) {
// Get consume size
+ // todo a check is needed if bucketIterator doesn't point to m_buckets.end()
size_t count = std::min(bytesLeft, (*bucketIterator)->left);
// Copy data to user pointer
}
}
-void BinaryQueue::FlattenConsume(void *buffer, size_t bufferSize)
-{
+void BinaryQueue::flattenConsume(void *buffer, size_t bufferSize) {
// FIXME: Optimize
- Flatten(buffer, bufferSize);
- Consume(bufferSize);
+ flatten(buffer, bufferSize);
+ consume(bufferSize);
}
-void BinaryQueue::DeleteBucket(BinaryQueue::Bucket *bucket)
-{
+void BinaryQueue::deleteBucket(BinaryQueue::Bucket *bucket) {
delete bucket;
}
-void BinaryQueue::BufferDeleterFree(const void* data,
- size_t dataSize,
- void* userParam)
-{
- (void)dataSize;
- (void)userParam;
-
+void BinaryQueue::bufferDeleterFree(const void* data,
+ size_t dataSize UNUSED,
+ void* userParam UNUSED) {
// Default free deleter
free(const_cast<void *>(data));
}
size(dataSize),
left(dataSize),
deleter(dataDeleter),
- param(userParam)
-{
- Assert(data != NULL);
- Assert(deleter != NULL);
+ param(userParam) {
+
+ if(data == nullptr)
+ throw NullPointerException("data");
+
+ if(dataDeleter == nullptr)
+ throw NullPointerException("dataDeleter");
}
-BinaryQueue::Bucket::~Bucket()
-{
+BinaryQueue::Bucket::~Bucket() {
// Invoke deleter on bucket data
deleter(buffer, size, param);
}
-BinaryQueue::BucketVisitor::~BucketVisitor()
-{}
+BinaryQueue::BucketVisitor::~BucketVisitor() {
+}
BinaryQueue::BucketVisitorCall::BucketVisitorCall(BucketVisitor *visitor) :
- m_visitor(visitor)
-{}
+ m_visitor(visitor) {
+}
-BinaryQueue::BucketVisitorCall::~BucketVisitorCall()
-{}
+BinaryQueue::BucketVisitorCall::~BucketVisitorCall() {
+}
-void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const
-{
- m_visitor->OnVisitBucket(bucket->ptr, bucket->left);
+void BinaryQueue::BucketVisitorCall::operator()(Bucket *bucket) const {
+ m_visitor->onVisitBucket(bucket->ptr, bucket->left);
}
-void BinaryQueue::VisitBuckets(BucketVisitor *visitor) const
-{
- Assert(visitor != NULL);
+void BinaryQueue::visitBuckets(BucketVisitor *visitor) const {
+ if(visitor == nullptr)
+ throw NullPointerException("visitor");
// Visit all buckets
std::for_each(m_buckets.begin(), m_buckets.end(), BucketVisitorCall(visitor));
}
-BinaryQueueAutoPtr BinaryQueue::Read(size_t size)
-{
- // Simulate input stream
- size_t available = std::min(size, m_size);
-
- std::unique_ptr<void, std::function<void(void*)>>
- bufferCopy(malloc(available), free);
-
- if (!bufferCopy.get()) {
- throw std::bad_alloc();
- }
-
- BinaryQueueAutoPtr result(new BinaryQueue());
-
- Flatten(bufferCopy.get(), available);
- result->AppendUnmanaged(
- bufferCopy.release(), available, &BufferDeleterFree, NULL);
- Consume(available);
-
- return result;
-}
-
-size_t BinaryQueue::Write(const BinaryQueue &buffer, size_t bufferSize)
-{
- // Simulate output stream
- AppendCopyFrom(buffer);
- return bufferSize;
-}
-} // namespace SecurityServer
+} // namespace Cynara
/*
- * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
/*
- * @file binary_queue.h
- * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
+ * @file BinaryQueue.h
+ * @author Przemyslaw Dobrowolski <p.dobrowolsk@samsung.com>
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
* @version 1.0
* @brief This file is the header file of binary queue
*/
-#ifndef SECURITY_SERVER_BINARY_QUEUE_H
-#define SECURITY_SERVER_BINARY_QUEUE_H
-//#include <dpl/abstract_input_output.h>
-#include <dpl/exception.h>
-#include <dpl/noncopyable.h>
+#ifndef SRC_COMMON_CONTAINERS_BINARYQUEUE_H_
+#define SRC_COMMON_CONTAINERS_BINARYQUEUE_H_
+
#include <memory>
#include <list>
-namespace SecurityServer {
+namespace Cynara {
/**
* Binary queue auto pointer
*/
* @todo Add optimized implementation for FlattenConsume
*/
class BinaryQueue
-// : public AbstractInputOutput
{
public:
- class Exception
- {
- public:
- DECLARE_EXCEPTION_TYPE(SecurityServer::Exception, Base)
- DECLARE_EXCEPTION_TYPE(Base, OutOfData)
- };
-
typedef void (*BufferDeleter)(const void *buffer, size_t bufferSize,
void *userParam);
- static void BufferDeleterFree(const void *buffer,
+ static void bufferDeleterFree(const void *buffer,
size_t bufferSize,
void *userParam);
* @param[in] buffer Constant pointer to bucket data buffer
* @param[in] bufferSize Number of bytes in bucket
*/
- virtual void OnVisitBucket(const void *buffer, size_t bufferSize) = 0;
+ virtual void onVisitBucket(const void *buffer, size_t bufferSize) = 0;
};
private:
- struct Bucket :
- private Noncopyable
+ struct Bucket
{
const void *buffer;
const void *ptr;
size_t bufferSize,
BufferDeleter deleter,
void *userParam);
- virtual ~Bucket();
+ ~Bucket();
+ // make it noncopyable
+ Bucket(const Bucket &) = delete;
+ const Bucket &operator=(const Bucket &) = delete;
+ // make it nonmoveable
+ Bucket(Bucket &&) = delete;
+ Bucket &operator=(Bucket &&) = delete;
};
typedef std::list<Bucket *> BucketList;
BucketList m_buckets;
size_t m_size;
- static void DeleteBucket(Bucket *bucket);
+ static void deleteBucket(Bucket *bucket);
class BucketVisitorCall
{
/**
* Destructor
*/
- virtual ~BinaryQueue();
+ ~BinaryQueue();
/**
* Construct binary queue via bare copy of other binary queue
* @exception std::bad_alloc Cannot allocate memory to hold additional data
* @see BinaryQueue::BufferDeleterFree
*/
- void AppendCopy(const void *buffer, size_t bufferSize);
+ void appendCopy(const void *buffer, size_t bufferSize);
/**
* Append @a bufferSize bytes from memory pointed by @a buffer
* buffer
* @param[in] userParam User parameter passed to deleter routine
* @exception std::bad_alloc Cannot allocate memory to hold additional data
+ * @exception Cynara::NullPointerException buffer or deleter are nullptr
*/
- void AppendUnmanaged(
+ void appendUnmanaged(
const void *buffer,
size_t bufferSize,
BufferDeleter deleter =
- &BinaryQueue::BufferDeleterFree,
- void *userParam = NULL);
+ &BinaryQueue::bufferDeleterFree,
+ void *userParam = nullptr);
/**
* Append copy of other binary queue to the end of this binary queue
* @exception std::bad_alloc Cannot allocate memory to hold additional data
* @warning One cannot assume that bucket structure is preserved during copy
*/
- void AppendCopyFrom(const BinaryQueue &other);
+ void appendCopyFrom(const BinaryQueue &other);
/**
* Move bytes from other binary queue to the end of this binary queue.
* @param[in] other Reference to other binary queue to move data from
* @exception std::bad_alloc Cannot allocate memory to hold additional data
*/
- void AppendMoveFrom(BinaryQueue &other);
+ void appendMoveFrom(BinaryQueue &other);
/**
* Append copy of binary queue to the end of other binary queue
* @exception std::bad_alloc Cannot allocate memory to hold additional data
* @warning One cannot assume that bucket structure is preserved during copy
*/
- void AppendCopyTo(BinaryQueue &other) const;
+ void appendCopyTo(BinaryQueue &other) const;
/**
* Move bytes from binary queue to the end of other binary queue.
* @param[in] other Reference to other binary queue to move data to
* @exception std::bad_alloc Cannot allocate memory to hold additional data
*/
- void AppendMoveTo(BinaryQueue &other);
+ void appendMoveTo(BinaryQueue &other);
/**
* Retrieve total size of all data contained in binary queue
*
* @return Number of bytes in binary queue
*/
- size_t Size() const;
+ size_t size() const;
/**
* Remove all data from binary queue
*
* @return none
*/
- void Clear();
+ void clear();
/**
* Check if binary queue is empty
*
* @return true if binary queue is empty, false otherwise
*/
- bool Empty() const;
+ bool empty() const;
/**
* Remove @a size bytes from beginning of binary queue
*
* @return none
* @param[in] size Number of bytes to remove
- * @exception BinaryQueue::Exception::OutOfData Number of bytes is larger
+ * @exception Cynara::OutOfDataException Number of bytes is larger
* than available bytes in binary queue
*/
- void Consume(size_t size);
+ void consume(size_t size);
/**
* Retrieve @a bufferSize bytes from beginning of binary queue and copy them
* @return none
* @param[in] buffer Pointer to user buffer to receive bytes
* @param[in] bufferSize Size of user buffer pointed by @a buffer
- * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
+ * @exception Cynara::OutOfDataException Number of bytes to flatten
* is larger than available bytes in binary queue
*/
- void Flatten(void *buffer, size_t bufferSize) const;
+ void flatten(void *buffer, size_t bufferSize) const;
/**
* Retrieve @a bufferSize bytes from beginning of binary queue, copy them
* @return none
* @param[in] buffer Pointer to user buffer to receive bytes
* @param[in] bufferSize Size of user buffer pointed by @a buffer
- * @exception BinaryQueue::Exception::OutOfData Number of bytes to flatten
+ * @exception Cynara::OutOfDataException Number of bytes to flatten
* is larger than available bytes in binary queue
*/
- void FlattenConsume(void *buffer, size_t bufferSize);
+ void flattenConsume(void *buffer, size_t bufferSize);
/**
* Visit each buffer with data using visitor object
* @return none
* @param[in] visitor Pointer to bucket visitor
* @see BinaryQueue::BucketVisitor
+ * @exception Cynara::NullPointerException visitor is nullptr
*/
- void VisitBuckets(BucketVisitor *visitor) const;
-
- /**
- * IAbstractInput interface
- */
- virtual BinaryQueueAutoPtr Read(size_t size);
-
- /**
- * IAbstractOutput interface
- */
- virtual size_t Write(const BinaryQueue &buffer, size_t bufferSize);
+ void visitBuckets(BucketVisitor *visitor) const;
};
-} // namespace SecurityServer
+} // namespace Cynara
-#endif // SECURITY_SERVER_BINARY_QUEUE_H
+#endif /* SRC_COMMON_CONTAINERS_BINARYQUEUE_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file NullPointerException.h
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of OutOfDataException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_NULLPOINTEREXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_NULLPOINTEREXCEPTION_H_
+
+#include <exception>
+#include <string>
+
+#include "Exception.h"
+
+namespace Cynara {
+
+class NullPointerException : public Exception {
+private:
+ std::string m_whatMsg;
+
+public:
+ NullPointerException() = delete;
+ NullPointerException(const char *varName) {
+ m_whatMsg = std::string("unexpected null value in variable <")
+ + std::string(varName)
+ + std::string(">");
+ }
+
+ virtual ~NullPointerException() = default;
+
+ virtual const char* what() const noexcept {
+ return m_whatMsg.c_str();
+ }
+};
+
+} // namespace Cynara
+
+#endif /* SRC_COMMON_EXCEPTIONS_NULLPOINTEREXCEPTION_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file OutOfDataException.h
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of OutOfDataException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_OUTOFDATAEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_OUTOFDATAEXCEPTION_H_
+
+#include <exception>
+#include <sstream>
+#include <string>
+
+#include "Exception.h"
+
+namespace Cynara {
+
+class OutOfDataException : public Exception {
+private:
+ std::string m_whatMsg;
+
+public:
+ OutOfDataException() = delete;
+ OutOfDataException(size_t dataRange, size_t accessTry) {
+ std::ostringstream stream;
+ stream << "trying access [" << accessTry << "]";
+ stream << " which exceeds data range [" << dataRange << "]";
+ m_whatMsg = stream.str();
+ }
+
+ virtual ~OutOfDataException() = default;
+
+ virtual const char* what() const noexcept {
+ return m_whatMsg.c_str();
+ }
+};
+
+} // namespace Cynara
+
+#endif /* SRC_COMMON_EXCEPTIONS_OUTOFDATAEXCEPTION_H_ */