aboutsummaryrefslogtreecommitdiff
path: root/src/cc/table_storage.h
blob: 87aaa338314888de8771bbabb46b71807ddeda30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2017 VMware, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <cstddef>
#include <iterator>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "table_desc.h"

namespace ebpf {

class TableStorageImpl;
class TableStorageIteratorImpl;

class Path {
 public:
  static const std::string DELIM;
  Path() = default;
  Path(const Path &other) = default;
  Path &operator=(const Path &other) = default;
  Path(std::initializer_list<std::string> parts) {
    size_t len = parts.size() * DELIM.size();
    for (const auto &s : parts)
      len += s.size();
    path_.reserve(len);
    for (const auto &s : parts)
      path_ += DELIM + s;
  }
  const std::string &to_string() const { return path_; }

 private:
  std::string path_;
};

class TableStorage {
 public:
  /// iterator is an abstract class for traversing the map entries in a table
  /// storage object.
  class iterator {
   private:
    friend class TableStorage;
    iterator(const iterator &);

   public:
    typedef std::pair<const std::string, TableDesc> value_type;
    typedef std::ptrdiff_t difference_type;
    typedef value_type *pointer;
    typedef value_type &reference;
    typedef std::forward_iterator_tag iterator_category;
    typedef iterator self_type;

    iterator();
    iterator(std::unique_ptr<TableStorageIteratorImpl>);
    ~iterator();
    iterator(iterator &&);
    iterator &operator=(iterator &&);
    self_type &operator++();
    self_type operator++(int);
    bool operator==(const self_type &) const;
    bool operator!=(const self_type &) const;
    value_type &operator*() const;
    pointer operator->() const;

   private:
    std::unique_ptr<TableStorageIteratorImpl> impl_;
  };

  TableStorage();
  ~TableStorage();
  void Init(std::unique_ptr<TableStorageImpl>);

  bool Find(const Path &path, TableStorage::iterator &result) const;
  bool Insert(const Path &path, TableDesc &&desc);
  bool Delete(const Path &path);
  size_t DeletePrefix(const Path &path);

  void AddMapTypesVisitor(std::unique_ptr<MapTypesVisitor>);
  void VisitMapType(TableDesc &desc, clang::ASTContext &C, clang::QualType key_type,
                    clang::QualType leaf_type);
  iterator begin();
  iterator end();
  iterator lower_bound(const Path &p);
  iterator upper_bound(const Path &p);

 private:
  std::unique_ptr<TableStorageImpl> impl_;
  std::vector<std::unique_ptr<MapTypesVisitor>> visitors_;
};

std::unique_ptr<TableStorage> createSharedTableStorage();
std::unique_ptr<TableStorage> createBpfFsTableStorage();
}