CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
icomparer.h
1
2#ifndef _IComparer_h_
3#define _IComparer_h_
4
5#include <detail.h>
6#include <system/object.h>
7#include <defines.h>
8
9namespace System {
10namespace Collections {
11namespace Generic {
12
18template<typename T>
19class ASPOSECPP_SHARED_CLASS IComparer : virtual public Object
20{
22 RTTI_INFO_TEMPLATE_CLASS(System::Collections::Generic::IComparer<T>, System::BaseTypesInfo<System::Object>);
23public:
24
25#ifdef ASPOSE_COMPARE_BY_REF
27 typedef const T& args_type;
28#else
30 typedef T args_type;
31#endif
36 virtual int Compare(args_type x, args_type y) const = 0;
37};
38
42template<class T>
43struct ASPOSECPP_SHARED_CLASS ComparerAdapter
44{
47 {}
51 : m_comparator(comparator)
52 {}
53
56 void set_Comparator(const SharedPtr<IComparer<T> >& comparator)
57 {
58 m_comparator = comparator;
59 }
60
66 template <typename Q = T>
67 typename std::enable_if<detail::has_operator_less<Q>::value, bool>::type operator()(const Q &x, const Q &y) const
68 {
69 // should be "<" operation !!! Not "<="
70 return m_comparator ? m_comparator->Compare(x, y) < 0 : (x < y);
71 }
72
78 template <typename Q = T>
79 typename std::enable_if<!detail::has_operator_less<Q>::value, bool>::type operator()(const Q &x, const Q &y) const
80 {
81 return m_comparator ? m_comparator->Compare(x, y) < 0 : false;
82 }
83
84private:
87};
88
95template<class T>
96class ASPOSECPP_SHARED_CLASS DefaultComparer : public IComparer<T>
97{
98public:
103
104private:
106 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType>);
107
108public:
113 virtual int Compare(typename ThisType::args_type x, typename ThisType::args_type y) const override
114 {
115 if (x == y)
116 return 0;
117
118 if (x < y)
119 return -1;
120
121 return 1;
122 }
123};
124
127template<class T>
128class ASPOSECPP_SHARED_CLASS Comparer : public IComparer<T>
129{
130public:
135
136private:
138 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType>);
139
140protected:
142 {
143 }
144
145public:
149 {
150 static SharedPtr<IComparer<T>> s_default_comparer = System::MakeObject<DefaultComparer<T>>();
151 return s_default_comparer;
152 }
153};
154
155}}}
156
157#endif
Provides a base class for implementations of the System.Collections.Generic.IComparer generic interfa...
Definition: icomparer.h:129
Comparer()
Definition: icomparer.h:141
static SharedPtr< IComparer< T > > get_Default()
Returns a default sort order comparer for the type specified by the generic argument.
Definition: icomparer.h:148
Default comparator class. Uses operator < and operator == to compare values. Objects of this class sh...
Definition: icomparer.h:97
virtual int Compare(typename ThisType::args_type x, typename ThisType::args_type y) const override
Actual data comparison.
Definition: icomparer.h:113
Interface that compares two objects in greater-equal-less sense. Objects of this class should only be...
Definition: icomparer.h:20
const T & args_type
Comparison argument type.
Definition: icomparer.h:27
virtual int Compare(args_type x, args_type y) const =0
Comparison function.
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9
Adapter to use IComparer within STL environment. Uses IComparer if set; otherwise,...
Definition: icomparer.h:44
ComparerAdapter()
Constructs adapter without any comparator available.
Definition: icomparer.h:46
ComparerAdapter(const SharedPtr< System::Collections::Generic::IComparer< T > > &comparator)
Constructs adapter.
Definition: icomparer.h:50
void set_Comparator(const SharedPtr< IComparer< T > > &comparator)
Sets comparator object.
Definition: icomparer.h:56
std::enable_if< detail::has_operator_less< Q >::value, bool >::type operator()(const Q &x, const Q &y) const
Comparison function for types with operator < available.
Definition: icomparer.h:67
std::enable_if<!detail::has_operator_less< Q >::value, bool >::type operator()(const Q &x, const Q &y) const
Comparison function for types with operator < not available.
Definition: icomparer.h:79