Skip to content

Commit

Permalink
C++ use nested namespaces for less right shift (#4213)
Browse files Browse the repository at this point in the history
### What

Changes codegen to use C++17 [nested
namespaces](https://en.cppreference.com/w/cpp/language/namespace).

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/4213) (if
applicable)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4213)
- [Docs
preview](https://rerun.io/preview/81288b0897c92c019187ad36de00df5d0764747a/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/81288b0897c92c019187ad36de00df5d0764747a/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://ref.rerun.io/dev/bench/)
- [Wasm size tracking](https://ref.rerun.io/dev/sizes/)
  • Loading branch information
Wumpf authored Nov 13, 2023
1 parent fe293f5 commit dcdde90
Show file tree
Hide file tree
Showing 253 changed files with 14,750 additions and 15,688 deletions.
202 changes: 96 additions & 106 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,28 +492,26 @@ impl QuotedObject {
let hpp = quote! {
#hpp_includes

namespace rerun {
namespace archetypes {
#quoted_docs
struct #type_ident {
#(#field_declarations;)*
namespace rerun::archetypes {
#quoted_docs
struct #type_ident {
#(#field_declarations;)*

#(#constants_hpp;)*
#(#constants_hpp;)*

#NEWLINE_TOKEN
#indicator_comment
using IndicatorComponent = components::IndicatorComponent<INDICATOR_COMPONENT_NAME>;
#NEWLINE_TOKEN
#indicator_comment
using IndicatorComponent = components::IndicatorComponent<INDICATOR_COMPONENT_NAME>;

#hpp_type_extensions
#hpp_type_extensions

#hpp_method_section
};
#NEWLINE_TOKEN
#NEWLINE_TOKEN
}
#hpp_method_section
};
#NEWLINE_TOKEN
#NEWLINE_TOKEN
}

namespace rerun {
// Instead of including as_components.hpp, simply re-declare the template since it's trivial
#doc_hide_comment
template<typename T>
Expand All @@ -533,12 +531,13 @@ impl QuotedObject {
let cpp = quote! {
#cpp_includes

namespace rerun {
namespace archetypes {
#(#constants_cpp;)*
namespace rerun::archetypes {
#(#constants_cpp;)*

#(#methods_cpp)*
}
#(#methods_cpp)*
}

namespace rerun {
#NEWLINE_TOKEN
#NEWLINE_TOKEN
#serialize_cpp
Expand Down Expand Up @@ -663,19 +662,17 @@ impl QuotedObject {

#hpp_declarations

namespace rerun {
namespace #namespace_ident {
#quoted_docs
struct #type_ident {
#(#field_declarations;)*
namespace rerun::#namespace_ident {
#quoted_docs
struct #type_ident {
#(#field_declarations;)*

#(#constants_hpp;)*
#(#constants_hpp;)*

#hpp_type_extensions
#hpp_type_extensions

#hpp_method_section
};
}
#hpp_method_section
};
}
};

Expand All @@ -685,12 +682,10 @@ impl QuotedObject {
let cpp = quote! {
#cpp_includes

namespace rerun {
namespace #namespace_ident {
#(#constants_cpp;)*
namespace rerun::#namespace_ident {
#(#constants_cpp;)*

#(#methods_cpp)*
}
#(#methods_cpp)*
}
};

Expand Down Expand Up @@ -1002,86 +997,83 @@ impl QuotedObject {

#hpp_declarations

namespace rerun {
namespace #namespace_ident {
namespace detail {
#hide_from_docs_comment
enum class #tag_typename : uint8_t {
#(#tag_fields)*
};

#hide_from_docs_comment
union #data_typename {
#(#enum_data_declarations;)*

// Required by static constructors
#data_typename() {
std::memset(reinterpret_cast<void*>(this), 0, sizeof(#data_typename));
}
~#data_typename() { }

// Note that this type is *not* copyable unless all enum fields are trivially destructable.

void swap(#data_typename& other) noexcept {
#NEWLINE_TOKEN
#swap_comment
char temp[sizeof(#data_typename)];
void* otherbytes = reinterpret_cast<void*>(&other);
void* thisbytes = reinterpret_cast<void*>(this);
std::memcpy(temp, thisbytes, sizeof(#data_typename));
std::memcpy(thisbytes, otherbytes, sizeof(#data_typename));
std::memcpy(otherbytes, temp, sizeof(#data_typename));
}
};

}
namespace rerun::#namespace_ident {
namespace detail {
#hide_from_docs_comment
enum class #tag_typename : uint8_t {
#(#tag_fields)*
};

#quoted_docs
struct #pascal_case_ident {
#(#constants_hpp;)*
#hide_from_docs_comment
union #data_typename {
#(#enum_data_declarations;)*

#pascal_case_ident() : _tag(detail::#tag_typename::None) {}
// Required by static constructors
#data_typename() {
std::memset(reinterpret_cast<void*>(this), 0, sizeof(#data_typename));
}
~#data_typename() { }

#copy_constructor
// Note that this type is *not* copyable unless all enum fields are trivially destructable.

// Copy-assignment
#pascal_case_ident& operator=(const #pascal_case_ident& other) noexcept {
#pascal_case_ident tmp(other);
this->swap(tmp);
return *this;
void swap(#data_typename& other) noexcept {
#NEWLINE_TOKEN
#swap_comment
char temp[sizeof(#data_typename)];
void* otherbytes = reinterpret_cast<void*>(&other);
void* thisbytes = reinterpret_cast<void*>(this);
std::memcpy(temp, thisbytes, sizeof(#data_typename));
std::memcpy(thisbytes, otherbytes, sizeof(#data_typename));
std::memcpy(otherbytes, temp, sizeof(#data_typename));
}
};
}

// Move-constructor:
#pascal_case_ident(#pascal_case_ident&& other) noexcept : #pascal_case_ident() {
this->swap(other);
}
#quoted_docs
struct #pascal_case_ident {
#(#constants_hpp;)*

// Move-assignment:
#pascal_case_ident& operator=(#pascal_case_ident&& other) noexcept {
this->swap(other);
return *this;
}
#pascal_case_ident() : _tag(detail::#tag_typename::None) {}

#destructor
#copy_constructor

#hpp_type_extensions
// Copy-assignment
#pascal_case_ident& operator=(const #pascal_case_ident& other) noexcept {
#pascal_case_ident tmp(other);
this->swap(tmp);
return *this;
}

// This is useful for easily implementing the move constructor and assignment operators:
void swap(#pascal_case_ident& other) noexcept {
// Swap tags: Not using std::swap here causes a warning for some gcc version about potentially uninitialized data.
std::swap(this->_tag, other._tag);
// Move-constructor:
#pascal_case_ident(#pascal_case_ident&& other) noexcept : #pascal_case_ident() {
this->swap(other);
}

// Swap data:
this->_data.swap(other._data);
}
// Move-assignment:
#pascal_case_ident& operator=(#pascal_case_ident&& other) noexcept {
this->swap(other);
return *this;
}

#(#methods_hpp)*
#destructor

private:
detail::#tag_typename _tag;
detail::#data_typename _data;
};
}
#hpp_type_extensions

// This is useful for easily implementing the move constructor and assignment operators:
void swap(#pascal_case_ident& other) noexcept {
// Swap tags: Not using std::swap here causes a warning for some gcc version about potentially uninitialized data.
std::swap(this->_tag, other._tag);

// Swap data:
this->_data.swap(other._data);
}

#(#methods_hpp)*

private:
detail::#tag_typename _tag;
detail::#data_typename _data;
};
}
};

Expand All @@ -1093,10 +1085,8 @@ impl QuotedObject {

#(#constants_cpp;)*

namespace rerun {
namespace #namespace_ident {
#(#cpp_methods)*
}
namespace rerun::#namespace_ident {
#(#cpp_methods)*
}
};

Expand Down
9 changes: 5 additions & 4 deletions rerun_cpp/src/rerun/archetypes/annotation_context.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dcdde90

Please sign in to comment.