gslcpp
Modern-C++ Wrapper for GSL
v-stor.hpp
Go to the documentation of this file.
1 /// \file include/gslcpp/vec/v-stor.hpp
2 /// \copyright 2022 Thomas E. Vaughan, all rights reserved.
3 /// \brief Definition for gsl::v_stor.
4 
5 #pragma once
6 #include "../wrap/free.hpp" // w_free
7 #include "../wrap/vector-alloc.hpp" // w_vector_alloc
8 #include "../wrap/vector-calloc.hpp" // w_vector_calloc
9 #include "../wrap/vector-view-array.hpp" // vector_view_array
10 
11 namespace gsl {
12 
13 
14 /// Generic %v_stor is interface to storage with two key properties:
15 /// (1) that size of storage is known statically, at compile-time, and
16 /// (2) that it is owned by instance of %v_stor.
17 ///
18 /// Specialization gsl::v_stor<T,0> is for storage-size determined at run-time.
19 ///
20 /// @tparam T Type of each element in vector.
21 /// @tparam S Compile-time size of vector (0 for size specified at run-time).
22 template<typename T, size_t S= 0> class v_stor {
23  static_assert(S > 0);
24 
25  T d_[S]; ///< Storage for data.
26  w_vector_view<T> cview_; ///< GSL's view of data.
27 
28  v_stor(v_stor const &)= delete; ///< Disable copy-construction.
29  v_stor &operator=(v_stor const &)= delete; ///< Disable copy-assignment.
30 
31 public:
32  /// Initialize GSL's view of static storage, but do not initialize elements.
33  /// If number `n` of elements intended for vector be not `S`, then throw.
34  /// \param n Number of elements intended for vector.
35  v_stor(size_t n= S): cview_(w_vector_view_array(d_, 1, S)) {
36  if(n != S) throw "mismatch in size";
37  }
38 
39  /// Pointer to GSL's interface to vector.
40  /// @return Pointer to GSL's interface to vector.
41  auto *v() { return &cview_.vector; }
42 
43  /// Pointer to GSL's interface to vector.
44  /// @return Pointer to GSL's interface to immutable vector.
45  auto const *v() const { return &cview_.vector; }
46 };
47 
48 
49 /// Identifier for each of two possible allocation-methods.
50 enum alloc_type {
51  ALLOC, ///< Just allocate without initialization.
52  CALLOC ///< Initialize each element to zero after allocation.
53 };
54 
55 
56 /// Specialization, which is interface to storage with two key properties:
57 /// (1) that size of storage is determined dynamically, at run-time, and
58 /// (2) that it is owned by instance of interface.
59 ///
60 /// Generic gsl::v_stor<T,S> is for storage-size `S` determined at
61 /// compile-time.
62 ///
63 /// Move-construction is provided, but move-assignment is not. Once memory is
64 /// allocated for a vector, that memory and only that memory belongs to the
65 /// vector until it is destroyed.
66 ///
67 /// @tparam T Type of each element in vector.
68 template<typename T> class v_stor<T> {
69  v_stor(v_stor const &)= delete; ///< Disable copy-construction.
70  v_stor &operator=(v_stor const &)= delete; ///< Disable copy-assignment.
71 
72 protected:
73  /// Pointer to allocated descriptor for vector.
74  w_vector<T> *v_= nullptr;
75 
76  /// Deallocate vector and its descriptor.
77  void free() {
78  if(v_) w_free(v_);
79  v_= nullptr;
80  }
81 
82  /// Allocate vector and its descriptor.
83  /// \param n Number of elements in vector.
84  /// \param a Type of allocation (simple or initialized to zero).
85  /// \return Pointer to vector's descriptor.
86  w_vector<T> *allocate(size_t n, alloc_type a) {
87  free();
88  if(a == ALLOC) return w_vector_alloc<T>(n);
89  return w_vector_calloc<T>(n);
90  }
91 
92 public:
93  /// Allocate vector and its descriptor.
94  /// @param n Number of elements in vector.
95  /// @param a Method to use for allocation.
96  v_stor(size_t n, alloc_type a= ALLOC) { v_= allocate(n, a); }
97 
98  /// True if object's data have been validly allocated.
99  /// \return True if object's data have been validly allocated.
100  bool valid() const { return v_!= nullptr; }
101 
102  /// Reference to GSL's interface to vector.
103  /// @return Reference to GSL's interface to vector.
104  auto *v() { return v_; }
105 
106  /// Reference to GSL's interface to vector.
107  /// @return Reference to GSL's interface to immutable vector.
108  auto const *v() const { return v_; }
109 
110  /// Move on construction.
111  /// Constructor is not template because moving works only from other %v_stor.
112  /// \param src Vector to move.
113  v_stor(v_stor &&src): v_(src.v_) { src.v_= nullptr; }
114 
115  /// Deallocate vector and its descriptor.
116  virtual ~v_stor() { free(); }
117 };
118 
119 
120 } // namespace gsl
121 
122 // EOF
gsl::v_stor::v
auto * v()
Pointer to GSL's interface to vector.
Definition: v-stor.hpp:41
gsl::v_stor::v_stor
v_stor(size_t n=S)
Initialize GSL's view of static storage, but do not initialize elements.
Definition: v-stor.hpp:35
gsl::v_stor< T >::free
void free()
Deallocate vector and its descriptor.
Definition: v-stor.hpp:77
gsl::v_stor::operator=
v_stor & operator=(v_stor const &)=delete
Disable copy-assignment.
gsl::v_stor< T >::v
auto * v()
Reference to GSL's interface to vector.
Definition: v-stor.hpp:104
gsl::v_stor
Generic v_stor is interface to storage with two key properties: (1) that size of storage is known sta...
Definition: v-stor.hpp:22
gsl::v_stor< T >::allocate
w_vector< T > * allocate(size_t n, alloc_type a)
Allocate vector and its descriptor.
Definition: v-stor.hpp:86
gsl::v_stor< T >::v_stor
v_stor(size_t n, alloc_type a=ALLOC)
Allocate vector and its descriptor.
Definition: v-stor.hpp:96
gsl::v_stor< T >::v
const auto * v() const
Reference to GSL's interface to vector.
Definition: v-stor.hpp:108
gsl::v_stor< T >::operator=
v_stor & operator=(v_stor const &)=delete
Disable copy-assignment.
gsl::v_stor::d_
T d_[S]
Storage for data.
Definition: v-stor.hpp:23
gsl::v_stor< T >::valid
bool valid() const
True if object's data have been validly allocated.
Definition: v-stor.hpp:100
gsl::alloc_type
alloc_type
Identifier for each of two possible allocation-methods.
Definition: v-stor.hpp:50
gsl::v_stor< T >::v_stor
v_stor(v_stor &&src)
Move on construction.
Definition: v-stor.hpp:113
gsl::v_stor< T >::~v_stor
virtual ~v_stor()
Deallocate vector and its descriptor.
Definition: v-stor.hpp:116
gsl::v_stor::v_stor
v_stor(v_stor const &)=delete
Disable copy-construction.
gsl::v_stor::cview_
w_vector_view< T > cview_
GSL's view of data.
Definition: v-stor.hpp:26
gsl::CALLOC
@ CALLOC
Initialize each element to zero after allocation.
Definition: v-stor.hpp:52
gsl::v_stor::v
const auto * v() const
Pointer to GSL's interface to vector.
Definition: v-stor.hpp:45
gsl::v_stor< T >::v_
w_vector< T > * v_
Pointer to allocated descriptor for vector.
Definition: v-stor.hpp:74
gsl
Namespace for C++-interface to GSL.
Definition: v-iface.hpp:51
gsl::ALLOC
@ ALLOC
Just allocate without initialization.
Definition: v-stor.hpp:51
gsl::v_stor< T >::v_stor
v_stor(v_stor const &)=delete
Disable copy-construction.