Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recognition of arrays and simple variables

1 view
Skip to first unread message

Alex Vinokur

unread,
Oct 24, 2002, 12:34:13 AM10/24/02
to

// ##################################
// Array & variable recognition
// Functions below are based on
// an Andrey Tarasevich's article :
// http://groups.google.com/groups?selm=3DB4E7BC.2030509%40hotmail.com
// ##################################

// ==============================================
// Windows 2000 Professional
// MinGW 2.0.0.-2
// gcc/g++ version 3.2 (mingw special 20020817-1)
// ==============================================


// ==============================================
// Alex Vinokur
// mailto:ale...@go.to
// http://go.to/alexvn
// -------------------
// 2002-10-24
// ==============================================

// ========= C++ code : BEGIN =========
// File main.cpp

#include <stdio.h>
#include <assert.h>
#include<string>
#include<vector>
#include<typeinfo>
#include<iterator>
#include<iostream>
#include<iomanip>
using namespace std;

#define SHOW_IT(x) \
cout << endl; \
cout << "\t### " << #x << " ###" << endl; \
cout << "typeid = " << typeid(x).name() << endl; \
cout << "basic typeid = " << get_basic_typeid (x) << endl; \
cout << (is_array(x) ? "is array" : "is not array") << endl; \
cout << "dimensionality = " << get_dimensionality(x) << endl; \
cout << "dimension sizes = "; \
{ \
vector<size_t> v = get_dimension_sizes(x); \
assert (get_dimensionality(x) == v.size()); \
copy (v.begin (), v.end (), ostream_iterator<size_t> (cout, " ")); \
cout << endl; \
} \
cout << endl

// ------------------------------------------------------
template <typename T>
size_t get_dimensionality(T&) { return 0; }

template <typename T, size_t N>
size_t get_dimensionality(T(&)[N])
{
T t;
return (get_dimensionality(t) + 1);
}


// ------------------------------------------------------
template <typename T>
bool is_array (const T&) {T t; return get_dimensionality (t); }


// ------------------------------------------------------
template <typename T>
vector<size_t> get_dimension_sizes(T&) { return vector<size_t>(); }

template <typename T, size_t N>
vector<size_t> get_dimension_sizes(T(&)[N])
{
T t;
vector<size_t> v = get_dimension_sizes(t);
v.push_back(N);
return v;
}

// ------------------------------------------------------
template <typename T>
string get_basic_typeid(T&) { return typeid(T).name(); }

template <typename T, size_t N>
string get_basic_typeid(T(&)[N])
{
T t;
return (get_basic_typeid(t) + string());
}


// ------------------------------------------------------
// ------------------------------------------------------
int main ()
{
int i;
float f;
int ai1[3];
int ai2[3][5];
int ai3[3][5][7][9][11];
string s;
string as1[4][6];
vector<int> v;
vector<int> av1[100][3];

SHOW_IT (i);
SHOW_IT (f);
SHOW_IT (ai1);
SHOW_IT (ai2);
SHOW_IT (ai3);
SHOW_IT (s);
SHOW_IT (as1);
SHOW_IT (v);
SHOW_IT (av1);

return 0;
}

// ========= C++ code : END ===========

// ========= Compilation & Run : BEGIN =========

% g++ main.cpp

% a.exe


### i ###
typeid = i
basic typeid = i
is not array
dimensionality = 0
dimension sizes =


### f ###
typeid = f
basic typeid = f
is not array
dimensionality = 0
dimension sizes =


### ai1 ###
typeid = A3_i
basic typeid = i
is array
dimensionality = 1
dimension sizes = 3


### ai2 ###
typeid = A3_A5_i
basic typeid = i
is array
dimensionality = 2
dimension sizes = 5 3


### ai3 ###
typeid = A3_A5_A7_A9_A11_i
basic typeid = i
is array
dimensionality = 5
dimension sizes = 11 9 7 5 3


### s ###
typeid = Ss
basic typeid = Ss
is not array
dimensionality = 0
dimension sizes =


### as1 ###
typeid = A4_A6_Ss
basic typeid = Ss
is array
dimensionality = 2
dimension sizes = 6 4


### v ###
typeid = St6vectorIiSaIiEE
basic typeid = St6vectorIiSaIiEE
is not array
dimensionality = 0
dimension sizes =


### av1 ###
typeid = A100_A3_St6vectorIiSaIiEE
basic typeid = St6vectorIiSaIiEE
is array
dimensionality = 2
dimension sizes = 3 100

// ========= Compilation & Run : END ===========

==================
Alex Vinokur
mailto:ale...@go.to
http://up.to/alexvn
==================


0 new messages