免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
打印 上一主题 下一主题

[C++] 。。。。。。 [复制链接]

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
61 [报告]
发表于 2013-01-28 17:07 |只看该作者
算知道c++,几年了

论坛徽章:
0
62 [报告]
发表于 2013-01-31 12:51 |只看该作者
下面的代码引用自chrome

weak_ptr.h
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.   
  5. // Weak pointers help in cases where you have many objects referring back to a
  6. // shared object and you wish for the lifetime of the shared object to not be
  7. // bound to the lifetime of the referrers.  In other words, this is useful when
  8. // reference counting is not a good fit.
  9. //
  10. // Thread-safety notes:
  11. // When you get a WeakPtr (from a WeakPtrFactory or SupportsWeakPtr),
  12. // the WeakPtr becomes bound to the current thread. You may only
  13. // dereference the WeakPtr on that thread. However, it is safe to
  14. // destroy the WeakPtr object on another thread.
  15. // Since a WeakPtr object may be destroyed on a background thread,
  16. // querying WeakPtrFactory's HasWeakPtrs() method can be racy.
  17. //
  18. //
  19. // A common alternative to weak pointers is to have the shared object hold a
  20. // list of all referrers, and then when the shared object is destroyed, it
  21. // calls a method on the referrers to tell them to drop their references.  This
  22. // approach also requires the referrers to tell the shared object when they get
  23. // destroyed so that the shared object can remove the referrer from its list of
  24. // referrers.  Such a solution works, but it is a bit complex.
  25. //
  26. // EXAMPLE:
  27. //
  28. //  class Controller : public SupportsWeakPtr<Controller> {
  29. //   public:
  30. //    void SpawnWorker() { Worker::StartNew(AsWeakPtr()); }
  31. //    void WorkComplete(const Result& result) { ... }
  32. //  };
  33. //
  34. //  class Worker {
  35. //   public:
  36. //    static void StartNew(const WeakPtr<Controller>& controller) {
  37. //      Worker* worker = new Worker(controller);
  38. //      // Kick off asynchronous processing...
  39. //    }
  40. //   private:
  41. //    Worker(const WeakPtr<Controller>& controller)
  42. //        : controller_(controller) {}
  43. //    void DidCompleteAsynchronousProcessing(const Result& result) {
  44. //      if (controller_)
  45. //        controller_->WorkComplete(result);
  46. //    }
  47. //    WeakPtr<Controller> controller_;
  48. //  };
  49. //
  50. // Given the above classes, a consumer may allocate a Controller object, call
  51. // SpawnWorker several times, and then destroy the Controller object before all
  52. // of the workers have completed.  Because the Worker class only holds a weak
  53. // pointer to the Controller, we don't have to worry about the Worker
  54. // dereferencing the Controller back pointer after the Controller has been
  55. // destroyed.
  56. //
  57.   
  58. #ifndef BASE_MEMORY_WEAK_PTR_H_
  59. #define BASE_MEMORY_WEAK_PTR_H_
  60. #pragma once
  61.   
  62. #include "base/base_export.h"
  63. #include "base/logging.h"
  64. #include "base/memory/ref_counted.h"
  65.   
  66. namespace base {
  67.   
  68. namespace internal {
  69. // These classes are part of the WeakPtr implementation.
  70. // DO NOT USE THESE CLASSES DIRECTLY YOURSELF.
  71.   
  72. class BASE_EXPORT WeakReference {
  73.   public:
  74.    // While Flag is bound to a specific thread, it may be deleted from another
  75.    // via base::WeakPtr::~WeakPtr().
  76.    class Flag : public RefCountedThreadSafe<Flag> {
  77.     public:
  78.      Flag();
  79.   
  80.      void Invalidate();
  81.      bool IsValid() const;
  82.   
  83.      void DetachFromThread() { }
  84.   
  85.     private:
  86.      friend class base::RefCountedThreadSafe<Flag>;
  87.   
  88.      ~Flag();
  89.   
  90.      bool is_valid_;
  91.    };
  92.   
  93.    WeakReference();
  94.    explicit WeakReference(const Flag* flag);
  95.    ~WeakReference();
  96.   
  97.    bool is_valid() const;
  98.   
  99.   private:
  100.    scoped_refptr<const Flag> flag_;
  101. };
  102.   
  103. class BASE_EXPORT WeakReferenceOwner {
  104.   public:
  105.    WeakReferenceOwner();
  106.    ~WeakReferenceOwner();
  107.   
  108.    WeakReference GetRef() const;
  109.   
  110.    bool HasRefs() const {
  111.      return flag_.get() && !flag_->HasOneRef();
  112.    }
  113.   
  114.    void Invalidate();
  115.   
  116.    // Indicates that this object will be used on another thread from now on.
  117.    void DetachFromThread() {
  118.      if (flag_) flag_->DetachFromThread();
  119.    }
  120.   
  121.   private:
  122.    mutable scoped_refptr<WeakReference::Flag> flag_;
  123. };
  124.   
  125. // This class simplifies the implementation of WeakPtr's type conversion
  126. // constructor by avoiding the need for a public accessor for ref_.  A
  127. // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this
  128. // base class gives us a way to access ref_ in a protected fashion.
  129. class BASE_EXPORT WeakPtrBase {
  130.   public:
  131.    WeakPtrBase();
  132.    ~WeakPtrBase();
  133.   
  134.   protected:
  135.    WeakPtrBase(const WeakReference& ref);
  136.   
  137.    WeakReference ref_;
  138. };
  139.   
  140. }  // namespace internal
  141.   
  142. template <typename T> class SupportsWeakPtr;
  143. template <typename T> class WeakPtrFactory;
  144.   
  145. // The WeakPtr class holds a weak reference to |T*|.
  146. //
  147. // This class is designed to be used like a normal pointer.  You should always
  148. // null-test an object of this class before using it or invoking a method that
  149. // may result in the underlying object being destroyed.
  150. //
  151. // EXAMPLE:
  152. //
  153. //   class Foo { ... };
  154. //   WeakPtr<Foo> foo;
  155. //   if (foo)
  156. //     foo->method();
  157. //
  158. template <typename T>
  159. class WeakPtr : public internal::WeakPtrBase {
  160.   public:
  161.    WeakPtr() : ptr_(NULL) {
  162.    }
  163.   
  164.    // Allow conversion from U to T provided U "is a" T.
  165.    template <typename U>
  166.    WeakPtr(const WeakPtr<U>& other) : WeakPtrBase(other), ptr_(other.get()) {
  167.    }
  168.   
  169.    T* get() const { return ref_.is_valid() ? ptr_ : NULL; }
  170.    operator T*() const { return get(); }
  171.   
  172.    T* operator*() const {
  173.      DCHECK(get() != NULL);
  174.      return *get();
  175.    }
  176.    T* operator->() const {
  177.      DCHECK(get() != NULL);
  178.      return get();
  179.    }
  180.   
  181.    void reset() {
  182.      ref_ = internal::WeakReference();
  183.      ptr_ = NULL;
  184.    }
  185.   
  186.   private:
  187.    friend class SupportsWeakPtr<T>;
  188.    friend class WeakPtrFactory<T>;
  189.   
  190.    WeakPtr(const internal::WeakReference& ref, T* ptr)
  191.        : WeakPtrBase(ref), ptr_(ptr) {
  192.    }
  193.   
  194.    // This pointer is only valid when ref_.is_valid() is true.  Otherwise, its
  195.    // value is undefined (as opposed to NULL).
  196.    T* ptr_;
  197. };
  198.   
  199. // A class may extend from SupportsWeakPtr to expose weak pointers to itself.
  200. // This is useful in cases where you want others to be able to get a weak
  201. // pointer to your class.  It also has the property that you don't need to
  202. // initialize it from your constructor.
  203. template <class T>
  204. class SupportsWeakPtr {
  205.   public:
  206.    SupportsWeakPtr() {}
  207.   
  208.    WeakPtr<T> AsWeakPtr() {
  209.      return WeakPtr<T>(weak_reference_owner_.GetRef(), static_cast<T*>(this));
  210.    }
  211.   
  212.    // Indicates that this object will be used on another thread from now on.
  213.    void DetachFromThread() {
  214.      weak_reference_owner_.DetachFromThread();
  215.    }
  216.   
  217.   private:
  218.    internal::WeakReferenceOwner weak_reference_owner_;
  219.    DISALLOW_COPY_AND_ASSIGN(SupportsWeakPtr);
  220. };
  221.   
  222. // A class may alternatively be composed of a WeakPtrFactory and thereby
  223. // control how it exposes weak pointers to itself.  This is helpful if you only
  224. // need weak pointers within the implementation of a class.  This class is also
  225. // useful when working with primitive types.  For example, you could have a
  226. // WeakPtrFactory<bool> that is used to pass around a weak reference to a bool.
  227. template <class T>
  228. class WeakPtrFactory {
  229.   public:
  230.    explicit WeakPtrFactory(T* ptr) : ptr_(ptr) {
  231.    }
  232.   
  233.    WeakPtr<T> GetWeakPtr() {
  234.      return WeakPtr<T>(weak_reference_owner_.GetRef(), ptr_);
  235.    }
  236.   
  237.    // Call this method to invalidate all existing weak pointers.
  238.    void InvalidateWeakPtrs() {
  239.      weak_reference_owner_.Invalidate();
  240.    }
  241.   
  242.    // Call this method to determine if any weak pointers exist.
  243.    bool HasWeakPtrs() const {
  244.      return weak_reference_owner_.HasRefs();
  245.    }
  246.   
  247.    // Indicates that this object will be used on another thread from now on.
  248.    void DetachFromThread() {
  249.      weak_reference_owner_.DetachFromThread();
  250.    }
  251.   
  252.   private:
  253.    internal::WeakReferenceOwner weak_reference_owner_;
  254.    T* ptr_;
  255.    DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
  256. };
  257.   
  258. }  // namespace base
  259.   
  260. #endif  // BASE_MEMORY_WEAK_PTR_H_
复制代码
weak_ptr.cc
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.   
  5. #include "base/memory/weak_ptr.h"
  6.   
  7. namespace base {
  8. namespace internal {
  9.   
  10. WeakReference::Flag::Flag() : is_valid_(true) {
  11. }
  12.   
  13. void WeakReference::Flag::Invalidate() {
  14.    // The flag being invalidated with a single ref implies that there are no
  15.    // weak pointers in existence. Allow deletion on other thread in this case.
  16.    DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef());
  17.    is_valid_ = false;
  18. }
  19.   
  20. bool WeakReference::Flag::IsValid() const {
  21.    DCHECK(thread_checker_.CalledOnValidThread());
  22.    return is_valid_;
  23. }
  24.   
  25. WeakReference::Flag::~Flag() {
  26. }
  27.   
  28. WeakReference::WeakReference() {
  29. }
  30.   
  31. WeakReference::WeakReference(const Flag* flag) : flag_(flag) {
  32. }
  33.   
  34. WeakReference::~WeakReference() {
  35. }
  36.   
  37. bool WeakReference::is_valid() const {
  38.    return flag_ && flag_->IsValid();
  39. }
  40.   
  41. WeakReferenceOwner::WeakReferenceOwner() {
  42. }
  43.   
  44. WeakReferenceOwner::~WeakReferenceOwner() {
  45.    Invalidate();
  46. }
  47.   
  48. WeakReference WeakReferenceOwner::GetRef() const {
  49.    // We also want to reattach to the current thread if all previous references
  50.    // have gone away.
  51.    if (!HasRefs())
  52.      flag_ = new WeakReference::Flag();
  53.    return WeakReference(flag_);
  54. }
  55.   
  56. void WeakReferenceOwner::Invalidate() {
  57.    if (flag_) {
  58.      flag_->Invalidate();
  59.      flag_ = NULL;
  60.    }
  61. }
  62.   
  63. WeakPtrBase::WeakPtrBase() {
  64. }
  65.   
  66. WeakPtrBase::~WeakPtrBase() {
  67. }
  68.   
  69. WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {
  70. }
  71.   
  72. }  // namespace internal
  73. }  // namespace base
  74.    
复制代码
boost里的没用过,不过我想原理应该差不多。其实核心不就是那个flag么?因为这玩意是引用计数,并且是线程安全的.

论坛徽章:
0
63 [报告]
发表于 2013-01-31 13:06 |只看该作者
starwing83 发表于 2013-01-20 09:13
回复 51# 耗资喜欢猫

在大些的工程中,基本不用原生指针,基本都是引用计数的,当然,性能是比原生指针差了些,但bug也比直接用原生指针少,即便出了问题,追bug也比较容易。

论坛徽章:
0
64 [报告]
发表于 2013-02-06 10:51 |只看该作者
我有一种想法,C++学得越多反而越糟糕,因为接近走火入魔了。类似张无忌学乾坤大挪移,幸亏没学最后一层。

学50%就够了。

论坛徽章:
0
65 [报告]
发表于 2014-07-17 11:14 |只看该作者
我用c++实现了jdk1.6的80%以上,跨osx、linux、win,没有stl和boost,因为也没心思去玩这两玩意,
这种情况不知道算不算掌握了c++。

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
66 [报告]
发表于 2014-07-18 15:08 |只看该作者
给88%了。很少使用。

论坛徽章:
46
2015小元宵徽章
日期:2015-03-06 15:58:18羊年新春福章
日期:2015-04-14 10:37:422015年亚洲杯之阿曼
日期:2015-04-14 10:41:50NBA常规赛纪念章
日期:2015-05-04 22:32:03NBA季后赛大富翁
日期:2015-05-04 22:34:11菠菜明灯
日期:2015-05-04 22:35:49新奥尔良黄蜂
日期:2015-05-04 22:49:2315-16赛季CBA联赛之广夏
日期:2015-12-11 15:02:342015年亚洲杯之巴勒斯坦
日期:2015-03-04 19:56:562015年亚洲杯之阿联酋
日期:2015-03-04 11:19:04休斯顿火箭
日期:2015-03-02 16:32:11纽约尼克斯
日期:2015-03-02 16:09:04
67 [报告]
发表于 2014-07-22 11:05 |只看该作者
将将能看看代码的水平

论坛徽章:
0
68 [报告]
发表于 2014-07-29 14:17 |只看该作者
太片面了,C++最重要的是封装思想,不是具体的某个细节,如果说C++就是那几个小问题的话,只能说太片面了!

论坛徽章:
8
2015年辞旧岁徽章
日期:2015-03-03 16:54:15午马
日期:2015-02-04 12:00:07羊年新春福章
日期:2015-02-04 11:57:56双子座
日期:2014-12-02 11:44:59金牛座
日期:2014-10-08 16:47:08狮子座
日期:2014-08-29 13:37:46巳蛇
日期:2014-08-26 17:32:29NBA常规赛纪念章
日期:2015-05-04 22:32:03
69 [报告]
发表于 2014-10-10 16:52 |只看该作者
果然处于大队伍的行列   

论坛徽章:
0
70 [报告]
发表于 2014-11-14 22:44 |只看该作者
回复 9# sonicling


    给大神跪了,求带
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP