Merge "[N_SE-33432] non-sqaure matrix is not set correctly" into tizen_2.1
[platform/framework/native/appfw.git] / src / io / FIo_DataSetImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19 * @file         FIo_DataSetImpl.cpp
20 * @brief                This is the implementation file for the _DataSetImpl class.
21 */
22
23 #include <new>
24 #include <unique_ptr.h>
25
26 #include <FBaseObject.h>
27 #include <FBaseInteger.h>
28 #include <FBaseSysLog.h>
29 #include <FIo_DataSetImpl.h>
30 #include <FIo_DataRowImpl.h>
31 #include <FIo_DataSetEnumeratorImpl.h>
32
33 using namespace std;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
36
37 namespace Tizen { namespace Io
38 {
39 _DataSetImpl::_DataSetImpl(void)
40         :__rowCount(0),
41         __pDataSet(null),
42         __pColumnList(null),
43         __pColumnTypeList(null)
44 {
45 }
46
47 _DataSetImpl::~_DataSetImpl(void)
48 {
49         delete __pDataSet;
50         delete __pColumnList;
51         delete __pColumnTypeList;
52 }
53
54 result
55 _DataSetImpl::Construct(const IList& columnNames)
56 {
57         result r = E_SUCCESS;
58
59         __pDataSet = new (std::nothrow) LinkedList(SingleObjectDeleter);
60         SysTryReturnResult(NID_IO, __pDataSet != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
61         __pColumnList = new (std::nothrow) ArrayList(SingleObjectDeleter);
62         SysTryReturnResult(NID_IO, __pColumnList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
63         __pColumnTypeList = new (std::nothrow) ArrayList(SingleObjectDeleter);
64         SysTryReturnResult(NID_IO, __pColumnTypeList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
65
66         __columnCount = columnNames.GetCount();
67         SysTryReturnResult(NID_IO, __columnCount > 0, E_INVALID_ARG, "column count is 0.");
68
69         r = __pColumnList->Construct(__columnCount);
70         SysTryReturn(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] column list construct failed");
71         r = __pColumnTypeList->Construct(__columnCount);
72         SysTryReturn(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, r, "[E_OUT_OF_MEMORY] column type list construct failed");
73
74         for (int i = 0 ; i < __columnCount ; i++)
75         {
76                 r = __pColumnList->Add(new (std::nothrow) String(*(String*)(columnNames.GetAt(i))));
77                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, "The memory is insufficient.");
78
79                 r = __pColumnTypeList->Add(new (std::nothrow) Integer(DB_COLUMNTYPE_NULL));
80                 SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OUT_OF_MEMORY, "The memory is insufficient.");
81         }
82         //SysLog(NID_IO, "column count is %d", __columnCount);
83
84         return r;
85 }
86
87 DataRow*
88 _DataSetImpl::CreateDataRowN(void)
89 {
90         result r;
91         unique_ptr<ArrayList> pNewRowArrayList(new (std::nothrow) ArrayList(SingleObjectDeleter));
92         SysTryReturn(NID_IO, pNewRowArrayList != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
93
94         unique_ptr<DataRow> pDataRow(_DataRowImpl::CreateDataRowInstanceN(__columnCount, pNewRowArrayList.get()));
95         SysTryReturn(NID_IO, pDataRow != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
96
97         r = __pDataSet->Add(pNewRowArrayList.release());
98         SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Add Row to DataSet is failed");
99
100         _DataRowImpl::GetInstance(*pDataRow)->__pColumnTypeList = __pColumnTypeList;
101
102         __rowCount = __pDataSet->GetCount();
103         //SysLog(NID_IO, "row count is %d", __rowCount);
104
105         return pDataRow.release();
106
107 }
108
109 DataSetEnumerator*
110 _DataSetImpl::GetDataSetEnumeratorN(void)
111 {
112         unique_ptr<DataSetEnumerator> pDataSetEnum(_DataSetEnumeratorImpl::CreateDataSetEnumeratorInstanceN());
113
114         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pDataSet = __pDataSet;
115         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__pColumnList = __pColumnList;
116         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__columnCount = __columnCount;
117         _DataSetEnumeratorImpl::GetInstance(*pDataSetEnum)->__rowCount = __rowCount;
118
119         //SysLog(NID_IO, "row count is %d, column count is %d", __rowCount, __columnCount);
120
121         return pDataSetEnum.release();
122 }
123
124 DataSet*
125 _DataSetImpl::CloneN(void) const
126 {
127         result r;
128
129         unique_ptr< DataSet > pDataSet(new (std::nothrow) DataSet());
130         SysTryReturn(NID_IO, pDataSet != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
131
132         pDataSet->Construct(*__pColumnList);
133
134         for (int i = 0; i < __columnCount; i++)
135         {
136                 r = _DataSetImpl::GetInstance(*pDataSet)->__pColumnTypeList->SetAt(new Integer(*(Integer*)__pColumnTypeList->GetAt(i)), i);
137                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] ColumnTypeList Copy Falied");
138         }
139
140         for (int i = 0; i < __rowCount; i++)
141         {
142                 DataRow* pNewRow = pDataSet->CreateDataRowN();
143                 r = _DataRowImpl::GetInstance(*pNewRow)->Clone((const ArrayList*)(__pDataSet->GetAt(i)));
144                 delete pNewRow;
145
146                 SysTryReturn(NID_IO, r == E_SUCCESS, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] DataRow Clone Falied");
147         }
148
149         return pDataSet.release();
150 }
151
152 bool
153 _DataSetImpl::Equals(const Tizen::Base::Object& obj) const
154 {
155         int thisHash = this->GetHashCode();
156         int othersHash = obj.GetHashCode();
157
158         if (thisHash == othersHash )
159                 return true;
160         else
161                 return false;
162 }
163
164 int
165 _DataSetImpl::GetHashCode(void) const
166 {
167         int hash = 0;
168
169         for (int i = 0; i < __rowCount; i++)
170         {
171                 const ArrayList* pDataRowArrayList = static_cast<const ArrayList*>(__pDataSet->GetAt(i));
172                 hash += _DataRowImpl::GetHashCode(pDataRowArrayList);
173         }
174
175         return hash;
176 }
177
178 _DataSetImpl*
179 _DataSetImpl::GetInstance(DataSet& dataSet)
180 {
181         return dataSet.__pDataSetImpl;
182 }
183
184 const _DataSetImpl*
185 _DataSetImpl::GetInstance(const DataSet& dataSet)
186 {
187         return dataSet.__pDataSetImpl;
188 }
189
190 }} // Tizen::Io
191
192