+ -
当前位置:首页 → 问答吧 → 恳求设计中C++继承、派生运用的较好的系统代码,学习学习

恳求设计中C++继承、派生运用的较好的系统代码,学习学习

时间:2010-08-31

来源:互联网

刚开始学C++,想深入学习这些东西,以前看过一下征途服务器端代码,觉得他们的那个框架和消息系统设计非常好,可惜当时没仔细分析。

恳求开源代码中框架写的较好的代码来学习,书上讲的太空洞了,求实际系统代码。

恳请不吝分享。

谢谢你的时间。

作者: thymye   发布时间: 2010-08-31

征途服务器端代码???
楼主能否共享一份?? 我有天龙八部服务端代码(第一版) 求共享

作者: linoom   发布时间: 2010-08-31

本帖最后由 zhujiang73 于 2010-08-31 18:02 编辑

回复 thymye


    还用的着那么兴师动众,看懂这个就算 C++ 过关。

http://www.boost.org/doc/libs/1_42_0/boost/any.hpp

http://www.boost.org/doc/libs/1_44_0/doc/html/any.html
  1. // See http://www.boost.org/libs/any for Documentation.

  2. #ifndef BOOST_ANY_INCLUDED
  3. #define BOOST_ANY_INCLUDED

  4. // what:  variant type boost::any
  5. // who:   contributed by Kevlin Henney,
  6. //        with features contributed and bugs found by
  7. //        Ed Brey, Mark Rodgers, Peter Dimov, and James Curran
  8. // when:  July 2001
  9. // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95

  10. #include <algorithm>
  11. #include <typeinfo>

  12. #include "boost/config.hpp"
  13. #include <boost/type_traits/remove_reference.hpp>
  14. #include <boost/type_traits/is_reference.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/static_assert.hpp>

  17. // See boost/python/type_id.hpp
  18. // TODO: add BOOST_TYPEID_COMPARE_BY_NAME to config.hpp
  19. # if (defined(__GNUC__) && __GNUC__ >= 3) \
  20. || defined(_AIX) \
  21. || (   defined(__sgi) && defined(__host_mips)) \
  22. || (defined(__hpux) && defined(__HP_aCC)) \
  23. || (defined(linux) && defined(__INTEL_COMPILER) && defined(__ICC))
  24. #  define BOOST_AUX_ANY_TYPE_ID_NAME
  25. #include <cstring>
  26. # endif

  27. namespace boost
  28. {
  29.     class any
  30.     {
  31.     public: // structors

  32.         any()
  33.           : content(0)
  34.         {
  35.         }

  36.         template<typename ValueType>
  37.         any(const ValueType & value)
  38.           : content(new holder<ValueType>(value))
  39.         {
  40.         }

  41.         any(const any & other)
  42.           : content(other.content ? other.content->clone() : 0)
  43.         {
  44.         }

  45.         ~any()
  46.         {
  47.             delete content;
  48.         }

  49.     public: // modifiers

  50.         any & swap(any & rhs)
  51.         {
  52.             std::swap(content, rhs.content);
  53.             return *this;
  54.         }

  55.         template<typename ValueType>
  56.         any & operator=(const ValueType & rhs)
  57.         {
  58.             any(rhs).swap(*this);
  59.             return *this;
  60.         }

  61.         any & operator=(any rhs)
  62.         {
  63.             rhs.swap(*this);
  64.             return *this;
  65.         }

  66.     public: // queries

  67.         bool empty() const
  68.         {
  69.             return !content;
  70.         }

  71.         const std::type_info & type() const
  72.         {
  73.             return content ? content->type() : typeid(void);
  74.         }

  75. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  76.     private: // types
  77. #else
  78.     public: // types (public so any_cast can be non-friend)
  79. #endif

  80.         class placeholder
  81.         {
  82.         public: // structors

  83.             virtual ~placeholder()
  84.             {
  85.             }

  86.         public: // queries

  87.             virtual const std::type_info & type() const = 0;

  88.             virtual placeholder * clone() const = 0;

  89.         };

  90.         template<typename ValueType>
  91.         class holder : public placeholder
  92.         {
  93.         public: // structors

  94.             holder(const ValueType & value)
  95.               : held(value)
  96.             {
  97.             }

  98.         public: // queries

  99.             virtual const std::type_info & type() const
  100.             {
  101.                 return typeid(ValueType);
  102.             }

  103.             virtual placeholder * clone() const
  104.             {
  105.                 return new holder(held);
  106.             }

  107.         public: // representation

  108.             ValueType held;

  109.         private: // intentionally left unimplemented
  110.             holder & operator=(const holder &);
  111.         };

  112. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS

  113.     private: // representation

  114.         template<typename ValueType>
  115.         friend ValueType * any_cast(any *);

  116.         template<typename ValueType>
  117.         friend ValueType * unsafe_any_cast(any *);

  118. #else

  119.     public: // representation (public so any_cast can be non-friend)

  120. #endif

  121.         placeholder * content;

  122.     };

  123.     class bad_any_cast : public std::bad_cast
  124.     {
  125.     public:
  126.         virtual const char * what() const throw()
  127.         {
  128.             return "boost::bad_any_cast: "
  129.                    "failed conversion using boost::any_cast";
  130.         }
  131.     };

  132.     template<typename ValueType>
  133.     ValueType * any_cast(any * operand)
  134.     {
  135.         return operand &&
  136. #ifdef BOOST_AUX_ANY_TYPE_ID_NAME
  137.             std::strcmp(operand->type().name(), typeid(ValueType).name()) == 0
  138. #else
  139.             operand->type() == typeid(ValueType)
  140. #endif
  141.             ? &static_cast<any::holder<ValueType> *>(operand->content)->held
  142.             : 0;
  143.     }

  144.     template<typename ValueType>
  145.     inline const ValueType * any_cast(const any * operand)
  146.     {
  147.         return any_cast<ValueType>(const_cast<any *>(operand));
  148.     }

  149.     template<typename ValueType>
  150.     ValueType any_cast(any & operand)
  151.     {
  152.         typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;

  153. #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  154.         // If 'nonref' is still reference type, it means the user has not
  155.         // specialized 'remove_reference'.

  156.         // Please use BOOST_BROKEN_COMPILER_TYPE_TRAITS_SPECIALIZATION macro
  157.         // to generate specialization of remove_reference for your class
  158.         // See type traits library documentation for details
  159.         BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
  160. #endif

  161.         nonref * result = any_cast<nonref>(&operand);
  162.         if(!result)
  163.             boost::throw_exception(bad_any_cast());
  164.         return *result;
  165.     }

  166.     template<typename ValueType>
  167.     inline ValueType any_cast(const any & operand)
  168.     {
  169.         typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;

  170. #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  171.         // The comment in the above version of 'any_cast' explains when this
  172.         // assert is fired and what to do.
  173.         BOOST_STATIC_ASSERT(!is_reference<nonref>::value);
  174. #endif

  175.         return any_cast<const nonref &>(const_cast<any &>(operand));
  176.     }

  177.     // Note: The "unsafe" versions of any_cast are not part of the
  178.     // public interface and may be removed at any time. They are
  179.     // required where we know what type is stored in the any and can't
  180.     // use typeid() comparison, e.g., when our types may travel across
  181.     // different shared libraries.
  182.     template<typename ValueType>
  183.     inline ValueType * unsafe_any_cast(any * operand)
  184.     {
  185.         return &static_cast<any::holder<ValueType> *>(operand->content)->held;
  186.     }

  187.     template<typename ValueType>
  188.     inline const ValueType * unsafe_any_cast(const any * operand)
  189.     {
  190.         return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
  191.     }
  192. }

  193. // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
  194. //
  195. // Distributed under the Boost Software License, Version 1.0. (See
  196. // accompanying file LICENSE_1_0.txt or copy at
  197. // http://www.boost.org/LICENSE_1_0.txt)

  198. #endif
复制代码

作者: zhujiang73   发布时间: 2010-08-31

相关阅读 更多

热门下载

更多