2d0bd378a0522d6dbbcd6dc4f2db813b5e07dda4
[platform/core/api/webapi-plugins.git] / src / messaging / MsgCommon / AbstractFilter.h
1 /*
2  * Copyright (c) 2015 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 __TIZEN_TIZEN_ABSTRACT_FILTER_H__
18 #define __TIZEN_TIZEN_ABSTRACT_FILTER_H__
19
20 #include <memory>
21 #include <sstream>
22 #include <vector>
23 #include <time.h>
24 //#include <JSArray.h>
25
26 #include "Any.h"
27 #include "common/filter-utils.h"
28
29 namespace extension {
30 namespace tizen {
31
32 class AbstractFilter;
33 typedef std::shared_ptr<AbstractFilter> AbstractFilterPtr;
34 typedef std::vector<AbstractFilterPtr> AbstractFilterPtrVector;
35
36 struct AbstractFilterHolder {
37   AbstractFilterPtr ptr;
38 };
39
40 enum FilterType { ABSTRACT_FILTER = 0, ATTRIBUTE_FILTER, ATTRIBUTE_RANGE_FILTER, COMPOSITE_FILTER };
41
42 class FilterableObject {
43  public:
44   virtual ~FilterableObject() {
45   }
46
47   virtual bool isMatchingAttribute(const std::string& attribute_name,
48                                    const common::AttributeMatchFlag match_flag,
49                                    AnyPtr match_value) const = 0;
50
51   virtual bool isMatchingAttributeRange(const std::string& attribute_name, AnyPtr initial_value,
52                                         AnyPtr end_value) const = 0;
53 };
54
55 class AbstractFilter {
56  public:
57   AbstractFilter(FilterType type = ABSTRACT_FILTER);
58   virtual ~AbstractFilter();
59
60   FilterType getFilterType() const;
61
62   //    static JSValueRef makeJSValue(JSContextRef context, AbstractFilterPtr priv);
63   //    static AbstractFilterPtr getPrivateObject(JSContextRef context,
64   //            JSValueRef value);
65
66   virtual bool isMatching(const FilterableObject* const filtered_object) const;
67
68  protected:
69   FilterType m_filter_type;
70 };
71
72 // class JSFilterArray : public Common::JSArray<AbstractFilterPtr> {
73 // public:
74 //    JSFilterArray(JSContextRef ctx, JSObjectRef array):
75 //            JSArray<AbstractFilterPtr>(ctx, array, AbstractFilter::getPrivateObject,
76 //                    AbstractFilter::makeJSValue)
77 //    {
78 //    }
79 //    JSFilterArray(JSContextRef ctx):
80 //            JSArray<AbstractFilterPtr>(ctx, AbstractFilter::getPrivateObject,
81 //                    AbstractFilter::makeJSValue)
82 //    {
83 //    }
84 //    void operator=(const std::vector<AbstractFilterPtr>& list)
85 //    {
86 //        overwrite(list);
87 //    }
88 //};
89
90 namespace FilterUtils {
91
92 bool isAnyStringMatching(const std::string& key, const std::vector<std::string>& values,
93                          common::AttributeMatchFlag flag);
94 bool isTimeStampInRange(const time_t& time_stamp, tizen::AnyPtr& initial_value,
95                         tizen::AnyPtr& end_value);
96 bool isInRange(const unsigned long messageCount, const unsigned long initial_value,
97                const unsigned long end_value);
98 inline bool isBetweenTimeRange(const time_t timestamp, const time_t from, const time_t to) {
99   return (difftime(timestamp, from) >= 0.0) && (difftime(to, timestamp) >= 0.0);
100 }
101
102 /*
103  * isMatching() behavior for different common::AttributeMatchFlags:
104  * - EXACTLY - value and filter_value's value must be identical
105  * - EXISTS - false is returned always. isMatching do not check if EXISTS filter matches.
106  *   Testing against EXISTS match flag has to be done in another function, i.e. before calling
107  * isMatching.
108  * - FULLSTRING, CONTAINS, STARTSWITH, ENDSWITH - applicable for strings only,
109  *   used to filter values of other types result in no-match
110  */
111 bool isMatching(const Any& filter_value, const std::string& value, common::AttributeMatchFlag flag);
112 bool isMatching(const Any& filter_value, const bool value, common::AttributeMatchFlag flag);
113 bool isMatching(const Any& filter_value, const unsigned long value,
114                 common::AttributeMatchFlag flag);
115
116 }  // namespace FilterUtils
117
118 }  // Tizen
119 }  // DeviceAPI
120
121 #endif  // __TIZEN_TIZEN_ABSTRACT_FILTER_H__