Removing copy&paste code, part 2
authorJanusz Majnert <j.majnert@samsung.com>
Thu, 22 Nov 2012 13:06:01 +0000 (14:06 +0100)
committerGerrit Code Review <gerrit2@kim11>
Mon, 3 Dec 2012 06:43:13 +0000 (15:43 +0900)
[Issue#] N/A
[Bug/Feature] Duplicated code
[Cause] Copy&Paste
[Solution] Extract common code, generalise it
[Verification] Build this and all dependent packages, install them accordingly and run all tests

Changes:
- In modules/db/include/dpl/db/orm.h - replaced class-local BindVisitor helper
  classes with a file-wide class.
- NamedInputPipe - code was virtually identical to FileInput, so now
  NamedInputPipe inherits after FileInput.
- Removed some code that has been commented out for most part of the year

Change-Id: I67dba22a6341bf38db198bbc31c1425ce8a41a9f

modules/core/config.cmake
modules/core/include/dpl/named_input_pipe.h
modules/core/src/file_input.cpp
modules/core/src/main.cpp
modules/core/src/named_input_pipe.cpp [deleted file]
modules/db/include/dpl/db/orm.h

index 75ec25b..147f155 100644 (file)
@@ -40,7 +40,6 @@ SET(DPL_CORE_SOURCES
     ${PROJECT_SOURCE_DIR}/modules/core/src/lexical_cast.cpp
     ${PROJECT_SOURCE_DIR}/modules/core/src/mutex.cpp
     ${PROJECT_SOURCE_DIR}/modules/core/src/named_base_pipe.cpp
-    ${PROJECT_SOURCE_DIR}/modules/core/src/named_input_pipe.cpp
     ${PROJECT_SOURCE_DIR}/modules/core/src/named_output_pipe.cpp
     ${PROJECT_SOURCE_DIR}/modules/core/src/noncopyable.cpp
     ${PROJECT_SOURCE_DIR}/modules/core/src/once.cpp
index 247890d..7f10c5d 100644 (file)
 #ifndef DPL_NAMED_PIPE_H
 #define DPL_NAMED_PIPE_H
 
-#include <dpl/noncopyable.h>
 #include <dpl/exception.h>
-#include <dpl/abstract_waitable_input.h>
 #include <dpl/named_base_pipe.h>
+#include <dpl/file_input.h>
 
 namespace DPL
 {
 class NamedInputPipe
-    : private Noncopyable,
-      public NamedBasePipe,
-      public AbstractWaitableInput
-{
-public:
-    class Exception
-    {
-    public:
-        DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
-        DECLARE_EXCEPTION_TYPE(Base, OpenFailed)
-        DECLARE_EXCEPTION_TYPE(Base, CloseFailed)
-    };
-
-protected:
-    int m_fifo;
-
-public:
-    NamedInputPipe();
-    virtual ~NamedInputPipe();
-
-    void Open(const std::string &fileName);
-    void Close();
-
-    // AbstractInput
-    virtual BinaryQueueAutoPtr Read(size_t size);
-
-    // AbstractWaitableInput
-    virtual WaitableHandle WaitableReadHandle() const;
-};
+    : public NamedBasePipe,
+      public FileInput
+{};
 } // namespace DPL
 
 #endif // DPL_NAMED_PIPE_H
index e51644d..c0007e4 100644 (file)
@@ -14,7 +14,7 @@
  *    limitations under the License.
  */
 /*
- * @file        named_input_pipe.cpp
+ * @file        file_input.cpp
  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
  * @version     1.0
  * @brief       This file is the implementation file of named input pipe
@@ -30,6 +30,7 @@
 
 namespace DPL
 {
+
 namespace // anonymous
 {
 const size_t DEFAULT_READ_BUFFER_SIZE = 4096;
index 9b4455a..80f8cb5 100644 (file)
@@ -359,60 +359,6 @@ void Main::HandleDirectInvoker()
 // GLIB loop intergration workaround
 int Main::EcoreSelectInterceptor(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
 {
-#if 0
-    // Check each descriptor to see if it is valid
-    for (int i = 0; i < nfds; i++)
-    {
-        if (FD_ISSET(i, readfds) || FD_ISSET(i, writefds) || FD_ISSET(i, exceptfds))
-        {
-            // Try to get descriptor flags
-            int result = fcntl(i, F_GETFL);
-
-            if (result == -1)
-            {
-                if (errno == EBADF)
-                {
-                    // This a bad descriptor. Remove all occurrences of it.
-                    if (FD_ISSET(i, readfds))
-                    {
-                        LogPedantic("GLIB_LOOP_INTEGRATION_WORKAROUND: Bad read descriptor: " << i);
-                        FD_CLR(i, readfds);
-                    }
-
-                    if (FD_ISSET(i, writefds))
-                    {
-                        LogPedantic("GLIB_LOOP_INTEGRATION_WORKAROUND: Bad write descriptor: " << i);
-                        FD_CLR(i, writefds);
-                    }
-
-                    if (FD_ISSET(i, exceptfds))
-                    {
-                        LogPedantic("GLIB_LOOP_INTEGRATION_WORKAROUND: Bad exception descriptor: " << i);
-                        FD_CLR(i, exceptfds);
-                    }
-                }
-                else
-                {
-                    // Unexpected error
-                    Assert(0);
-                }
-            }
-        }
-    }
-
-    // Find out new maximum
-    int newNfds = 0;
-
-    for (int i = 0; i < nfds; i++)
-    {
-        if (FD_ISSET(i, readfds) || FD_ISSET(i, writefds) || FD_ISSET(i, exceptfds))
-            newNfds = i;
-    }
-
-    return MainSingleton::Instance().m_oldEcoreSelect(newNfds + 1, readfds, writefds, exceptfds, timeout);
-
-#else
-
     // We have to check error code here and make another try because of some glib error's.
     fd_set rfds, wfds, efds;
     memcpy(&rfds, readfds, sizeof(fd_set));
@@ -482,7 +428,6 @@ int Main::EcoreSelectInterceptor(int nfds, fd_set *readfds, fd_set *writefds, fd
     }
 
     return ret;
-#endif
 }
 #endif // DPL_ENABLE_GLIB_LOOP_INTEGRATION_WORKAROUND
 } // namespace DPL
diff --git a/modules/core/src/named_input_pipe.cpp b/modules/core/src/named_input_pipe.cpp
deleted file mode 100644 (file)
index f5f4538..0000000
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright (c) 2011 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        named_input_pipe.cpp
- * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
- * @version     1.0
- * @brief       This file is the implementation file of named input pipe
- */
-#include <stddef.h>
-#include <dpl/named_input_pipe.h>
-#include <dpl/binary_queue.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-
-namespace DPL
-{
-namespace // anonymous
-{
-const size_t DEFAULT_READ_BUFFER_SIZE = 1024;
-} // namespace anonymous
-
-NamedInputPipe::NamedInputPipe()
-    : m_fifo(-1)
-{
-}
-
-NamedInputPipe::~NamedInputPipe()
-{
-    Close();
-}
-
-void NamedInputPipe::Open(const std::string& pipeName)
-{
-    // Open pipe for reading
-    int fifo = TEMP_FAILURE_RETRY(open(pipeName.c_str(), O_RDONLY | O_NONBLOCK));
-
-    if (fifo == -1)
-        ThrowMsg(Exception::OpenFailed, pipeName);
-
-    m_fifo = fifo;
-}
-
-void NamedInputPipe::Close()
-{
-    if (m_fifo == -1)
-        return;
-
-    if (TEMP_FAILURE_RETRY(close(m_fifo)) == -1)
-        Throw(Exception::CloseFailed);
-
-    m_fifo = -1;
-}
-
-BinaryQueueAutoPtr NamedInputPipe::Read(size_t size)
-{
-    size_t bytesToRead = size > DEFAULT_READ_BUFFER_SIZE ? DEFAULT_READ_BUFFER_SIZE : size;
-
-    // Malloc default read buffer size
-    // It is unmanaged, so it can be then attached directly to binary queue
-    void *buffer = malloc(bytesToRead);
-
-    if (buffer == NULL)
-        throw std::bad_alloc();
-
-    ssize_t result = TEMP_FAILURE_RETRY(read(m_fifo, buffer, bytesToRead));
-
-    if (result > 0)
-    {
-        // Succedded to read socket data
-        BinaryQueueAutoPtr binaryQueue(new BinaryQueue());
-
-        // Append unmanaged memory
-        binaryQueue->AppendUnmanaged(buffer, result, &BinaryQueue::BufferDeleterFree, NULL);
-
-        // Return buffer
-        return binaryQueue;
-    }
-    else if (result == 0)
-    {
-        // Socket was gracefuly closed
-        free(buffer);
-
-        // Return empty buffer
-        return BinaryQueueAutoPtr(new BinaryQueue());
-    }
-    else
-    {
-        // Must first save errno value, because it may be altered
-        int lastErrno = errno;
-
-        // Free buffer
-        free(buffer);
-
-        // Interpret error result
-        (void)lastErrno;
-
-        // FIXME: Handle specific errno
-        Throw(AbstractInput::Exception::ReadFailed);
-    }
-}
-
-int NamedInputPipe::WaitableReadHandle() const
-{
-    return m_fifo;
-}
-} // namespace DPL
index 64c0705..0d851a6 100644 (file)
@@ -632,6 +632,29 @@ public:
     }
 };
 
+namespace {
+class BindVisitor {
+private:
+    DataCommand *m_command;
+public:
+    ArgumentIndex m_bindArgumentIndex;
+
+    BindVisitor(DataCommand *command) :
+        m_command(command),
+        m_bindArgumentIndex(1)
+    {}
+
+    template<typename ColumnType>
+    void Visit(const char*, const ColumnType& value, bool isSet)
+    {
+        if ( isSet )
+        {
+            DataCommandUtils::BindArgument(m_command, m_bindArgumentIndex, value);
+            m_bindArgumentIndex++;
+        }
+    }
+};
+} //anonymous namespace
 template<typename TableDefinition>
 class __attribute__ ((visibility("hidden"))) Insert : public Query<TableDefinition>
 {
@@ -689,27 +712,6 @@ protected:
         }
     }
 
-    class BindVisitor {
-    private:
-        DataCommand *m_command;
-        ArgumentIndex m_bindArgumentIndex;
-    public:
-        BindVisitor(DataCommand *command) :
-            m_command(command),
-            m_bindArgumentIndex(1)
-        {}
-
-        template<typename ColumnType>
-        void Visit(const char*, const ColumnType& value, bool isSet)
-        {
-            if ( isSet )
-            {
-                DataCommandUtils::BindArgument(m_command, m_bindArgumentIndex, value);
-                m_bindArgumentIndex++;
-            }
-        }
-    };
-
     void Bind()
     {
         BindVisitor visitor(this->m_command);
@@ -999,29 +1001,6 @@ protected:
         }
     }
 
-    class BindVisitor {
-    private:
-        DataCommand *m_command;
-
-    public:
-        ArgumentIndex m_bindArgumentIndex;
-
-        BindVisitor(DataCommand *command) :
-            m_command(command),
-            m_bindArgumentIndex(1)
-        {}
-
-        template<typename ColumnType>
-        void Visit(const char*, const ColumnType& value, bool isSet)
-        {
-            if ( isSet )
-            {
-                DataCommandUtils::BindArgument(m_command, m_bindArgumentIndex, value);
-                m_bindArgumentIndex++;
-            }
-        }
-    };
-
     void Bind()
     {
         BindVisitor visitor(this->m_command);