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 <system/icomparable.h>
8#include <defines.h>
9
10namespace System {
11namespace Collections {
12namespace Generic {
13
19template<typename T>
20class ASPOSECPP_SHARED_CLASS IComparer : virtual public Object
21{
23 RTTI_INFO_TEMPLATE_CLASS(System::Collections::Generic::IComparer<T>, System::BaseTypesInfo<System::Object>);
24public:
25
26#ifdef ASPOSE_COMPARE_BY_REF
28 typedef const T& args_type;
29#else
31 typedef T args_type;
32#endif
37 virtual int Compare(args_type x, args_type y) const = 0;
38};
39
43template<class T>
44struct ASPOSECPP_SHARED_CLASS ComparerAdapter
45{
48 {}
52 : m_comparator(comparator)
53 {}
54
57 void set_Comparator(const SharedPtr<IComparer<T> >& comparator)
58 {
59 m_comparator = comparator;
60 }
61
67 template <typename Q = T>
68 typename std::enable_if<detail::has_operator_less<Q>::value, bool>::type operator()(const Q &x, const Q &y) const
69 {
70 // should be "<" operation !!! Not "<="
71 return m_comparator ? m_comparator->Compare(x, y) < 0 : (x < y);
72 }
73
79 template <typename Q = T>
80 typename std::enable_if<!detail::has_operator_less<Q>::value, bool>::type operator()(const Q &x, const Q &y) const
81 {
82 return m_comparator ? m_comparator->Compare(x, y) < 0 : false;
83 }
84
85private:
88};
89
96template<class T, typename = void>
97class ASPOSECPP_SHARED_CLASS DefaultComparer : public IComparer<T>
98{
99public:
104
105private:
107 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType>);
108
109public:
114 virtual int Compare(typename ThisType::args_type x, typename ThisType::args_type y) const override
115 {
116 if (x == y)
117 return 0;
118
119 if (x < y)
120 return -1;
121
122 return 1;
123 }
124};
125
126template <typename T>
127class ASPOSECPP_SHARED_CLASS DefaultComparer<T, typename std::enable_if<std::is_base_of<IComparable<T>, T>::value>::type>
128 : public IComparer<T>
129{
130public:
132 using BaseType = IComparer<T>;
134 using ThisType = DefaultComparer<T>;
135
136private:
138 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType>);
139
140public:
145 virtual int Compare(typename ThisType::args_type x, typename ThisType::args_type y) const override
146 {
147 return const_cast<T&>(x).CompareTo(const_cast<T&>(y));
148 }
149};
150
153template<class T>
154class ASPOSECPP_SHARED_CLASS Comparer : public IComparer<T>
155{
156public:
161
162private:
164 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType>);
165
166protected:
168 {
169 }
170
171public:
175 {
176 static SharedPtr<IComparer<T>> s_default_comparer = System::MakeObject<DefaultComparer<T>>();
177 return s_default_comparer;
178 }
179};
180
181}}}
182
183#endif
Provides a base class for implementations of the System.Collections.Generic.IComparer generic interfa...
Definition: icomparer.h:155
Comparer()
Definition: icomparer.h:167
static SharedPtr< IComparer< T > > get_Default()
Returns a default sort order comparer for the type specified by the generic argument.
Definition: icomparer.h:174
Default comparator class. Uses operator < and operator == to compare values. Objects of this class sh...
Definition: icomparer.h:98
virtual int Compare(typename ThisType::args_type x, typename ThisType::args_type y) const override
Actual data comparison.
Definition: icomparer.h:114
Interface that compares two objects in greater-equal-less sense. Objects of this class should only be...
Definition: icomparer.h:21
const T & args_type
Comparison argument type.
Definition: icomparer.h:28
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:51
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
std::enable_if_t<!std::is_floating_point< TA >::value &&!std::is_floating_point< TB >::value, int > Compare(const TA &a, const TB &b)
Compares two values.
Definition: primitive_types.h:113
Adapter to use IComparer within STL environment. Uses IComparer if set; otherwise,...
Definition: icomparer.h:45
ComparerAdapter()
Constructs adapter without any comparator available.
Definition: icomparer.h:47
ComparerAdapter(const SharedPtr< System::Collections::Generic::IComparer< T > > &comparator)
Constructs adapter.
Definition: icomparer.h:51
void set_Comparator(const SharedPtr< IComparer< T > > &comparator)
Sets comparator object.
Definition: icomparer.h:57
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:68
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:80