| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SANDBOX_LINUX_BPF_DSL_CONS_H_ | 5 #ifndef SANDBOX_LINUX_BPF_DSL_CONS_H_ |
| 6 #define SANDBOX_LINUX_BPF_DSL_CONS_H_ | 6 #define SANDBOX_LINUX_BPF_DSL_CONS_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "sandbox/sandbox_export.h" | 11 #include "sandbox/sandbox_export.h" |
| 11 | 12 |
| 12 namespace sandbox { | 13 namespace sandbox { |
| 13 namespace cons { | 14 namespace cons { |
| 14 | 15 |
| 15 // Namespace cons provides an abstraction for immutable "cons list" | 16 // Namespace cons provides an abstraction for immutable "cons list" |
| 16 // data structures as commonly provided in functional programming | 17 // data structures as commonly provided in functional programming |
| 17 // languages like Lisp or Haskell. | 18 // languages like Lisp or Haskell. |
| 18 // | 19 // |
| 19 // A cons list is a linked list consisting of "cells", each of which | 20 // A cons list is a linked list consisting of "cells", each of which |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 // } | 54 // } |
| 54 | 55 |
| 55 // Forward declarations. | 56 // Forward declarations. |
| 56 template <typename T> | 57 template <typename T> |
| 57 class Cell; | 58 class Cell; |
| 58 template <typename T> | 59 template <typename T> |
| 59 class ListIterator; | 60 class ListIterator; |
| 60 | 61 |
| 61 // List represents a (possibly null) pointer to a cons cell. | 62 // List represents a (possibly null) pointer to a cons cell. |
| 62 template <typename T> | 63 template <typename T> |
| 63 using List = scoped_refptr<const Cell<T>>; | 64 using List = std::shared_ptr<const Cell<T>>; |
| 64 | 65 |
| 65 // Cons extends a cons list by prepending a new value to the front. | 66 // Cons extends a cons list by prepending a new value to the front. |
| 66 template <typename T> | 67 template <typename T> |
| 67 List<T> Cons(const T& head, const List<T>& tail) { | 68 List<T> Cons(const T& head, List<T> tail) { |
| 68 return List<T>(new const Cell<T>(head, tail)); | 69 return std::make_shared<Cell<T>>(head, std::move(tail)); |
| 69 } | 70 } |
| 70 | 71 |
| 71 // Cell represents an individual "cons cell" within a cons list. | 72 // Cell represents an individual "cons cell" within a cons list. |
| 72 template <typename T> | 73 template <typename T> |
| 73 class Cell : public base::RefCounted<Cell<T>> { | 74 class Cell { |
| 74 public: | 75 public: |
| 75 Cell(const T& head, const List<T>& tail) : head_(head), tail_(tail) {} | 76 Cell(const T& head, List<T> tail) : head_(head), tail_(std::move(tail)) {} |
| 76 | 77 |
| 77 // Head returns this cell's head element. | 78 // Head returns this cell's head element. |
| 78 const T& head() const { return head_; } | 79 const T& head() const { return head_; } |
| 79 | 80 |
| 80 // Tail returns this cell's tail element. | 81 // Tail returns this cell's tail element. |
| 81 const List<T>& tail() const { return tail_; } | 82 const List<T>& tail() const { return tail_; } |
| 82 | 83 |
| 83 private: | 84 private: |
| 84 virtual ~Cell() {} | |
| 85 | |
| 86 T head_; | 85 T head_; |
| 87 List<T> tail_; | 86 List<T> tail_; |
| 88 | 87 |
| 89 friend class base::RefCounted<Cell<T>>; | |
| 90 DISALLOW_COPY_AND_ASSIGN(Cell); | 88 DISALLOW_COPY_AND_ASSIGN(Cell); |
| 91 }; | 89 }; |
| 92 | 90 |
| 93 // Begin returns a list iterator pointing to the first element of the | 91 // Begin returns a list iterator pointing to the first element of the |
| 94 // cons list. It's provided to support range-based for loops. | 92 // cons list. It's provided to support range-based for loops. |
| 95 template <typename T> | 93 template <typename T> |
| 96 ListIterator<T> begin(const List<T>& list) { | 94 ListIterator<T> begin(const List<T>& list) { |
| 97 return ListIterator<T>(list); | 95 return ListIterator<T>(list); |
| 98 } | 96 } |
| 99 | 97 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 130 | 128 |
| 131 template <typename T> | 129 template <typename T> |
| 132 bool operator!=(const ListIterator<T>& lhs, const ListIterator<T>& rhs) { | 130 bool operator!=(const ListIterator<T>& lhs, const ListIterator<T>& rhs) { |
| 133 return !(lhs == rhs); | 131 return !(lhs == rhs); |
| 134 } | 132 } |
| 135 | 133 |
| 136 } // namespace cons | 134 } // namespace cons |
| 137 } // namespace sandbox | 135 } // namespace sandbox |
| 138 | 136 |
| 139 #endif // SANDBOX_LINUX_BPF_DSL_CONS_H_ | 137 #endif // SANDBOX_LINUX_BPF_DSL_CONS_H_ |
| OLD | NEW |