28 #ifndef GRAPHLAB_ANY_INCLUDED
29 #define GRAPHLAB_ANY_INCLUDED
44 #include <boost/type_traits/remove_reference.hpp>
45 #include <boost/type_traits/is_reference.hpp>
46 #include <boost/throw_exception.hpp>
47 #include <boost/static_assert.hpp>
48 #include <boost/utility.hpp>
49 #include <boost/exception/detail/is_output_streamable.hpp>
50 #include <boost/functional/hash.hpp>
53 #include <graphlab/logger/assertions.hpp>
54 #include <graphlab/serialization/serialization_includes.hpp>
146 virtual ~iholder() { }
147 virtual const std::type_info&
type()
const = 0;
148 virtual iholder * clone()
const = 0;
149 virtual uint64_t deserializer_id()
const = 0;
150 virtual void deep_op_equal(
const iholder* c) = 0;
153 virtual std::ostream& print(std::ostream& out)
const = 0;
159 any() : contents(NULL) { }
162 template<
typename ValueType>
163 explicit any(
const ValueType& value)
164 : contents(new
holder<ValueType>(value)) { }
168 contents(other.
empty() ? NULL : other.contents->clone()) { }
174 bool empty()
const {
return contents == NULL; }
178 template<
typename ValueType>
180 DASSERT_TRUE(
type() ==
typeid(ValueType));
181 DASSERT_FALSE(
empty());
187 template<
typename ValueType>
188 inline const ValueType&
as()
const{
189 DASSERT_TRUE(
type() ==
typeid(ValueType));
190 DASSERT_FALSE(
empty());
204 template<
typename ValueType>
206 if (contents != NULL && contents->type() ==
typeid(ValueType)) {
207 as<ValueType>() = rhs;
208 }
else {
any(rhs).swap(*
this); }
218 if (contents)
delete contents;
221 if (contents != NULL && contents->type() == rhs.contents->type()) {
222 contents->deep_op_equal(rhs.contents);
223 }
else {
any(rhs).swap(*
this); }
228 std::ostream& print(std::ostream& out)
const {
229 return empty()? (out <<
"EMPTY") : contents->print(out);
233 const std::type_info&
type()
const {
234 return empty() ?
typeid(void) : contents->type();
239 return empty() ?
"NULL" : std::string(contents->type().name());
245 if(contents != NULL) {
delete contents; contents = NULL; }
254 bool isempty =
empty();
256 if (isempty ==
false) contents->save(osoftarc);
269 typedef std::map<uint64_t, deserialize_function_type> registry_map_type;
282 template <
typename ValueType>
static
283 typename boost::disable_if_c<boost::is_output_streamable<ValueType>::value,
285 print_type_or_contents(std::ostream& out,
const ValueType &h) {
286 out <<
"Not_Printable[" <<
typeid(ValueType).name() <<
']';
289 template <
typename ValueType>
static
290 typename boost::enable_if_c<boost::is_output_streamable<ValueType>::value,
292 print_type_or_contents(std::ostream& out,
const ValueType &h) { out << h; }
300 template<
typename ValueType>
303 typedef ValueType value_type;
311 const std::type_info&
type()
const {
return typeid(ValueType); }
330 std::ostream&
print(std::ostream& out)
const {
331 any::print_type_or_contents(out,
contents);
351 boost::hash<std::string> hash_function;
353 localid = hash_function(
typeid(ValueType).name());
374 template<
typename ValueType>
375 typename any::holder<ValueType>::registry_type any::holder<ValueType>::registry;
379 std::ostream& operator<<(std::ostream& out,
const graphlab::any& any);