User Tools

Site Tools


blog:dynamictracer

dynamictracer

This sunday, we developed a really cool dynamic tracer for AspectC++.

#include <iostream>
using namespace std;
 
struct X { X (...) {} };
 
typedef char magically_sized_type[43564367];
magically_sized_type &operator << (ostream &os, X);
 
template <typename T> struct detector {
  enum { RET = (sizeof (cout << T()) == sizeof (magically_sized_type)) };
};
 
template <typename T, int> struct Printer {
  static void print (ostream &out, const T &arg) {
    out << arg << endl;
  }
};
 
template <typename T> struct Printer<T,1> {
  static void print (ostream &out, const T &arg) {
    out << "Unprintable type, addr: " << &arg 
	<< "size: " << sizeof (T) << endl;
  }
};
 
template <typename T> void print (ostream &out, const T &arg) {
  Printer<T, detector<T>::RET>::print (out, arg);
}
 
int main() {
  print(std::cout, "Teststring");
  print(std::cout, 4711);
  print(std::cout, 11.1);
}

The interesting part about this pattern is, that it uses the operator« for types which have such an operator, and an alternate implementation, which doesn't need it. I need it for implementing a generic tracer.

Please tell me if someone can construct a type or construction, which fails for this tracer.

~~DISCUSSION~~

tech english aop uni

blog/dynamictracer.txt · Last modified: 2008/10/18 15:56 by 127.0.0.1