Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / include / ResourceCacheManager.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef RCM_RESOURCECACHEMANAGER_H_
22 #define RCM_RESOURCECACHEMANAGER_H_
23
24 #include <list>
25 #include <string>
26 #include <mutex>
27 #include <map>
28
29 #include "CacheTypes.h"
30 #include "DataCache.h"
31
32 namespace OIC
33 {
34     namespace Service
35     {
36         class ResourceCacheManager
37         {
38             public:
39                 class InvalidParameterException: public RCSException
40                 {
41                     public:
42                         InvalidParameterException(std::string &&what)
43                             : RCSException { std::move(what) } {}
44                 };
45                 class HasNoCachedDataException: public RCSException
46                 {
47                     public:
48                         HasNoCachedDataException(std::string &&what)
49                             : RCSException { std::move(what) } {}
50                 };
51
52                 static ResourceCacheManager *getInstance();
53
54                 // throw InvalidParameterException;
55                 CacheID requestResourceCache(
56                     PrimitiveResourcePtr pResource, CacheCB func = NULL,
57                     REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
58
59                 // throw InvalidParameterException;
60                 void cancelResourceCache(CacheID id);
61
62                 // throw InvalidParameterException;
63                 void updateResourceCache(PrimitiveResourcePtr pResource) const;
64                 void updateResourceCache(CacheID id) const;
65
66                 // throw InvalidParameterException;
67                 // throw HasNoCachedDataException;
68                 const RCSResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
69                 const RCSResourceAttributes getCachedData(CacheID id) const;
70
71                 // throw InvalidParameterException;
72                 CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
73                 CACHE_STATE getResourceCacheState(CacheID id) const;
74
75                 // throw InvalidParameterException;
76                 bool isCachedData(PrimitiveResourcePtr pResource) const;
77                 bool isCachedData(CacheID id) const;
78
79             private:
80                 static ResourceCacheManager *s_instance;
81                 static std::mutex s_mutex;
82                 static std::mutex s_mutexForCreation;
83                 static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
84                 std::map<CacheID, DataCachePtr> cacheIDmap;
85
86                 ResourceCacheManager() = default;
87                 ~ResourceCacheManager();
88                 ResourceCacheManager(const ResourceCacheManager &) = delete;
89                 ResourceCacheManager(ResourceCacheManager &&) = delete;
90
91                 ResourceCacheManager &operator=(const ResourceCacheManager &) const = delete;
92                 ResourceCacheManager &operator=(ResourceCacheManager && ) const = delete;
93
94                 static void initializeResourceCacheManager();
95                 DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
96                 DataCachePtr findDataCache(CacheID id) const;
97         };
98     } // namespace Service
99 } // namespace OIC
100
101 #endif /* RCM_RESOURCECACHEMANAGER_H_ */