OmniSciDB  2e3a973ef4
TrackedPtr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "ParserNode.h"
20 
21 namespace Parser {
22 
29 template <typename T>
30 class TrackedPtr {
31  static_assert(std::is_same<std::string, T>::value || std::is_same<Node, T>::value);
32 
33  public:
34  TrackedPtr(const TrackedPtr&) = delete;
35 
36  TrackedPtr& operator=(const TrackedPtr&) = delete;
37 
42  T* release() {
43  if (is_empty_) {
44  return nullptr;
45  }
47  is_released_ = true;
48  return value_.release();
49  }
50 
54  T* get() {
55  if (is_empty_) {
56  return nullptr;
57  }
58  return value_.get();
59  }
60 
69  template <typename... Args>
70  static TrackedPtr<T>* make(std::vector<std::unique_ptr<TrackedPtr<T>>>& tracked_ptrs,
71  Args&&... args) {
72  std::unique_ptr<TrackedPtr<T>> tracked_ptr{
73  new TrackedPtr<T>(std::forward<Args>(args)...)};
74  const auto& ptr = tracked_ptrs.emplace_back(std::move(tracked_ptr));
75  return ptr.get();
76  }
77 
82  static std::unique_ptr<TrackedPtr<T>> empty_tracked_ptr_ =
83  std::unique_ptr<TrackedPtr<T>>(new TrackedPtr<T>());
84  return empty_tracked_ptr_.get();
85  }
86 
87  private:
88  std::unique_ptr<T> value_;
89  bool is_empty_;
91 
92  TrackedPtr() : is_empty_(true), is_released_(false){};
93 
95  : value_(std::unique_ptr<Node>(node)), is_empty_(false), is_released_(false) {}
96 
97  TrackedPtr(const std::string& str)
98  : value_(std::make_unique<std::string>(str))
99  , is_empty_(false)
100  , is_released_(false) {}
101 
102  TrackedPtr(const char* str, size_t len)
103  : value_(std::make_unique<std::string>(str, len))
104  , is_empty_(false)
105  , is_released_(false) {}
106 };
107 
114 template <typename T>
116  static_assert(std::is_same<std::string, T>::value || std::is_same<Node, T>::value);
117 
118  public:
119  TrackedListPtr(const TrackedListPtr&) = delete;
120 
121  TrackedListPtr& operator=(const TrackedListPtr&) = delete;
122 
127  std::list<T*>* release() {
128  if (is_empty_) {
129  return nullptr;
130  }
132  is_released_ = true;
133 
134  auto result = new std::list<T*>();
135  for (auto& ptr : *value_) {
136  result->emplace_back(ptr->release());
137  }
138  return result;
139  }
140 
147  void push_back(TrackedPtr<T>* item) { value_->emplace_back(item); }
148 
156  void push_back(T* item) {
157  value_->emplace_back(TrackedPtr<T>::make(owned_ptrs_, item));
158  }
159 
169  template <typename... Args>
171  std::vector<std::unique_ptr<TrackedListPtr<T>>>& tracked_ptrs,
172  Args&&... args) {
173  std::unique_ptr<TrackedListPtr<T>> tracked_ptr{
174  new TrackedListPtr<T>(std::forward<Args>(args)...)};
175  const auto& ptr = tracked_ptrs.emplace_back(std::move(tracked_ptr));
176  return ptr.get();
177  }
178 
184  static std::unique_ptr<TrackedListPtr<T>> empty_tracked_ptr_ =
185  std::unique_ptr<TrackedListPtr<T>>(new TrackedListPtr<T>());
186  return empty_tracked_ptr_.get();
187  }
188 
189  private:
190  std::unique_ptr<std::list<TrackedPtr<T>*>> value_;
191  std::vector<std::unique_ptr<TrackedPtr<T>>> owned_ptrs_{};
192  bool is_empty_;
194 
195  TrackedListPtr() : is_empty_(true), is_released_(false){};
196 
197  TrackedListPtr(size_t n)
198  : value_(std::make_unique<std::list<TrackedPtr<T>*>>(n))
199  , is_empty_(false)
200  , is_released_(false) {}
201 
203  : value_(std::make_unique<std::list<TrackedPtr<T>*>>(n, val))
204  , is_empty_(false)
205  , is_released_(false) {}
206 
207  TrackedListPtr(size_t n, T* val)
208  : value_(std::make_unique<std::list<TrackedPtr<T>*>>(
209  n,
210  TrackedPtr<T>::make(owned_ptrs_, (T*)val)))
211  , is_empty_(false)
212  , is_released_(false) {}
213 };
214 } // namespace Parser
std::unique_ptr< std::list< TrackedPtr< T > * > > value_
Definition: TrackedPtr.h:190
static TrackedListPtr< T > * make(std::vector< std::unique_ptr< TrackedListPtr< T >>> &tracked_ptrs, Args &&... args)
Definition: TrackedPtr.h:170
std::list< T * > * release()
Definition: TrackedPtr.h:127
static TrackedPtr< T > * make(std::vector< std::unique_ptr< TrackedPtr< T >>> &tracked_ptrs, Args &&... args)
Definition: TrackedPtr.h:70
TrackedListPtr(size_t n)
Definition: TrackedPtr.h:197
TrackedListPtr(size_t n, T *val)
Definition: TrackedPtr.h:207
Classes representing a parse tree.
TrackedPtr(const char *str, size_t len)
Definition: TrackedPtr.h:102
std::unique_ptr< T > value_
Definition: TrackedPtr.h:88
void push_back(T *item)
Definition: TrackedPtr.h:156
static TrackedListPtr< T > * makeEmpty()
Definition: TrackedPtr.h:183
TrackedPtr & operator=(const TrackedPtr &)=delete
static TrackedPtr< T > * makeEmpty()
Definition: TrackedPtr.h:81
Definition: Catalog.h:62
TrackedPtr(const std::string &str)
Definition: TrackedPtr.h:97
#define CHECK(condition)
Definition: Logger.h:197
void push_back(TrackedPtr< T > *item)
Definition: TrackedPtr.h:147
TrackedListPtr(size_t n, TrackedPtr< T > *val)
Definition: TrackedPtr.h:202
TrackedPtr(Node *node)
Definition: TrackedPtr.h:94