Home | History | Annotate | Download | only in cc
      1 /*
      2  * Copyright (c) 2017 VMware, 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 #include <unistd.h>
     18 
     19 #include <clang/AST/Type.h>
     20 
     21 #include "table_storage_impl.h"
     22 
     23 namespace ebpf {
     24 
     25 using std::move;
     26 using std::string;
     27 using std::unique_ptr;
     28 
     29 const string Path::DELIM = "/";
     30 
     31 TableStorage::TableStorage() {}
     32 TableStorage::~TableStorage() {}
     33 void TableStorage::Init(unique_ptr<TableStorageImpl> impl) { impl_ = move(impl); }
     34 bool TableStorage::Find(const Path &path, TableStorage::iterator &result) const {
     35   return impl_->Find(path.to_string(), result);
     36 }
     37 bool TableStorage::Insert(const Path &path, TableDesc &&desc) {
     38   return impl_->Insert(path.to_string(), move(desc));
     39 }
     40 bool TableStorage::Delete(const Path &path) { return impl_->Delete(path.to_string()); }
     41 size_t TableStorage::DeletePrefix(const Path &path) {
     42   size_t i = 0;
     43   auto it = lower_bound(path);
     44   auto upper = upper_bound(path);
     45   while (it != upper) {
     46     it = impl_->erase(*it.impl_);
     47     ++i;
     48   }
     49   return i;
     50 }
     51 
     52 void TableStorage::AddMapTypesVisitor(unique_ptr<MapTypesVisitor> visitor) {
     53   visitors_.push_back(move(visitor));
     54 }
     55 void TableStorage::VisitMapType(TableDesc &desc, clang::ASTContext &C, clang::QualType key_type,
     56                                 clang::QualType leaf_type) {
     57   for (auto &v : visitors_)
     58     v->Visit(desc, C, key_type, leaf_type);
     59 }
     60 
     61 TableStorage::iterator TableStorage::begin() { return impl_->begin(); }
     62 TableStorage::iterator TableStorage::end() { return impl_->end(); }
     63 TableStorage::iterator TableStorage::lower_bound(const Path &p) {
     64   return impl_->lower_bound(p.to_string());
     65 }
     66 TableStorage::iterator TableStorage::upper_bound(const Path &p) {
     67   return impl_->upper_bound(p.to_string() + "\x7f");
     68 }
     69 
     70 /// TableStorage::iterator implementation
     71 TableStorage::iterator::iterator() {}
     72 TableStorage::iterator::iterator(unique_ptr<TableStorageIteratorImpl> impl) : impl_(move(impl)) {}
     73 TableStorage::iterator::iterator(const iterator &that) : impl_(that.impl_->clone()) {}
     74 TableStorage::iterator::~iterator() {}
     75 TableStorage::iterator::iterator(iterator &&that) { *this = move(that); }
     76 TableStorage::iterator &TableStorage::iterator::operator=(iterator &&that) {
     77   impl_ = move(that.impl_);
     78   return *this;
     79 }
     80 
     81 TableStorage::iterator &TableStorage::iterator::operator++() {
     82   ++*impl_;
     83   return *this;
     84 }
     85 TableStorage::iterator TableStorage::iterator::operator++(int) {
     86   iterator tmp(*this);
     87   operator++();
     88   return tmp;
     89 }
     90 bool TableStorage::iterator::operator==(const iterator &rhs) const {
     91   // assumes that the underlying pair is stored in only one place
     92   return &**impl_ == &**rhs.impl_;
     93 }
     94 bool TableStorage::iterator::operator!=(const iterator &rhs) const {
     95   return &**impl_ != &**rhs.impl_;
     96 }
     97 TableStorage::iterator::reference TableStorage::iterator::operator*() const { return **impl_; }
     98 TableStorage::iterator::pointer TableStorage::iterator::operator->() const { return &**impl_; }
     99 
    100 }  // namespace ebpf
    101