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


 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
VariantUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2011 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 "Lucene.h"
12 #include "MiscUtils.h"
13 
14 namespace Lucene
15 {
16  class LPPAPI VariantUtils
17  {
18  public:
19  template <typename TYPE>
20  static TYPE get(boost::any var)
21  {
22  return var.type() == typeid(TYPE) ? boost::any_cast<TYPE>(var) : TYPE();
23  }
24 
25  template <typename TYPE, typename VAR>
26  static TYPE get(VAR var)
27  {
28  return var.type() == typeid(TYPE) ? boost::get<TYPE>(var) : TYPE();
29  }
30 
31  template <typename TYPE, typename VAR>
32  static bool typeOf(VAR var)
33  {
34  return (var.type() == typeid(TYPE));
35  }
36 
37  static VariantNull null()
38  {
39  return VariantNull();
40  }
41 
42  static bool isNull(boost::any var)
43  {
44  return var.empty();
45  }
46 
47  template <typename VAR>
48  static bool isNull(VAR var)
49  {
50  return typeOf<VariantNull>(var);
51  }
52 
53  template <typename VAR>
54  static int32_t hashCode(VAR var)
55  {
56  if (typeOf<String>(var))
57  return StringUtils::hashCode(get<String>(var));
58  if (typeOf<int32_t>(var))
59  return get<int32_t>(var);
60  if (typeOf<int64_t>(var))
61  return (int32_t)get<int64_t>(var);
62  if (typeOf<double>(var))
63  {
64  int64_t longBits = MiscUtils::doubleToLongBits(get<double>(var));
65  return (int32_t)(longBits ^ (longBits >> 32));
66  }
67  if (typeOf< Collection<uint8_t> >(var))
68  return get< Collection<uint8_t> >(var).hashCode();
69  if (typeOf< Collection<int32_t> >(var))
70  return get< Collection<int32_t> >(var).hashCode();
71  if (typeOf< Collection<int64_t> >(var))
72  return get< Collection<int64_t> >(var).hashCode();
73  if (typeOf< Collection<double> >(var))
74  return get< Collection<double> >(var).hashCode();
75  if (typeOf< Collection<String> >(var))
76  return get< Collection<String> >(var).hashCode();
77  if (typeOf<LuceneObjectPtr>(var))
78  return get<LuceneObjectPtr>(var)->hashCode();
79  return 0;
80  }
81 
82  template <typename FIRST, typename SECOND>
83  static bool equalsType(FIRST first, SECOND second)
84  {
85  return (first.type() == second.type());
86  }
87 
88  template <typename FIRST, typename SECOND>
89  static bool equals(FIRST first, SECOND second)
90  {
91  return first.type() == second.type() ? (first == second) : false;
92  }
93 
94  template <typename VAR>
95  static int32_t compareTo(VAR first, VAR second)
96  {
97  return first < second ? -1 : (first == second ? 0 : 1);
98  }
99  };
100 }
101 
102 #endif

clucene.sourceforge.net