Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8065543a authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Add namespace in bluetooth_packetgen

::bluetooth is our top level namespace. Each subdirectory is a
namespace.

Test: bluetooth_packet_parser_test
Change-Id: Ica38a448c692396e6a34dd66eabc3eaa8df884e0
parent 503e5cc2
Loading
Loading
Loading
Loading
+41 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <fstream>
#include <iostream>
#include <queue>
#include <regex>
#include <sstream>
#include <vector>

@@ -33,6 +34,33 @@ void yylex_destroy(void*);
void yyset_debug(int, void*);
void yyset_in(FILE*, void*);

namespace {

const std::string kBluetoothTopNamespace = "bluetooth";

void parse_namespace(std::filesystem::path input_file_relative_path, std::vector<std::string>& token) {
  std::filesystem::path gen_namespace = kBluetoothTopNamespace / input_file_relative_path;
  std::string gen_namespace_str = gen_namespace;
  std::regex path_tokenizer("/");
  auto it = std::sregex_token_iterator(gen_namespace_str.cbegin(), gen_namespace_str.cend(), path_tokenizer, -1);
  std::sregex_token_iterator it_end;
  for (; it != it_end; ++it) {
    token.push_back(it->str());
  }
}

void generate_namespace_open(const std::vector<std::string>& token, std::ostream& output) {
  for (auto it = token.begin(); it != token.end(); ++it) {
    output << "namespace " << *it << " {" << std::endl;
  }
}

void generate_namespace_close(const std::vector<std::string>& token, std::ostream& output) {
  for (auto it = token.rbegin(); it != token.rend(); ++it) {
    output << "}  //namespace " << *it << std::endl;
  }
}

bool parse_one_file(std::filesystem::path input_file, std::filesystem::path include_dir,
                    std::filesystem::path out_dir) {
  auto gen_relative_path = input_file.lexically_relative(include_dir).parent_path();
@@ -83,11 +111,15 @@ bool parse_one_file(std::filesystem::path input_file, std::filesystem::path incl
  out_file << "#include \"packet/packet_view.h\"\n";
  out_file << "\n\n";

  out_file << "using bluetooth::packet::BasePacketBuilder;";
  out_file << "using bluetooth::packet::BitInserter;";
  out_file << "using bluetooth::packet::kLittleEndian;";
  out_file << "using bluetooth::packet::PacketBuilder;";
  out_file << "using bluetooth::packet::PacketView;";
  std::vector<std::string> namespace_list;
  parse_namespace(gen_relative_path, namespace_list);
  generate_namespace_open(namespace_list, out_file);

  out_file << "using ::bluetooth::packet::BasePacketBuilder;";
  out_file << "using ::bluetooth::packet::BitInserter;";
  out_file << "using ::bluetooth::packet::kLittleEndian;";
  out_file << "using ::bluetooth::packet::PacketBuilder;";
  out_file << "using ::bluetooth::packet::PacketView;";
  out_file << "\n\n";

  for (const auto& e : decls.enum_defs_queue_) {
@@ -112,12 +144,16 @@ bool parse_one_file(std::filesystem::path input_file, std::filesystem::path incl
    out_file << "\n\n";
  }

  generate_namespace_close(namespace_list, out_file);

  out_file.close();
  fclose(in_file);

  return true;
}

}  // namespace

int main(int argc, const char** argv) {
  std::filesystem::path out_dir;
  std::filesystem::path include_dir;
+4 −3
Original line number Diff line number Diff line
@@ -24,9 +24,9 @@
#include "packet/bit_inserter.h"
#include "packet/raw_builder.h"

using bluetooth::packet::BitInserter;
using bluetooth::packet::kLittleEndian;
using bluetooth::packet::RawBuilder;
using ::bluetooth::packet::BitInserter;
using ::bluetooth::packet::kLittleEndian;
using ::bluetooth::packet::RawBuilder;
using std::vector;

namespace {
@@ -49,6 +49,7 @@ vector<uint8_t> child = {
namespace bluetooth {
namespace packet {
namespace parser {
using namespace test;

TEST(GeneratedPacketTest, testChildTwoTwoThree) {
  auto packet = ChildTwoTwoThreeBuilder::Create();