tahoma2d/toonz/sources/include/tcg/base.h

118 lines
3.5 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TCG_BASE_H
#define TCG_BASE_H
2016-06-15 18:43:10 +12:00
namespace tcg {
2016-03-19 06:57:51 +13:00
/*!
\file tcg_base.h
\brief This file contains the implementation of some handy base classes
that can be inherited from.
*/
//========================================================================
/*!
\brief The empty_type can be used as default template parameter in cases
where the template parameter may be omitted.
*/
2016-06-15 18:43:10 +12:00
struct empty_type {};
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------
/*!
\brief The noncopyable class can be inherited to forbid access to the
copy constructor and assignment operator.
\note A template parameter is provided to permit the empty base
class optimization. In case this optimization is not needed,
please use boost::noncopyable instead.
*/
template <typename B = empty_type>
struct noncopyable : public B {
2016-06-15 18:43:10 +12:00
noncopyable() {}
// noncopyable(const B& b) : B(b) {} // Would introduce
// additional copies
// along the inheritance chain. Not worth it.
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
~noncopyable() {} //!< Protected destructor since the class
//! is intended for nonvirtual inheritance.
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
noncopyable(const noncopyable &); //!< Non-accessible copy constructor.
noncopyable &operator=(
const noncopyable &); //!< Non-accessible assignment operator.
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
/*!
\brief The polymorphic class just implements an empty class
with a virtual destructor.
\details It can be useful in certain occasions:
\li Explicitly marks derived classes as polymporphic, and
spares the need to write a virtual destructor.
\li It's noncopyable, disabling value semantics.
\li Provides a common base class to polymorphic hierarchies.
\li Enables lightweight type erasure without resorting to
a wrapper class like boost::any, assuming you have
enough control of the involved class to add a base class.
*/
2016-06-15 18:43:10 +12:00
class polymorphic : noncopyable<> // Noncopyable to prevent slicing
2016-03-19 06:57:51 +13:00
{
protected:
2016-06-15 18:43:10 +12:00
polymorphic() {} //!< Protected constructor to ensure that the
//! class is only used as base class.
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
virtual ~polymorphic() {} //!< A virtual destructor as every good base
//! class must have.
2016-03-19 06:57:51 +13:00
};
//------------------------------------------------------------------------
/*!
The tcg::safe_bool template class is a simple implementation
of the classic Safe Bool Idiom, meant to be used inside tcg.
For a better implementation, you should consider looking into
Boost.Spirit.Safe_Bool.
*/
2016-06-15 18:43:10 +12:00
template <typename T, typename B = empty_type> // B is used for base class
2016-06-20 14:23:05 +12:00
// chaining, to deal with
class safe_bool : public B // the empty class optimization
2016-03-19 06:57:51 +13:00
{
2016-06-15 18:43:10 +12:00
class dummy {};
struct detail {
dummy *member;
};
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
typedef dummy *detail::*bool_type;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
safe_bool() {}
// safe_bool(const B& b) : B(b) {} // Would introduce additional
// copies
// along the inheritance chain. Not worth it.
operator bool_type() const {
return static_cast<const T *>(this)->operator_bool() ? &detail::member : 0;
}
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
~safe_bool() {} //!< Protected destructor since the class
//! is intended for nonvirtual inheritance.
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
bool operator==(const safe_bool &);
bool operator!=(const safe_bool &);
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace tcg
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
#endif // TCG_BASE_H