Apply shared_ptr to Tuple pointers handled by Tuple & Datbase 01/122701/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 3 Apr 2017 07:20:42 +0000 (16:20 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 3 Apr 2017 07:20:42 +0000 (16:20 +0900)
Change-Id: I2830382970a147648860c309e9137a26b38557e5
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/Database.h
include/Tuple.h
src/database/Database.cpp
src/shared/Tuple.cpp

index f8e9c8a..ea9c470 100644 (file)
@@ -20,6 +20,7 @@
 #include <utility>
 #include <string>
 #include <vector>
+#include <Tuple.h>
 #include <ContextTypes.h>
 
 #define COL_INT64      "x"
@@ -31,8 +32,6 @@ struct sqlite3_stmt;
 
 namespace ctx {
 
-       class Tuple;
-
        class EXPORT_API Database {
        public:
                class Insertions {
@@ -61,12 +60,12 @@ namespace ctx {
                bool open();
                void close();
 
-               bool execute(const std::string& query, const std::string& columnTypes, std::vector<std::string>* columnNames, std::vector<Tuple*>* queryResult);
-               bool execute(const std::string& query, std::vector<Tuple*>* queryResult);
+               bool execute(const std::string& query, const std::string& columnTypes, std::vector<std::string>* columnNames, std::vector<std::shared_ptr<Tuple>>* queryResult);
+               bool execute(const std::string& query, std::vector<std::shared_ptr<Tuple>>* queryResult);
 
                // insert() does not consume the input tuples
                bool insert(const std::string& tableName, const std::string& columns, Tuple* tuple, Tuple* tupleExt = NULL);
-               bool insert(const std::string& tableName, const std::string& columns, std::vector<Tuple*>& tuples);
+               bool insert(const std::string& tableName, const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples);
                bool insert(Insertions& insertions);
 
                void beginTransaction();
index e63caaa..69a90bd 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef __CONTEXT_TUPLE_H__
 #define __CONTEXT_TUPLE_H__
 
+#include <memory>
 #include <vector>
 #include <string>
 #include <ContextTypes.h>
@@ -25,6 +26,7 @@ namespace ctx {
 
        class EXPORT_API Tuple {
        public:
+               Tuple(GVariant* gVar);
                ~Tuple();
 
                unsigned int size();
@@ -34,16 +36,14 @@ namespace ctx {
                bool getAt(unsigned int idx, std::string* val);
                bool getAt(unsigned int idx, const char** val);
 
-               static Tuple* duplicate(Tuple& tuple);
+               static std::shared_ptr<Tuple> duplicate(std::shared_ptr<Tuple> tuple);
 
-               static GVariant* toGVariant(Tuple* tuple);
-               static GVariant* toGVariant(std::vector<Tuple*>& tuples);
+               static GVariant* toGVariant(std::shared_ptr<Tuple> tuple);
+               static GVariant* toGVariant(std::vector<std::shared_ptr<Tuple>>& tuples);
 
-               static std::vector<Tuple*> buildFrom(GVariant* gVar);
+               static std::vector<std::shared_ptr<Tuple>> buildFrom(GVariant* gVar);
 
        private:
-               Tuple(GVariant* gVar);
-
                bool __parse();
                bool __verify(unsigned int idx, const GVariantType* type);
 
@@ -61,7 +61,7 @@ namespace ctx {
                        Builder& add(double val);
                        Builder& add(const std::string& val);
 
-                       Tuple* build();
+                       std::shared_ptr<Tuple> build();
 
                private:
                        bool __init();
index 07813ab..ab0b8d2 100644 (file)
 
 #include <algorithm>
 #include <sqlite3.h>
-#include <Tuple.h>
 #include <Database.h>
 
 using namespace ctx;
+using std::shared_ptr;
 
 Database::Database(const std::string& dbPath) :
        __dbHandle(NULL),
@@ -72,8 +72,8 @@ void Database::close()
 struct ExecutionCbData {
        const std::string* columnTypes;
        std::vector<std::string>* columnNames;
-       std::vector<Tuple*>* queryResult;
-       ExecutionCbData(const std::string* types, std::vector<std::string>* names, std::vector<Tuple*>* result) :
+       std::vector<shared_ptr<Tuple>>* queryResult;
+       ExecutionCbData(const std::string* types, std::vector<std::string>* names, std::vector<shared_ptr<Tuple>>* result) :
                columnTypes(types),
                columnNames(names),
                queryResult(result)
@@ -139,15 +139,18 @@ static int __executionCb(void *userData, int dim, char** value, char** column)
                }
        }
 
-       Tuple* tuple = builder.build();
-       IF_FAIL_RETURN_TAG(tuple, E_NO_MEM, _E, "Memory allocation failed");
+       try {
+               cbData->queryResult->push_back(builder.build());
+       } catch (std::exception& e) {
+               _E("Exception: %s", e.what());
+               return E_FAILED;
+       }
 
-       cbData->queryResult->push_back(tuple);
        return E_NONE;
 }
 
 bool Database::execute(const std::string& query, const std::string& columnTypes,
-               std::vector<std::string>* columnNames, std::vector<Tuple*>* queryResult)
+               std::vector<std::string>* columnNames, std::vector<shared_ptr<Tuple>>* queryResult)
 {
        IF_FAIL_RETURN_TAG(__dbHandle, false, _E, "Not opened");
        _SD("%s", query.c_str());
@@ -169,7 +172,7 @@ bool Database::execute(const std::string& query, const std::string& columnTypes,
        return true;
 }
 
-bool Database::execute(const std::string& query, std::vector<Tuple*>* result)
+bool Database::execute(const std::string& query, std::vector<shared_ptr<Tuple>>* result)
 {
        return execute(query, EMPTY_STR, NULL, result);
 }
@@ -181,11 +184,11 @@ bool Database::insert(const std::string& tableName, const std::string& columns,
        return insertions.__execute();
 }
 
-bool Database::insert(const std::string& tableName, const std::string& columns, std::vector<Tuple*>& tuples)
+bool Database::insert(const std::string& tableName, const std::string& columns, std::vector<shared_ptr<Tuple>>& tuples)
 {
        Insertions insertions(this, tableName, columns);
        for (auto& tuple : tuples) {
-               if (!insertions.add(tuple, NULL))
+               if (!insertions.add(tuple.get(), NULL))
                        return false;
        }
        return insert(insertions);
index 101c644..20cdb8e 100644 (file)
@@ -17,6 +17,9 @@
 #include <Tuple.h>
 
 using namespace ctx;
+using std::string;
+using std::vector;
+using std::shared_ptr;
 
 Tuple::Tuple(GVariant* gVar) :
        __gVar(gVar),
@@ -128,24 +131,29 @@ bool Tuple::getAt(unsigned int idx, const char** val)
        return true;
 }
 
-Tuple* Tuple::duplicate(Tuple& tuple)
+shared_ptr<Tuple> Tuple::duplicate(shared_ptr<Tuple> tuple)
 {
-   gchar* printed = g_variant_print(tuple.__gVar, TRUE);
+   gchar* printed = g_variant_print(tuple->__gVar, TRUE);
    GVariant* gv = g_variant_parse(NULL, printed, NULL, NULL, NULL);
    g_free(printed);
-   return new(std::nothrow) Tuple(gv);
-}
 
+   try {
+          return std::make_shared<Tuple>(gv);
+   } catch (std::exception& e) {
+          _E("Exception: %s", e.what());
+          g_variant_unref(gv);
+          throw;
+   }
+}
 
-GVariant* Tuple::toGVariant(Tuple* tuple)
+GVariant* Tuple::toGVariant(shared_ptr<Tuple> tuple)
 {
        GVariant* gv = tuple->__gVar;
        tuple->__gVar = NULL;
-       delete tuple;
        return gv;
 }
 
-GVariant* Tuple::toGVariant(std::vector<Tuple*>& tuples)
+GVariant* Tuple::toGVariant(vector<shared_ptr<Tuple>>& tuples)
 {
        IF_FAIL_RETURN_TAG(!tuples.empty(), NULL, _W, "Empty");
 
@@ -161,17 +169,15 @@ GVariant* Tuple::toGVariant(std::vector<Tuple*>& tuples)
        return g_variant_builder_end(&builder);
 }
 
-std::vector<Tuple*> Tuple::buildFrom(GVariant* gVar)
+vector<shared_ptr<Tuple>> Tuple::buildFrom(GVariant* gVar)
 {
-       std::vector<Tuple*> tuples;
-       Tuple* tuple = NULL;
+       vector<shared_ptr<Tuple>> tuples;
 
        if (g_variant_is_of_type(gVar, G_VARIANT_TYPE_TUPLE)) {
-               tuple = new(std::nothrow) Tuple(gVar);
-               if (tuple) {
-                       tuples.push_back(tuple);
-               } else {
-                       _E("Memory allocation failed");
+               try {
+                       tuples.push_back(std::make_shared<Tuple>(gVar));
+               } catch (std::exception& e) {
+                       _E("Exception: %s", e.what());
                        g_variant_unref(gVar);
                }
        } else if (g_variant_is_of_type(gVar, G_VARIANT_TYPE_ARRAY)) {
@@ -181,14 +187,13 @@ std::vector<Tuple*> Tuple::buildFrom(GVariant* gVar)
                while ((child = g_variant_iter_next_value(&iter))) {
                        if (!g_variant_is_of_type(child, G_VARIANT_TYPE_TUPLE)) {
                                _E("Invalid data");
-                               g_variant_unref(gVar);
+                               g_variant_unref(child);
                                continue;
                        }
-                       tuple = new(std::nothrow) Tuple(child);
-                       if (tuple) {
-                               tuples.push_back(tuple);
-                       } else {
-                               _E("Memory allocation failed");
+                       try {
+                               tuples.push_back(std::make_shared<Tuple>(child));
+                       } catch (std::exception& e) {
+                               _E("Exception: %s", e.what());
                                g_variant_unref(child);
                        }
                }
@@ -242,20 +247,23 @@ Tuple::Builder& Tuple::Builder::add(const std::string& val)
        return *this;
 }
 
-Tuple* Tuple::Builder::build()
+shared_ptr<Tuple> Tuple::Builder::build()
 {
-       GVariant* gVar = g_variant_builder_end(__gvBuilder);
-       IF_FAIL_RETURN_TAG(gVar, NULL, _E, "Memory allocation failed");
-
-       Tuple* tuple = new(std::nothrow) Tuple(gVar);
-       if (!tuple) {
-               _E("Memory allocation failed");
-               g_variant_unref(gVar);
-               return NULL;
-       }
+       if (!__gvBuilder)
+               throw std::runtime_error("Tuple: building failed");
 
+       GVariant* gVar = g_variant_builder_end(__gvBuilder);
        g_variant_builder_unref(__gvBuilder);
        __gvBuilder = NULL;
 
-       return tuple;
+       if (!gVar)
+               throw std::runtime_error("Tuple: building failed");
+
+       try {
+               return std::make_shared<Tuple>(gVar);
+       } catch (std::exception& e) {
+          _E("Exception: %s", e.what());
+               g_variant_unref(gVar);
+               throw;
+       }
 }