a98f3099b9930e5e0757e718a6f51ba864bd4442
[framework/security/security-server.git] / src / daemon / security_daemon.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  * @file        security_daemon.h
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     1.0
20  * @brief       This is header file of Security Daemon
21  */
22
23 #ifndef WRT_SRC_SECURITY_DAEMON_SECURITY_DAEMON_H
24 #define WRT_SRC_SECURITY_DAEMON_SECURITY_DAEMON_H
25
26 #include <utility>
27 #include <memory>
28 #include <list>
29 #include <dpl/noncopyable.h>
30 #include <dpl/singleton.h>
31 #include <dpl/assert.h>
32
33
34 namespace SecurityDaemon {
35
36 class DaemonService : DPL::Noncopyable {
37   public:
38     virtual void initialize() = 0;
39     virtual void start() = 0;
40     virtual void stop() = 0;
41     virtual void deinitialize() = 0;
42 };
43
44 class SecurityDaemon : DPL::Noncopyable
45 {
46   public:
47     SecurityDaemon();
48
49     void initialize(int& argc, char** argv);
50     int execute();
51     void terminate(int returnValue = 0);
52
53     template<typename ServiceType, typename ...Args>
54     void registerService(Args&&... args)
55     {
56         Assert(!m_initialized && "Too late for registration");
57
58         m_servicesList.push_back(
59                 std::make_shared<ServiceType>(std::forward<Args>(args)...));
60     }
61
62     void shutdown();
63
64   private:
65     bool m_initialized;
66     bool m_terminating;
67     int m_returnValue;
68     typedef std::list<std::shared_ptr<DaemonService>> DaemonServiceList;
69     DaemonServiceList m_servicesList;
70 };
71
72 namespace DatabaseService {
73     void initialize();
74     void deinitialize();
75 };
76
77 } //namespace SecurityDaemon
78
79 typedef DPL::Singleton<SecurityDaemon::SecurityDaemon> SecurityDaemonSingleton;
80
81 #define DAEMON_REGISTER_SERVICE_MODULE(Type)                                \
82     namespace {                                                             \
83         static int initializeModule();                                      \
84         static int initializeModuleHelper = initializeModule();             \
85         int initializeModule()                                              \
86         {                                                                   \
87             (void)initializeModuleHelper;                                   \
88             SecurityDaemonSingleton::Instance().registerService<Type>();    \
89             return 0;                                                       \
90         }                                                                   \
91     }
92
93
94 #endif /* WRT_SRC_SECURITY_DAEMON_SECURITY_DAEMON_H */