Tizen 2.0 Release
[framework/web/wrt-commons.git] / modules / auto_save_dao / include / wrt-commons / auto-save-dao / AutoSaveDatabase.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 #ifndef _AUTOSAVEDATABASE_H_
18 #define _AUTOSAVEDATABASE_H_
19
20 #include <dpl/thread.h>
21 #include <dpl/mutex.h>
22 #include <dpl/db/thread_database_support.h>
23
24 extern DPL::Mutex g_autoSaveDbQueriesMutex;
25
26 #define AUTOSAVE_DB_INTERNAL(tlsCommand, InternalType, interface)            \
27     static DPL::ThreadLocalVariable<InternalType> *tlsCommand ## Ptr = NULL; \
28     {                                                                        \
29         DPL::Mutex::ScopedLock lock(&g_autoSaveDbQueriesMutex);              \
30         if (!tlsCommand ## Ptr) {                                            \
31             static DPL::ThreadLocalVariable<InternalType> tmp;               \
32             tlsCommand ## Ptr = &tmp;                                        \
33         }                                                                    \
34     }                                                                        \
35     DPL::ThreadLocalVariable<InternalType> &tlsCommand = *tlsCommand ## Ptr; \
36     if (tlsCommand.IsNull()) { tlsCommand = InternalType(interface); }
37
38 #define AUTOSAVE_DB_SELECT(name, type, interface) \
39     AUTOSAVE_DB_INTERNAL(name, type::Select, interface)
40
41 #define AUTOSAVE_DB_INSERT(name, type, interface) \
42     AUTOSAVE_DB_INTERNAL(name, type::Insert, interface)
43
44 #define AUTOSAVE_DB_UPDATE(name, type, interface) \
45     AUTOSAVE_DB_INTERNAL(name, type::Update, interface)
46
47 #define AUTOSAVE_DB_DELETE(name, type, interface) \
48     AUTOSAVE_DB_INTERNAL(name, type::Delete, interface)
49
50 namespace AutoSaveDB {
51 namespace Interface {
52
53 extern DPL::DB::ThreadDatabaseSupport m_autoSavedbInterface;
54
55 } // namespace Interface
56 } // namespace AutoSaveDB
57
58 #endif /* _AUTOSAVEDATABASE_H_ */
59