X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fsandbox%2Flinux%2Fbpf_dsl%2Fcons.h;h=fa47c140ff503943f372b14a698c100b49414f6a;hb=1afa4dd80ef85af7c90efaea6959db1d92330844;hp=eb9e3aa990def77ea9074acfb48b0d0dd587ae0a;hpb=90762837333c13ccf56f2ad88e4481fc71e8d281;p=platform%2Fframework%2Fweb%2Fcrosswalk.git diff --git a/src/sandbox/linux/bpf_dsl/cons.h b/src/sandbox/linux/bpf_dsl/cons.h index eb9e3aa..fa47c14 100644 --- a/src/sandbox/linux/bpf_dsl/cons.h +++ b/src/sandbox/linux/bpf_dsl/cons.h @@ -9,38 +9,130 @@ #include "sandbox/sandbox_export.h" namespace sandbox { +namespace cons { -// Cons provides an immutable linked list abstraction as commonly -// provided in functional programming languages like Lisp or Haskell. +// Namespace cons provides an abstraction for immutable "cons list" +// data structures as commonly provided in functional programming +// languages like Lisp or Haskell. +// +// A cons list is a linked list consisting of "cells", each of which +// have a "head" and a "tail" element. A cell's head element contains +// a user specified value, while the tail element contains a (possibly +// null) pointer to another cell. +// +// An empty list (idiomatically referred to as "nil") can be +// constructed as "cons::List()" or simply as "nullptr" if Foo +// can be inferred from context (e.g., calling a function that has a +// "cons::List" parameter). +// +// Existing lists (including empty lists) can be extended by +// prepending new values to the front using the "Cons(head, tail)" +// function, which will allocate a new cons cell. Notably, cons lists +// support creating multiple lists that share a common tail sequence. +// +// Lastly, lists support iteration via C++11's range-based for loop +// construct. +// +// Examples: +// +// // basic construction +// const cons::List kNil = nullptr; +// cons::List ba = Cons('b', Cons('a', kNil)); +// +// // common tail sequence +// cons::List cba = Cons('c', ba); +// cons::List dba = Cons('d', ba); +// +// // iteration +// for (const char& ch : cba) { +// // iterates 'c', 'b', 'a' +// } +// for (const char& ch : dba) { +// // iterates 'd', 'b', 'a' +// } + +// Forward declarations. +template +class Cell; +template +class ListIterator; + +// List represents a (possibly null) pointer to a cons cell. +template +using List = scoped_refptr>; + +// Cons extends a cons list by prepending a new value to the front. +template +List Cons(const T& head, const List& tail) { + return List(new const Cell(head, tail)); +} + +// Cell represents an individual "cons cell" within a cons list. template -class Cons : public base::RefCounted > { +class Cell : public base::RefCounted> { public: - // List provides an abstraction for referencing a list of zero or - // more Cons nodes. - typedef scoped_refptr > List; + Cell(const T& head, const List& tail) : head_(head), tail_(tail) {} - // Return this node's head element. + // Head returns this cell's head element. const T& head() const { return head_; } - // Return this node's tail element. - List tail() const { return tail_; } - - // Construct a new List using |head| and |tail|. - static List Make(const T& head, List tail) { - return make_scoped_refptr(new const Cons(head, tail)); - } + // Tail returns this cell's tail element. + const List& tail() const { return tail_; } private: - Cons(const T& head, List tail) : head_(head), tail_(tail) {} - virtual ~Cons() {} + virtual ~Cell() {} T head_; - List tail_; + List tail_; - friend class base::RefCounted >; - DISALLOW_COPY_AND_ASSIGN(Cons); + friend class base::RefCounted>; + DISALLOW_COPY_AND_ASSIGN(Cell); }; +// Begin returns a list iterator pointing to the first element of the +// cons list. It's provided to support range-based for loops. +template +ListIterator begin(const List& list) { + return ListIterator(list); +} + +// End returns a list iterator pointing to the "past-the-end" element +// of the cons list (i.e., nil). It's provided to support range-based +// for loops. +template +ListIterator end(const List& list) { + return ListIterator(); +} + +// ListIterator provides C++ forward iterator semantics for traversing +// a cons list. +template +class ListIterator { + public: + ListIterator() : list_() {} + explicit ListIterator(const List& list) : list_(list) {} + + const T& operator*() const { return list_->head(); } + + ListIterator& operator++() { + list_ = list_->tail(); + return *this; + } + + friend bool operator==(const ListIterator& lhs, const ListIterator& rhs) { + return lhs.list_ == rhs.list_; + } + + private: + List list_; +}; + +template +bool operator!=(const ListIterator& lhs, const ListIterator& rhs) { + return !(lhs == rhs); +} + +} // namespace cons } // namespace sandbox #endif // SANDBOX_LINUX_BPF_DSL_CONS_H_