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