- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
#include <iostream>
using namespace std;
// -- robot`s internal closed implementation --
int data1[] = { 0, 1, 2 }; char data2[] = { 42 };
// -- robot`s API
const int *GetMechaData1() { return data1; }
size_t GetMechaData1Size() { return 3; }
const char *GetMechaData2(){ return data2; }
size_t GetMechaData2Size() { return 1; }
// -- pentagon`s internal closed implementation --
//--------------------если T равно U, то результат будет D, а иначе - G------------//
template<class T, class U, class D, class G> struct SelectIF { typedef G type; };
template<class T, class D, class G> struct SelectIF<T, T, D, G> { typedef D type; };
// -- pentagon`s API
enum { eMAXBUFER = 200 * sizeof(int) };
template<class T, size_t N>void AcceptData(const T(&src)[N])
{
typedef typename SelectIF<T, char, int, T>::type Cast;
enum { is_char = std::is_same<T,char>::value };
cout << "received data:\n";
const size_t num = (N<eMAXBUFER) ? N : eMAXBUFER;
if (is_char)
for (size_t n = 0; n < num; ++n)
cout << "char code = " << (Cast)src[n] << " : char = '" << src[n] << "'\n";
else
for (size_t n = 0; n<num; ++n)
cout << "item = " << src[n] << endl;
}
// client code
template<class T, size_t N> struct Adapter
{
typedef Adapter<T, N + 1> Next;
void Pass(const T* data, const size_t num)
{
if (N < num)
{
Next().Pass(data, num);
return;
}
T(&arr)[N] = reinterpret_cast< T(&)[N] > (mBuf);
for (size_t n = 0; n< N; ++n) arr[n] = data[n];
AcceptData(arr);
}
size_t mLen;
T mBuf[eMAXBUFER];
};
template<class T> struct Adapter<T, eMAXBUFER>
{
void Pass(const T* data, const size_t)
{
for (size_t n = 0; n< eMAXBUFER; ++n) mBuf[n] = data[n];
AcceptData(mBuf);
}
size_t mLen;
T mBuf[eMAXBUFER];
};
Adapter<int, 1> adapter1;
Adapter<char, 1> adapter2;
int main()
{
{
const auto data = GetMechaData1();
const auto num = GetMechaData1Size();
adapter1.Pass(data, num);
}
{
const auto data = GetMechaData2();
const auto num = GetMechaData2Size();
adapter2.Pass(data, num);
}
}
Крестушки раскрестушились, а подраться не решились.
Под катом ещё несколько вариантов.