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


Loading...
Searching...
No Matches
VariantUtils.h
Go to the documentation of this file.
1
2// 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
15namespace Lucene {
16
17class LPPAPI VariantUtils {
18public:
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 template <typename VAR>
52 static int32_t hashCode(VAR var) {
53 if (typeOf<String>(var)) {
54 return StringUtils::hashCode(get<String>(var));
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 if (typeOf< Collection<uint8_t> >(var)) {
67 return get< Collection<uint8_t> >(var).hashCode();
68 }
69 if (typeOf< Collection<int32_t> >(var)) {
70 return get< Collection<int32_t> >(var).hashCode();
71 }
72 if (typeOf< Collection<int64_t> >(var)) {
73 return get< Collection<int64_t> >(var).hashCode();
74 }
75 if (typeOf< Collection<double> >(var)) {
76 return get< Collection<double> >(var).hashCode();
77 }
78 if (typeOf< Collection<String> >(var)) {
79 return get< Collection<String> >(var).hashCode();
80 }
81 if (typeOf<LuceneObjectPtr>(var)) {
82 return get<LuceneObjectPtr>(var)->hashCode();
83 }
84 return 0;
85 }
86
87 template <typename FIRST, typename SECOND>
88 static bool equalsType(FIRST first, SECOND second) {
89 return (first.type() == second.type());
90 }
91
92 template <typename FIRST, typename SECOND>
93 static bool equals(FIRST first, SECOND second) {
94 return first.type() == second.type() ? (first == second) : false;
95 }
96
97 template <typename VAR>
98 static int32_t compareTo(VAR first, VAR second) {
99 return first < second ? -1 : (first == second ? 0 : 1);
100 }
101};
102
103}
104
105#endif
Utility template class to handle collections that can be safely copied and shared.
Definition Collection.h:17
Definition VariantUtils.h:17
static TYPE get(const boost::any &var)
Definition VariantUtils.h:20
static int32_t hashCode(VAR var)
Definition VariantUtils.h:52
static bool isNull(VAR var)
Definition VariantUtils.h:47
static bool typeOf(VAR var)
Definition VariantUtils.h:34
static bool isNull(const boost::any &var)
Definition VariantUtils.h:42
static bool equalsType(FIRST first, SECOND second)
Definition VariantUtils.h:88
static int32_t compareTo(VAR first, VAR second)
Definition VariantUtils.h:98
static TYPE get(VAR var)
Definition VariantUtils.h:25
static VariantNull null()
Definition VariantUtils.h:38
static bool equals(FIRST first, SECOND second)
Definition VariantUtils.h:93
Definition AbstractAllTermDocs.h:12

clucene.sourceforge.net