Hello there,
I am playing around with template metaprograming: I am trying to
redefines my own types. But I am facing a small issue, where I cannot
describe the whole implementation in one single class. Instead I have
to separate implementation for binary and ascii in two different
classes as I don't know how to use the traits technique for
constructors.
Any pointers very welcome !
Mathieu
Ps: My goal is collapse MyASCIITypes and MyTypes in one single
templated class.
Full source is:
#include <iostream> // cout
#include <sstream> // istringstream
typedef enum {
ASCII= 0,
BINARY
} Modes;
// Define my types
typedef enum {
Type1 = 1,
Type2,
Type3,
Type4,
} Types;
// Defines my value multiplicity
typedef enum {
VM1,
VM2,
VM3,
VM4,
VM5,
VM6,
VM8,
} VMTypes;
// Mapping from Enum type to a Mode
template<int T> struct EnumTypeToMode;
template<> struct EnumTypeToMode<Type1>
{ enum { Mode = ASCII }; };
template<> struct EnumTypeToMode<Type2>
{ enum { Mode = BINARY }; };
template<> struct EnumTypeToMode<Type3>
{ enum { Mode = BINARY }; };
template<> struct EnumTypeToMode<Type4>
{ enum { Mode = ASCII }; };
// Mapping from Enum type to a real type
template<int T> struct TypeEnumToType;
template<> struct TypeEnumToType<Type1>
{ typedef float Type; };
template<> struct TypeEnumToType<Type2>
{ typedef double Type; };
template<> struct TypeEnumToType<Type3>
{ typedef int Type; };
template<> struct TypeEnumToType<Type4>
{ typedef unsigned int Type; };
template<int T> struct ValueEnumToLength;
template<> struct ValueEnumToLength<VM1>
{ enum { Len = 1 }; };
template<> struct ValueEnumToLength<VM2>
{ enum { Len = 2 }; };
template<> struct ValueEnumToLength<VM3>
{ enum { Len = 3 }; };
template<> struct ValueEnumToLength<VM4>
{ enum { Len = 4 }; };
template<> struct ValueEnumToLength<VM5>
{ enum { Len = 5 }; };
template<> struct ValueEnumToLength<VM6>
{ enum { Len = 6 }; };
template<> struct ValueEnumToLength<VM8>
{ enum { Len = 8 }; };
// forward declaration
template<int T> class ModeImplementation;
// Definition of my type
template<int Type, int Size>
class MyTypes
{
public:
typename TypeEnumToType<Type>::Type
Data[ValueEnumToLength<Size>::Len];
int GetLength() const {
return ValueEnumToLength<Size>::Len;
}
void Print() const {
ModeImplementation< EnumTypeToMode<Type>::Mode >::Print(Data,
GetLength());
}
};
// Implementation for Mode=ASCII
template<> class ModeImplementation<ASCII>
{
public:
template <typename T>
static inline void Print(const T array[], const int len) {
std::cout << "ASCII:" << array[0]; // garantee to be >1
for(int i=1; i<len; ++i)
std::cout << "," << array[i];
std::cout << std::endl;
}
};
// Implementation for Mode=BINARY
template<> class ModeImplementation<BINARY>
{
public:
template <typename T>
static inline void Print(const T array[], const int len) {
std::cout << "BINARY:" << array[0];
for(int i=1; i<len; ++i)
std::cout << "," << array[i];
std::cout << std::endl;
}
};
template<int Type, int Size>
class MyASCIITypes : public MyTypes<Type,Size>
{
public:
MyASCIITypes(const char array[])
{
const int length = ValueEnumToLength<Size>::Len;
std::istringstream is(array);
int i = 0;
while( is >> this->Data[i++] );
assert( length == i-1 );
}
};
int main()
{
MyTypes<Type4,VM4> t_bin = { 1, 2, 3, 4 };
t_bin.Print();
MyASCIITypes<Type3,VM4> t_text = "1 2 3 4";
t_text.Print();
return 0;
} 1 2333
And to answer myself... I did not find a very elegant solution, but
still is fairly decent. Complete souce is (*). It would be nicer if
partial specialization would allow to define is for all ASCII type
instead of one at a time (in my case Type4 only is defined).
(*)
#include <iostream> // cout
#include <sstream>
typedef enum {
ASCII= 0,
BINARY
} Modes;
// Define my types
typedef enum {
Type1 = 1,
Type2,
Type3,
Type4,
} Types;
// Defines my value multiplicity
typedef enum {
VM1,
VM2,
VM3,
VM4,
VM5,
VM6,
VM8,
} VMTypes;
// Mapping from Enum type to a Mode
template<int T> struct EnumTypeToMode;
template<> struct EnumTypeToMode<Type1>
{ enum { Mode = ASCII }; };
template<> struct EnumTypeToMode<Type2>
{ enum { Mode = BINARY }; };
template<> struct EnumTypeToMode<Type3>
{ enum { Mode = BINARY }; };
template<> struct EnumTypeToMode<Type4>
{ enum { Mode = ASCII }; };
// Mapping from Enum type to a real type
template<int T> struct TypeEnumToType;
template<> struct TypeEnumToType<Type1>
{ typedef float Type; };
template<> struct TypeEnumToType<Type2>
{ typedef double Type; };
template<> struct TypeEnumToType<Type3>
{ typedef int Type; };
template<> struct TypeEnumToType<Type4>
{ typedef unsigned int Type; };
template<int T> struct ValueEnumToLength;
template<> struct ValueEnumToLength<VM1>
{ enum { Len = 1 }; };
template<> struct ValueEnumToLength<VM2>
{ enum { Len = 2 }; };
template<> struct ValueEnumToLength<VM3>
{ enum { Len = 3 }; };
template<> struct ValueEnumToLength<VM4>
{ enum { Len = 4 }; };
template<> struct ValueEnumToLength<VM5>
{ enum { Len = 5 }; };
template<> struct ValueEnumToLength<VM6>
{ enum { Len = 6 }; };
template<> struct ValueEnumToLength<VM8>
{ enum { Len = 8 }; };
// forward declaration
template<int T> class ModeImplementation;
// Definition of my type
template<int Type, int Size>
class MyTypes
{
public:
typename TypeEnumToType<Type>::Type
Data[ValueEnumToLength<Size>::Len];
int GetLength() const {
return ValueEnumToLength<Size>::Len;
}
void Print() const {
ModeImplementation< EnumTypeToMode<Type>::Mode >::Print(Data,
GetLength());
}
};
// Implementation for Mode=ASCII
template<> class ModeImplementation<ASCII>
{
public:
template <typename T>
static inline void Print(const T array[], const int len) {
std::cout << "ASCII:" << array[0]; // garantee to be >1
for(int i=1; i<len; ++i)
std::cout << "," << array[i];
std::cout << std::endl;
}
};
// Implementation for Mode=BINARY
template<> class ModeImplementation<BINARY>
{
public:
template <typename T>
static inline void Print(const T array[], const int len) {
std::cout << "BINARY:" << array[0];
for(int i=1; i<len; ++i)
std::cout << "," << array[i];
std::cout << std::endl;
}
};
template<int Size>
class MyTypes<Type4,Size>
{
public:
MyTypes(const char array[])
{
const int length = ValueEnumToLength<Size>::Len;
std::istringstream is(array);
int i = 0;
while( is >> this->Data[i++] );
assert( length == i-1 );
}
int GetLength() const {
return ValueEnumToLength<Size>::Len;
}
void Print() const {
ModeImplementation<ASCII>::Print(Data, GetLength());
}
private:
typename TypeEnumToType<Type4>::Type
Data[ValueEnumToLength<Size>::Len];
};
int main()
{
MyTypes<Type3,VM4> t_bin = { 1, 2, 3, 4 };
t_bin.Print();
MyTypes<Type4,VM4> t_text = "1 2 3 4";
t_text.Print();
return 0;
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave |
last post by:
For those who might be so inclined, I was wondering if I might get honest
critiques of my first real venture into template metaprogramming. This
template metaprogram sorts a list of integers at...
|
by: Dave |
last post by:
Would people agree with the statement that to a large degree, using template
metaprogramming techniques turns a C++ compiler into a C++ interpreter (but
just for the metaprogrammed portions of the...
|
by: Mohammad |
last post by:
Hi,
Is it possible to disable a method of a template class depending on
the typename at compile time?
thanks!
|
by: Protoman |
last post by:
I've been looking at template metaprogramming. It seems really cool,
make the compiler do most of the work. I have very simple program that
uses TMP,it calculates the square of a number, but it...
|
by: JohnQ |
last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post).
It would be more than a little bit nice if C++ was much "cleaner" (less
complex) so that it wasn't a major world wide...
|
by: Joe |
last post by:
Hi,
I found a concept named template metaprogramming that can be used in C+
+ code at compile-time. I am a beginner at C++. But I am a programmer
on the .NET platform. Do you know if template...
|
by: Ted |
last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num-
analysis
in the belief that the topic is of interest to some in both groups.
I am building my toolkit, in support of my efforts in...
|
by: iapx86 |
last post by:
My parser project calls for a computed goto (see code below).
The C preprocessor delivers the desired result, but is ugly.
Template metaprogramming delivers results I do not understand.
Can...
|
by: nooneinparticular314159 |
last post by:
Hello. If I declare the following:
template<int a, int b, int SomeArray>
class DoSomething{
public:
..
..
..
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |