Lucene++ - a full-featured, c++ search engine
API Documentation


VariantUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2014 Alan Wright. All rights reserved.
3 // Distributable under the terms of either the Apache License (Version 2.0)
4 // or the GNU Lesser General Public License.
6 
7 #ifndef VARIANTUTILS_H
8 #define VARIANTUTILS_H
9 
10 #include <boost/any.hpp>
11 #include <boost/version.hpp>
12 #include "Lucene.h"
13 #include "MiscUtils.h"
14 
15 namespace Lucene {
16 
17 class LPPAPI VariantUtils {
18 public:
19  template <typename TYPE>
20  static TYPE get(const boost::any& var) {
21  return var.type() == typeid(TYPE) ? boost::any_cast<TYPE>(var) : TYPE();
22  }
23 
24  template <typename TYPE, typename VAR>
25  static TYPE get(VAR var) {
26 #if BOOST_VERSION < 105800
27  return var.type() == typeid(TYPE) ? boost::get<TYPE>(var) : TYPE();
28 #else
29  return var.type() == typeid(TYPE) ? boost::relaxed_get<TYPE>(var) : TYPE();
30 #endif
31  }
32 
33  template <typename TYPE, typename VAR>
34  static bool typeOf(VAR var) {
35  return (var.type() == typeid(TYPE));
36  }
37 
38  static VariantNull null() {
39  return VariantNull();
40  }
41 
42  static bool isNull(const boost::any& var) {
43  return var.empty();
44  }
45 
46  template <typename VAR>
47  static bool isNull(VAR var) {
48  return typeOf<VariantNull>(var);
49  }
50 
51  // Specialized hashCode for NumericValue (boost::variant<int32_t, int64_t, double, VariantNull>)
52  static int32_t hashCode(const NumericValue& var) {
53  if (typeOf<VariantNull>(var)) {
54  return 0;
55  }
56  if (typeOf<int32_t>(var)) {
57  return get<int32_t>(var);
58  }
59  if (typeOf<int64_t>(var)) {
60  return (int32_t)get<int64_t>(var);
61  }
62  if (typeOf<double>(var)) {
63  int64_t longBits = MiscUtils::doubleToLongBits(get<double>(var));
64  return (int32_t)(longBits ^ (longBits >> 32));
65  }
66  return 0;
67  }
68 
69  template <typename VAR>
70  static int32_t hashCode(VAR var) {
71  if (typeOf<String>(var)) {
72  return StringUtils::hashCode(get<String>(var));
73  }
74  if (typeOf<int32_t>(var)) {
75  return get<int32_t>(var);
76  }
77  if (typeOf<int64_t>(var)) {
78  return (int32_t)get<int64_t>(var);
79  }
80  if (typeOf<double>(var)) {
81  int64_t longBits = MiscUtils::doubleToLongBits(get<double>(var));
82  return (int32_t)(longBits ^ (longBits >> 32));
83  }
84  if (typeOf< Collection<uint8_t> >(var)) {
85  return get< Collection<uint8_t> >(var).hashCode();
86  }
87  if (typeOf< Collection<int32_t> >(var)) {
88  return get< Collection<int32_t> >(var).hashCode();
89  }
90  if (typeOf< Collection<int64_t> >(var)) {
91  return get< Collection<int64_t> >(var).hashCode();
92  }
93  if (typeOf< Collection<double> >(var)) {
94  return get< Collection<double> >(var).hashCode();
95  }
96  if (typeOf< Collection<String> >(var)) {
97  return get< Collection<String> >(var).hashCode();
98  }
99  if (typeOf<LuceneObjectPtr>(var)) {
100  return get<LuceneObjectPtr>(var)->hashCode();
101  }
102  return 0;
103  }
104 
105  template <typename FIRST, typename SECOND>
106  static bool equalsType(FIRST first, SECOND second) {
107  return (first.type() == second.type());
108  }
109 
110  template <typename FIRST, typename SECOND>
111  static bool equals(FIRST first, SECOND second) {
112  return first.type() == second.type() ? (first == second) : false;
113  }
114 
115  template <typename VAR>
116  static int32_t compareTo(VAR first, VAR second) {
117  return first < second ? -1 : (first == second ? 0 : 1);
118  }
119 };
120 
121 }
122 
123 #endif
static bool isNull(const boost::any &var)
Definition: VariantUtils.h:42
static VariantNull null()
Definition: VariantUtils.h:38
static int64_t doubleToLongBits(double value)
Returns a representation of the specified floating-point value according to the IEEE 754 floating-poi...
static int32_t hashCode(const String &value)
Compute the hash code from string.
static bool typeOf(VAR var)
Definition: VariantUtils.h:34
static int32_t hashCode(VAR var)
Definition: VariantUtils.h:70
Definition: VariantUtils.h:17
Definition: AbstractAllTermDocs.h:12
static int32_t compareTo(VAR first, VAR second)
Definition: VariantUtils.h:116
static bool isNull(VAR var)
Definition: VariantUtils.h:47
static int32_t hashCode(const NumericValue &var)
Definition: VariantUtils.h:52
static bool equals(FIRST first, SECOND second)
Definition: VariantUtils.h:111
static bool equalsType(FIRST first, SECOND second)
Definition: VariantUtils.h:106

clucene.sourceforge.net