atlas
VectorOfAbstract.h
1 /*
2  * (C) Copyright 2013 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation
8  * nor does it submit to any jurisdiction.
9  */
10 
15 
16 #pragma once
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "atlas/library/config.h"
22 
23 namespace atlas {
24 namespace util {
25 
26 //------------------------------------------------------------------------------------------------------
27 
28 template <typename BaseIterator>
29 class DereferenceIterator : DOXYGEN_HIDE( public BaseIterator ) {
30 public:
31  using value_type = typename BaseIterator::value_type::element_type;
32  using pointer = value_type*;
33  using reference = value_type&;
34 
35  DereferenceIterator( const BaseIterator& other ) : BaseIterator( other ) {}
36 
37  reference operator*() const { return *( this->BaseIterator::operator*() ); }
38  pointer operator->() const { return this->BaseIterator::operator*().get(); }
39  reference operator[]( size_t n ) const { return *( this->BaseIterator::operator[]( n ) ); }
40 };
41 
42 //------------------------------------------------------------------------------------------------------
43 
44 template <typename Iterator>
45 DereferenceIterator<Iterator> make_dereference_iterator( Iterator t ) {
47 }
48 
49 //------------------------------------------------------------------------------------------------------
50 
51 template <typename Abstract>
53 public:
54  using value_type = Abstract;
55  using container_type = std::vector<std::unique_ptr<value_type> >;
56  using const_reference = const value_type&;
57  using reference = const_reference;
59 
60 public:
61  VectorOfAbstract() = default;
62  VectorOfAbstract( VectorOfAbstract&& other ) : container_( std::move( other.container_ ) ) {}
63 
64  const_iterator begin() const { return make_dereference_iterator( container_.begin() ); }
65  const_iterator end() const { return make_dereference_iterator( container_.end() ); }
66  const_reference operator[]( idx_t i ) const { return *container_[i]; }
67  const_reference at( idx_t i ) const { return *container_[i]; }
68  idx_t size() const { return container_.size(); }
69  void reserve( size_t size ) { container_.reserve( size ); }
70  template <typename... Args>
71  void emplace_back( Args&&... args ) {
72  container_.emplace_back( std::forward<Args>( args )... );
73  }
74  container_type& get() { return container_; }
75  void clear() { return container_.clear(); }
76 
77 private:
78  container_type container_;
79 };
80 
81 //------------------------------------------------------------------------------------------------------
82 
83 } // namespace util
84 } // namespace atlas
Definition: VectorOfAbstract.h:29
Definition: VectorOfAbstract.h:52
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42