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

Recognition of simple variables, pointers, arrays

0 views
Skip to first unread message

Alex Vinokur

unread,
Oct 26, 2002, 3:37:04 AM10/26/02
to
// ################################################
// Recognition of simple variables, pointers, arrays
// ################################################

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


// ========= 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 " << get_is_name (x) << endl; \
cout << "dimensions/depth = " << get_number_of_dimensions(x) << endl; \
cout << "dimension sizes = "; \
{ \
vector<dim_type> v = get_dimension_sizes(x); \
copy (v.begin (), v.end (), ostream_iterator<dim_type> (cout, " ")); \
cout << endl; \
} \
cout << endl

enum var_type { IS_SIMPLE_VARIABLE, IS_POINTER, IS_ARRAY};
class dim_type {
friend ostream& operator<<(ostream &stream_o, const dim_type& instance_i);
public :
size_t size_;
var_type type_;
dim_type (size_t size_i, var_type type_i) : size_ (size_i), type_ (type_i) {}
};
ostream& operator<<(ostream &stream_o, const dim_type& instance_i)
{
string ret_str;
switch (instance_i.type_)
{
case IS_SIMPLE_VARIABLE :
return stream_o << "";
break; // unused
case IS_POINTER :
assert (instance_i.size_ == 0);
return stream_o << "p";
break; // unused
case IS_ARRAY :
return stream_o << instance_i.size_;
break; // unused
default :
assert (0);
break; // unused
}
assert (0);

}

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

template <typename T>
size_t get_number_of_dimensions(T*)
{
T t;
return (get_number_of_dimensions(t) + 1);
}

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


// ------------------------------------------------------
template <typename T>
bool is_simple_variable (const T&) {T t; return get_dimension_sizes(t).empty(); }

// ------------------------------------------------------
template <typename T>
bool is_pointer (const T&)
{
T t;
vector<dim_type> v = get_dimension_sizes(t);
return ((!v.empty()) && (v.front().type_ == IS_POINTER));
}

// ------------------------------------------------------
template <typename T>
bool is_array (const T&)
{
T t;
vector<dim_type> v = get_dimension_sizes(t);
return ((!v.empty()) && (v.front().type_ == IS_ARRAY));
}

// ------------------------------------------------------
template <typename T>
string get_is_name (const T&)
{
T t;
if (is_simple_variable(t)) return "SIMPLE VARIABLE";
if (is_pointer(t)) return "POINTER";
if (is_array(t)) return "ARRAY";
}

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

template <typename T>
vector<dim_type> get_dimension_sizes(T*)
{
T t;
vector<dim_type> v = get_dimension_sizes(t);
v.push_back(dim_type (0, IS_POINTER));
return v;
}

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

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

template <typename T>
string get_basic_typeid(T*)
{
T t;
return (get_basic_typeid(t) + string());
}


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 ai0[0];
int ai1[3];
int ai2[3][5];
int ai3[3][5][7][9][11];
int ai4[10][0][7];
int *pi1;
int ***pi3;
int *api1[10];
int *api2[10][15];
int **api3[20];
string s;
string as1[4][6];
vector<int> v;
vector<int> av1[100][3];

SHOW_IT (i);
SHOW_IT (f);
SHOW_IT (ai0);
SHOW_IT (ai1);
SHOW_IT (ai2);
SHOW_IT (ai3);
SHOW_IT (ai4);
SHOW_IT (pi1);
SHOW_IT (pi3);
SHOW_IT (api1);
SHOW_IT (api2);
SHOW_IT (api3);
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 SIMPLE VARIABLE
dimensions/depth = 0
dimension sizes =


### f ###
typeid = f
basic typeid = f
is SIMPLE VARIABLE
dimensions/depth = 0
dimension sizes =


### ai0 ###
typeid = A0_i
basic typeid = i
is POINTER
dimensions/depth = 1
dimension sizes = p


### ai1 ###
typeid = A3_i
basic typeid = i
is ARRAY
dimensions/depth = 1
dimension sizes = 3


### ai2 ###
typeid = A3_A5_i
basic typeid = i
is ARRAY
dimensions/depth = 2
dimension sizes = 5 3


### ai3 ###
typeid = A3_A5_A7_A9_A11_i
basic typeid = i
is ARRAY
dimensions/depth = 5
dimension sizes = 11 9 7 5 3


### ai4 ###
typeid = A10_A0_A7_i
basic typeid = i
is ARRAY
dimensions/depth = 3
dimension sizes = 7 p 10


### pi1 ###
typeid = Pi
basic typeid = i
is POINTER
dimensions/depth = 1
dimension sizes = p


### pi3 ###
typeid = PPPi
basic typeid = i
is POINTER
dimensions/depth = 3
dimension sizes = p p p


### api1 ###
typeid = A10_Pi
basic typeid = i
is POINTER
dimensions/depth = 2
dimension sizes = p 10


### api2 ###
typeid = A10_A15_Pi
basic typeid = i
is POINTER
dimensions/depth = 3
dimension sizes = p 15 10


### api3 ###
typeid = A20_PPi
basic typeid = i
is POINTER
dimensions/depth = 3
dimension sizes = p p 20


### s ###
typeid = Ss
basic typeid = Ss
is SIMPLE VARIABLE
dimensions/depth = 0
dimension sizes =


### as1 ###
typeid = A4_A6_Ss
basic typeid = Ss
is ARRAY
dimensions/depth = 2
dimension sizes = 6 4


### v ###
typeid = St6vectorIiSaIiEE
basic typeid = St6vectorIiSaIiEE
is SIMPLE VARIABLE
dimensions/depth = 0
dimension sizes =


### av1 ###
typeid = A100_A3_St6vectorIiSaIiEE
basic typeid = St6vectorIiSaIiEE
is ARRAY
dimensions/depth = 2
dimension sizes = 3 100

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


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


tom_usenet

unread,
Oct 28, 2002, 11:26:42 AM10/28/02
to
On Sat, 26 Oct 2002 09:37:04 +0200, "Alex Vinokur"
<ale...@bigfoot.com> wrote:

>// ################################################
>// Recognition of simple variables, pointers, arrays
>// ################################################
>

Have you looked at boost's type traits library? It doesn't have the
multidimensional array support, but it does have the benefit of giving
compile time constants rather runtime boolean values. Adding compile
time constant versions of the multidimensional array stuff would also
be easy.

Class templates are generally more powerful than function ones and are
more useful in template metaprogramming.

Tom

0 new messages