Hazelcast C++ Client
EvictionPolicyEvaluatorProvider.h
1 /*
2  * Copyright (c) 2008-2017, Hazelcast, Inc. 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 #ifndef HAZELCAST_CLIENT_INTERNAL_EVICTION_EVICTIONPOLICYEVALUATORPROVIDER_H_
17 #define HAZELCAST_CLIENT_INTERNAL_EVICTION_EVICTIONPOLICYEVALUATORPROVIDER_H_
18 
19 #include <sstream>
20 #include <boost/shared_ptr.hpp>
21 
22 #include "hazelcast/client/exception/IllegalArgumentException.h"
23 #include "hazelcast/client/internal/eviction/impl/comparator/LRUEvictionPolicyComparator.h"
24 #include "hazelcast/client/internal/eviction/impl/comparator/LFUEvictionPolicyComparator.h"
25 #include "hazelcast/client/internal/eviction/impl/comparator/RandomEvictionPolicyComparator.h"
26 #include "hazelcast/client/internal/eviction/EvictionConfiguration.h"
27 #include "hazelcast/client/internal/eviction/EvictionPolicyEvaluator.h"
28 #include "hazelcast/client/internal/eviction/impl/evaluator/DefaultEvictionPolicyEvaluator.h"
29 
30 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
31 #pragma warning(push)
32 #pragma warning(disable: 4251) //for dll export
33 #endif
34 
35 namespace hazelcast {
36  namespace client {
37  namespace internal {
38  namespace eviction {
43  public:
51  template<typename MAPKEY, typename MAPVALUE, typename A, typename E>
52  static std::auto_ptr<EvictionPolicyEvaluator<MAPKEY, MAPVALUE, A, E> > getEvictionPolicyEvaluator(
53  const boost::shared_ptr<EvictionConfiguration<MAPKEY, MAPVALUE> > &evictionConfig) {
54  if (evictionConfig.get() == NULL) {
55  return std::auto_ptr<EvictionPolicyEvaluator<MAPKEY, MAPVALUE, A, E> >();
56  }
57 
58  boost::shared_ptr<EvictionPolicyComparator<MAPKEY, MAPVALUE> > evictionPolicyComparator;
59 
60  const boost::shared_ptr<EvictionPolicyComparator<MAPKEY, MAPVALUE> > &comparator = evictionConfig->getComparator();
61  if (comparator.get() != NULL) {
62  evictionPolicyComparator = comparator;
63  } else {
64  EvictionPolicyType evictionPolicyType = evictionConfig->getEvictionPolicyType();
65  evictionPolicyComparator = createEvictionPolicyComparator<MAPKEY, MAPVALUE>(evictionPolicyType);
66  }
67 
68  return std::auto_ptr<EvictionPolicyEvaluator<MAPKEY, MAPVALUE, A, E> >(
69  new impl::evaluator::DefaultEvictionPolicyEvaluator<MAPKEY, MAPVALUE, A, E>(
70  evictionPolicyComparator));
71  }
72 
73  private:
74  template<typename A, typename E>
75  static boost::shared_ptr<EvictionPolicyComparator<A, E> > createEvictionPolicyComparator(
76  EvictionPolicyType evictionPolicyType) {
77  switch (evictionPolicyType) {
78  case LRU:
79  return boost::shared_ptr<EvictionPolicyComparator<A, E> >(
80  new impl::comparator::LRUEvictionPolicyComparator<A, E>());
81  case LFU:
82  return boost::shared_ptr<EvictionPolicyComparator<A, E> >(
83  new impl::comparator::LFUEvictionPolicyComparator<A, E>());
84  case RANDOM:
85  return boost::shared_ptr<EvictionPolicyComparator<A, E> >(
86  new impl::comparator::RandomEvictionPolicyComparator<A, E>());
87  case NONE:
88  return boost::shared_ptr<EvictionPolicyComparator<A, E> >();
89  default:
90  std::ostringstream out;
91  out << "Unsupported eviction policy type: " << (int) evictionPolicyType;
92  throw exception::IllegalArgumentException(out.str());
93  }
94  }
95 
96  //Non-constructable class
97  EvictionPolicyEvaluatorProvider();
98 
99  ~EvictionPolicyEvaluatorProvider();
100  };
101 
102  }
103  }
104  }
105 };
106 
107 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
108 #pragma warning(pop)
109 #endif
110 
111 #endif /* HAZELCAST_CLIENT_INTERNAL_EVICTION_EVICTIONPOLICYEVALUATORPROVIDER_H_ */
Provider to get any kind (EvictionPolicyType) of EvictionPolicyEvaluator.
Definition: EvictionPolicyEvaluatorProvider.h:42
Definition: MapEntryView.h:32
static std::auto_ptr< EvictionPolicyEvaluator< MAPKEY, MAPVALUE, A, E > > getEvictionPolicyEvaluator(const boost::shared_ptr< EvictionConfiguration< MAPKEY, MAPVALUE > > &evictionConfig)
Gets the EvictionPolicyEvaluator implementation specified with.
Definition: EvictionPolicyEvaluatorProvider.h:52
Interface for configuration information about eviction.
Definition: EvictionConfiguration.h:38