LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
flatitemsmodel.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <utility>
13 
14 namespace LC::Util
15 {
16  namespace detail
17  {
18  struct Any
19  {
20  template<typename T>
21  constexpr operator T ();
22  };
23 
24  template<int>
25  struct FC {};
26 
27  template<typename T>
28  constexpr int GetFieldsCount ()
29  {
30  Any any;
31 
32  if constexpr (requires { T { any, any, any, any, any, any }; })
33  return 6;
34  else if constexpr (requires { T { any, any, any, any, any }; })
35  return 5;
36  else if constexpr (requires { T { any, any, any, any }; })
37  return 4;
38  else if constexpr (requires { T { any, any, any }; })
39  return 3;
40  else if constexpr (requires { T { any, any }; })
41  return 2;
42  else if constexpr (requires { T { any }; })
43  return 1;
44  else
45  static_assert (std::is_same_v<T, struct Dummy>, "Don't know how to handle this type");
46  }
47 
48  template<typename T>
49  QVariant GetFieldImpl (const T& item, int idx, FC<1>)
50  {
51  auto [a0] = item;
52  switch (idx)
53  {
54  case 0:
55  return a0;
56  default:
57  return {};
58  }
59  }
60 
61  template<typename T>
62  QVariant GetFieldImpl (const T& item, int idx, FC<2>)
63  {
64  auto [a0, a1] = item;
65  switch (idx)
66  {
67  case 0:
68  return a0;
69  case 1:
70  return a1;
71  default:
72  return {};
73  }
74  }
75 
76  template<typename T>
77  QVariant GetFieldImpl (const T& item, int idx, FC<3>)
78  {
79  auto [a0, a1, a2] = item;
80  switch (idx)
81  {
82  case 0:
83  return a0;
84  case 1:
85  return a1;
86  case 2:
87  return a2;
88  default:
89  return {};
90  }
91  }
92 
93  template<typename T>
94  QVariant GetFieldImpl (const T& item, int idx, FC<4>)
95  {
96  auto [a0, a1, a2, a3] = item;
97  switch (idx)
98  {
99  case 0:
100  return a0;
101  case 1:
102  return a1;
103  case 2:
104  return a2;
105  case 3:
106  return a3;
107  default:
108  return {};
109  }
110  }
111 
112  template<typename T>
113  QVariant GetFieldImpl (const T& item, int idx, FC<5>)
114  {
115  auto [a0, a1, a2, a3, a4] = item;
116  switch (idx)
117  {
118  case 0:
119  return a0;
120  case 1:
121  return a1;
122  case 2:
123  return a2;
124  case 3:
125  return a3;
126  case 4:
127  return a4;
128  default:
129  return {};
130  }
131  }
132 
133  template<typename T>
134  QVariant GetFieldImpl (const T& item, int idx, FC<6>)
135  {
136  auto [a0, a1, a2, a3, a4, a5] = item;
137  switch (idx)
138  {
139  case 0:
140  return a0;
141  case 1:
142  return a1;
143  case 2:
144  return a2;
145  case 3:
146  return a3;
147  case 4:
148  return a4;
149  case 5:
150  return a5;
151  default:
152  return {};
153  }
154  }
155 
156  template<typename T>
157  QVariant GetField (const T& item, int idx)
158  {
159  return GetFieldImpl (item, idx, FC<GetFieldsCount<T> ()> {});
160  }
161  }
162 
163  template<typename T>
164  class FlatItemsModel : public FlatItemsModelTypedBase<T>
165  {
166  public:
167  using FlatItemsModelTypedBase<T>::FlatItemsModelTypedBase;
168  protected:
169  QVariant GetData (int row, int col, int role) const override
170  {
171  if (role == this->DataRole)
172  return QVariant::fromValue (this->Items_.at (row));
173 
174  if (role != Qt::DisplayRole)
175  return {};
176 
177  return detail::GetField (this->Items_.at (row), col);
178  }
179  };
180 }
LC::Util::FlatItemsModelBase::DataRole
constexpr static auto DataRole
Definition: flatitemsmodelbase.h:32
LC::Util::detail::Any
Definition: flatitemsmodel.h:30
LC::Util::detail::GetFieldsCount
constexpr int GetFieldsCount()
Definition: flatitemsmodel.h:40
LC::Util
Definition: icoreproxy.h:33
LC::Util::FlatItemsModel::GetData
QVariant GetData(int row, int col, int role) const override
Definition: flatitemsmodel.h:175
LC::Util::FlatItemsModelTypedBase::Items_
QVector< T > Items_
Definition: flatitemsmodeltypedbase.h:31
flatitemsmodeltypedbase.h
LC::Util::detail::GetFieldImpl
QVariant GetFieldImpl(const T &item, int idx, FC< 1 >)
Definition: flatitemsmodel.h:61
LC::Util::detail::FC
Definition: flatitemsmodel.h:37
LC::Util::detail::GetField
QVariant GetField(const T &item, int idx)
Definition: flatitemsmodel.h:169