10 #ifndef TURI_ANY_INCLUDED 11 #define TURI_ANY_INCLUDED 26 #include <boost/type_traits/remove_reference.hpp> 27 #include <boost/type_traits/is_reference.hpp> 28 #include <boost/throw_exception.hpp> 29 #include <boost/static_assert.hpp> 30 #include <boost/utility.hpp> 31 #include <boost/exception/detail/is_output_streamable.hpp> 32 #include <boost/functional/hash.hpp> 35 #include <core/logging/assertions.hpp> 36 #include <core/storage/serialization/serialization_includes.hpp> 129 virtual ~iholder() { }
130 virtual const std::type_info&
type()
const = 0;
131 virtual iholder * clone()
const = 0;
132 virtual uint64_t deserializer_id()
const = 0;
133 virtual void deep_op_equal(
const iholder* c) = 0;
136 virtual std::ostream& print(std::ostream& out)
const = 0;
142 any() : contents(NULL) { }
145 template<
typename ValueType>
146 explicit any(
const ValueType& value)
147 : contents(new
holder<ValueType>(value)) { }
151 contents(other.
empty() ? NULL : other.contents->clone()) { }
157 bool empty()
const {
return contents == NULL; }
161 template<
typename ValueType>
170 template<
typename ValueType>
171 inline const ValueType&
as()
const{
178 template<
typename ValueType>
180 return (
type() ==
typeid(ValueType));
186 std::swap(contents, rhs.contents);
194 template<
typename ValueType>
196 if (contents != NULL && contents->type() ==
typeid(ValueType)) {
197 as<ValueType>() = rhs;
198 }
else {
any(rhs).swap(*
this); }
208 if (contents)
delete contents;
211 if (contents != NULL && contents->type() == rhs.contents->type()) {
212 contents->deep_op_equal(rhs.contents);
213 }
else {
any(rhs).swap(*
this); }
218 std::ostream& print(std::ostream& out)
const {
219 return empty()? (out <<
"EMPTY") : contents->print(out);
223 const std::type_info&
type()
const {
224 return empty() ?
typeid(void) : contents->type();
229 return empty() ?
"NULL" : std::string(contents->type().name());
235 if(contents != NULL) {
delete contents; contents = NULL; }
238 if (isempty ==
false) contents = iholder::load(isoftarc);
244 bool isempty =
empty();
246 if (isempty ==
false) contents->save(osoftarc);
259 typedef std::map<uint64_t, deserialize_function_type> registry_map_type;
272 template <
typename ValueType>
static 273 typename boost::disable_if_c<boost::is_output_streamable<ValueType>::value,
275 print_type_or_contents(std::ostream& out,
const ValueType &h) {
276 out <<
"Not_Printable[" <<
typeid(ValueType).name() <<
']';
279 template <
typename ValueType>
static 280 typename boost::enable_if_c<boost::is_output_streamable<ValueType>::value,
282 print_type_or_contents(std::ostream& out,
const ValueType &h) { out << h; }
290 template<
typename ValueType>
293 typedef ValueType value_type;
297 holder(
const ValueType& value) : contents(value) { }
301 const std::type_info&
type()
const {
return typeid(ValueType); }
314 arc << registry.localid << contents;
320 std::ostream&
print(std::ostream& out)
const {
321 any::print_type_or_contents(out, contents);
341 boost::hash<std::string> hash_function;
343 localid = hash_function(
typeid(ValueType).name());
364 template<
typename ValueType>
369 std::ostream& operator<<(std::ostream& out,
const turi::any&
any);
uint64_t deserializer_id() const
void load(iarchive &arc)
loads the any from a file.
any(const any &other)
Construct an any from another any.
holder(iarchive_soft_fail &arc)
Construct a holder from an archive.
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
~any()
Destroy the contentss of this any.
const ValueType & as() const
bool empty() const
Returns true if the object does not contain any stored data.
const std::string type_name() const
Return the name of the internal type as a string.
any & operator=(const any &rhs)
std::ostream & print(std::ostream &out) const
static iholder * deserialize(iarchive_soft_fail &arc)
const std::type_info & type() const
Returns the type information of the stored data.
void deep_op_equal(const iholder *other)
Deep assignment.
When this archive is used to serialize an object, and the object does not support serialization...
#define DASSERT_FALSE(cond)
any & swap(any &rhs)
Exchanges the contents of two any's.
iholder * clone() const
Clone a holder.
ValueType contents
The actual contents of the holder.
void save(oarchive &arc) const
Saves the any to a file. Caveats apply. See the main any docs.
holder(const ValueType &value)
Construct a holder from a value.
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
any()
default constructor. Creates an empty any
any & operator=(const ValueType &rhs)
static registry_type registry
bool is() const
Returns true if the contained type is of type ValueType.
const std::type_info & type() const
Get the type info of the holder.
static registry_map_type & get_global_registry()
#define DASSERT_TRUE(cond)
any(const ValueType &value)
Creates an any which stores the value.
When this archive is used to deserialize an object, and the object does not support serialization...