Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
version_parser.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <map>
23 #include <memory>
24 #include <regex> // NOLINT
25 #include <string>
26 #include <vector>
27 
28 #include "graphar/result.h"
29 
30 namespace graphar {
31 
33 class InfoVersion {
34  public:
36  static Result<std::shared_ptr<const InfoVersion>> Parse(
37  const std::string& str) noexcept;
38 
40  InfoVersion() : version_(version2types.rbegin()->first) {}
42  explicit InfoVersion(int version) : version_(version) {
43  if (version2types.find(version) == version2types.end()) {
44  throw std::invalid_argument("Unsupported version: " +
45  std::to_string(version));
46  }
47  }
49  explicit InfoVersion(int version,
50  const std::vector<std::string>& user_define_types)
51  : version_(version), user_define_types_(user_define_types) {
52  if (version2types.find(version) == version2types.end()) {
53  throw std::invalid_argument("Unsupported version: " +
54  std::to_string(version));
55  }
56  }
58  InfoVersion(const InfoVersion& other) = default;
60  inline InfoVersion& operator=(const InfoVersion& other) = default;
61 
63  bool operator==(const InfoVersion& other) const {
64  return version_ == other.version_ &&
65  user_define_types_ == other.user_define_types_;
66  }
67 
69  int version() const { return version_; }
70 
72  const std::vector<std::string>& user_define_types() const {
73  return user_define_types_;
74  }
75 
77  std::string ToString() const {
78  std::string str = "gar/v" + std::to_string(version_);
79  if (!user_define_types_.empty()) {
80  str += " (";
81  for (auto& type : user_define_types_) {
82  str += type + ",";
83  }
84  str.back() = ')';
85  }
86  return str;
87  }
88 
90  inline bool CheckType(const std::string& type_str) const {
91  auto& types = version2types.at(version_);
92  // check if type_str is in supported types of version
93  if (std::find(types.begin(), types.end(), type_str) != types.end()) {
94  return true;
95  }
96  // check if type_str is in user defined types
97  if (std::find(user_define_types_.begin(), user_define_types_.end(),
98  type_str) != user_define_types_.end()) {
99  return true;
100  }
101  return false;
102  }
103 
104  private:
105  inline static const std::map<int, std::vector<std::string>> version2types{
106  {1, {"bool", "int32", "int64", "float", "double", "string"}},
107  };
108  int version_;
109  std::vector<std::string> user_define_types_;
110 };
111 
112 } // namespace graphar
InfoVersion(const InfoVersion &other)=default
InfoVersion(int version, const std::vector< std::string > &user_define_types)
InfoVersion & operator=(const InfoVersion &other)=default
std::string ToString() const
static Result< std::shared_ptr< const InfoVersion > > Parse(const std::string &str) noexcept
const std::vector< std::string > & user_define_types() const
bool operator==(const InfoVersion &other) const
bool CheckType(const std::string &type_str) const
InfoVersion(int version)