.dp-highlighter { font-family: "Consolas", "Monaco", "Courier New", Courier, monospace; font-size: 12px; background-color: #E7E5DC; width: 99%; overflow: auto; margin: 18px 0 18px 0 !important; padding-top: 1px; /* adds a little border on top when controls are hidden */ } /* clear styles */ .dp-highlighter ol, .dp-highlighter ol li, .dp-highlighter ol li span { margin: 0; padding: 0; border: none; } .dp-highlighter a, .dp-highlighter a:hover { background: none; border: none; padding: 0; margin: 0; } .dp-highlighter .bar { padding-left: 45px; } .dp-highlighter.collapsed .bar, .dp-highlighter.nogutter .bar { padding-left: 0px; } .dp-highlighter ol { list-style: decimal; /* for ie */ background-color: #fff; margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */ padding: 0px; color: #5C5C5C; } .dp-highlighter.nogutter ol, .dp-highlighter.nogutter ol li { list-style: none !important; margin-left: 0px !important; } .dp-highlighter ol li, .dp-highlighter .columns div { list-style: decimal-leading-zero; /* better look for others, override cascade from OL */ list-style-position: outside !important; border-left: 3px solid #6CE26C; background-color: #F8F8F8; color: #5C5C5C; padding: 0 3px 0 10px !important; margin: 0 !important; line-height: 14px; } .dp-highlighter.nogutter ol li, .dp-highlighter.nogutter .columns div { border: 0; } .dp-highlighter .columns { background-color: #F8F8F8; color: gray; overflow: hidden; width: 100%; } .dp-highlighter .columns div { padding-bottom: 5px; } .dp-highlighter ol li.alt { background-color: #FFF; color: inherit; } .dp-highlighter ol li span { color: black; background-color: inherit; } /* Adjust some properties when collapsed */ .dp-highlighter.collapsed ol { margin: 0px; } .dp-highlighter.collapsed ol li { display: none; } /* Additional modifications when in print-view */ .dp-highlighter.printing { border: none; } .dp-highlighter.printing .tools { display: none !important; } .dp-highlighter.printing li { display: list-item !important; } /* Styles for the tools */ .dp-highlighter .tools { padding: 3px 8px 3px 10px; font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: #f8f8f8; padding-bottom: 10px; border-left: 3px solid #6CE26C; } .dp-highlighter.nogutter .tools { border-left: 0; } .dp-highlighter.collapsed .tools { border-bottom: 0; } .dp-highlighter .tools a { font-size: 9px; color: #a0a0a0; background-color: inherit; text-decoration: none; margin-right: 10px; } .dp-highlighter .tools a:hover { color: red; background-color: inherit; text-decoration: underline; } /* About dialog styles */ .dp-about { background-color: #fff; color: #333; margin: 0px; padding: 0px; } .dp-about table { width: 100%; height: 100%; font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; } .dp-about td { padding: 10px; vertical-align: top; } .dp-about .copy { border-bottom: 1px solid #ACA899; height: 95%; } .dp-about .title { color: red; background-color: inherit; font-weight: bold; } .dp-about .para { margin: 0 0 4px 0; } .dp-about .footer { background-color: #ECEADB; color: #333; border-top: 1px solid #fff; text-align: right; } .dp-about .close { font-size: 11px; font-family: Tahoma, Verdana, Arial, sans-serif !important; background-color: #ECEADB; color: #333; width: 60px; height: 22px; } /* Language specific styles */ .dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; } .dp-highlighter .string { color: blue; background-color: inherit; } .dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; } .dp-highlighter .preprocessor { color: gray; background-color: inherit; }

Sunday, November 9, 2008

boost::any (1)

Sometimes you might want to store unrelated types in the same container, and convey the data from one point to another without caring much about it's type. C++ containers are templated on the type and can only store objects of the same type in the same container. Traditionally, this problem has been solved by storing pointers to objects as void pointers, or using discriminated unions. The problem with these two approaches is that they lose type safety. You can access elements using the wrong type and causes disastrous results at run time with no errors from the compiler.

boost::any solves this problem. Internally boost::any uses a templates to create a wrapper class for the type you pass to it, and then create an instance of this class on the heap. It then manages retrieving the object inside using boost::any_cast safely.

Examples:

#include <boost/any.hpp>
#include <iostream>
#include <string>
#include <vector>

class A
{
int value_;
public:
A(int value) : value_(value) {}
void print() const {std::cout << "A's print: " << value_ << std::endl; }
};

class B
{
public:
void print() const {std::cout << "B's print" << std::endl; }
};

class C
{
public:
void print() const {std::cout << "C's print" << std::endl; }
};

int main()
{
typedef std::vector<boost::any> MixedContainer_t;
MixedContainer_t mixedContainer;

mixedContainer.push_back(A(1));

try
{
A a = boost::any_cast<A> (mixedContainer.back());
a.print();
}
catch(boost::bad_any_cast& bac)
{
bac.what();
}

//you can also retrieve the object via a pointer using the
//the overloaded boost::any_cast
if(A* a = boost::any_cast<A> (&mixedContainer.back()))
{
std::cout << "I can retrieve A by pointer" << std::endl;
}

boost::any a1 = mixedContainer.back();
if(typeid(A*) == a1.type())
std::cout << "last element is of A's type " << std::endl;


mixedContainer.push_back(B());
mixedContainer.push_back(C());

//push a string
std::string str("Hello World!");
mixedContainer.push_back(str);

void print_any(boost::any& element); //declaration
std::for_each(mixedContainer.begin(),
mixedContainer.end(),
print_any);
return 0;
}

void print_any(boost::any& element)
{
try
{
A a = boost::any_cast<A> (element);
a.print();
}
catch(boost::bad_any_cast& ex)
{
std::cout << "bad cast exception" << ex.what();
}
try
{
B b = boost::any_cast<B> (element);
b.print();
}
catch(boost::bad_any_cast& ex)
{
std::cout << "bad cast exception" << ex.what();
}
try
{
C c = boost::any_cast<C> (element);
c.print();
}
catch(boost::bad_any_cast& ex)
{
std::cout << "bad cast exception" << ex.what();
}
}




As you can see in the previous example, we can store any type in boost::any. Boost::any preserves the type and will not let you tamper with it without knowing the correct type. You can store pointers too, but retrieving pointers is a bit tricky and I'll discuss it in a later post. For now, we are interested in the two mechanism for retrieving the values stores in boost::any:

template<typename ValueType>
ValueType any_cast(const any & operand);

The argument is the boost::any we want to retrieve, and the ValueType is the type of the stored value. If the type does not correspond to what is stored in boost::any, then this version of any_cast will throw a bad_any_cast exception.

template<typename ValueType>
ValueType * any_cast(any * operand)


This overloaded any_cast takes a pointer to any, and returns a pointer to the stored value. If the type in the any isn 't Value_Type, a NULL is returned. There is also a version of this any_cast for const pointers.

Note the difference between the first mechanism where an exception is thrown, and the second where a null pointer is returned. Also note that we can use any of the overloaded any_cast exception to retrieve the value by either passing our argument by reference (to use the first mechanism) or value (to use the second) as we show in the preceding example:

    mixedContainer.push_back(A(1));

try
{
A a = boost::any_cast<A> (mixedContainer.back());
a.print();
}
catch(...)
{}

if(A* a = boost::any_cast<A> (&mixedContainer.back()))
{
std::cout << "I can retrieve A by pointer" << std::endl;
}



References:
- Introduction to Boost by Bjorn Karlsson
- the boost website

No comments :