Skip to content

Commit

Permalink
clang-format compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
sturnclaw committed May 13, 2022
1 parent 16e6554 commit 7b4144b
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 188 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ PointerAlignment: Right
#SpacesInCStyleCastParentheses: false
#SpacesInParentheses: false
#SpacesInSquareBrackets: false
SpacesInLineCommentPrefix:
Maximum: -1
Minimum: 0
Standard: Cpp11
TabWidth: 4
UseTab: Always
Expand Down
9 changes: 4 additions & 5 deletions src/core/Property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ PropertyMapWrapper &PropertyMapWrapper::operator=(const PropertyMapWrapper &rhs)

// =============================================================================


void from_json(const Json &obj, Property &n)
{
n = nullptr;
Expand Down Expand Up @@ -116,7 +115,8 @@ PropertyMap::iterator PropertyMap::iterator::operator++()
{
size_t sz = map->m_keys.size();
if (map && sz > idx)
while (!map->m_keys[++idx] && idx < sz) {}
while (!map->m_keys[++idx] && idx < sz) {
}
return *this;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ void PropertyMap::SaveToJson(Json &obj)
}
}

static PropertyMap::value_type null_map_value {};
static PropertyMap::value_type null_map_value{};

// Implementation adapted from:
// https://preshing.com/20130605/the-worlds-simplest-lock-free-hash-table/
Expand All @@ -191,8 +191,7 @@ void PropertyMap::SetRef(uint32_t hash, value_type &&value)
if (m_entries >= uint32_t(m_keys.size() * 0.8))
Grow();

for (uint32_t idx = hash;; idx++)
{
for (uint32_t idx = hash;; idx++) {
idx &= m_keys.size() - 1;

uint32_t probed_key = m_keys[idx];
Expand Down
57 changes: 32 additions & 25 deletions src/core/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include "Color.h"
#include "JsonFwd.h"
#include "RefCounted.h"
#include "Quaternion.h"
#include "RefCounted.h"
#include "core/StringName.h"
#include "vector2.h"
#include "vector3.h"
Expand All @@ -23,11 +23,13 @@ class PropertyMap;
// without direct circular compilation dependency
struct PropertyMapWrapper {
public:
PropertyMapWrapper() : m_map(nullptr) {}
PropertyMapWrapper() :
m_map(nullptr) {}
PropertyMapWrapper(PropertyMap *);
~PropertyMapWrapper();

PropertyMapWrapper(const PropertyMapWrapper &rhs) : PropertyMapWrapper(rhs.m_map) {}
PropertyMapWrapper(const PropertyMapWrapper &rhs) :
PropertyMapWrapper(rhs.m_map) {}
PropertyMapWrapper &operator=(const PropertyMapWrapper &rhs);

// implicit conversion
Expand All @@ -46,10 +48,7 @@ struct PropertyMapWrapper {
mutable PropertyMap *m_map;
};

/*
* A property is a simple tagged union type
*/
class Property : public std::variant<
using PropertyBase = std::variant<
std::nullptr_t,
bool,
double,
Expand All @@ -59,18 +58,26 @@ class Property : public std::variant<
Color,
Quaternionf,
StringName,
PropertyMapWrapper
> {
PropertyMapWrapper>;

/*
* A property is a simple tagged union type
*/
class Property : public PropertyBase {
public:
using variant::variant;
using PropertyBase::PropertyBase;

// construction from string literal
template<size_t N>
Property(const char (&s)[N]) : Property(StringName(std::string_view(s, N-1), hash_32_fnv1a(s, N-1))) {}
template <size_t N>
Property(const char (&s)[N]) :
Property(StringName(std::string_view(s, N - 1), hash_32_fnv1a(s, N - 1))) {}
// have to have all of these string constructors to get around the one-implicit-conversion rule
Property(const char *s) : Property(std::string_view(s)) {}
Property(std::string_view s) : Property(StringName(s)) {}
Property(const std::string &s) : Property(StringName(std::string_view(s))) {}
Property(const char *s) :
Property(std::string_view(s)) {}
Property(std::string_view s) :
Property(StringName(s)) {}
Property(const std::string &s) :
Property(StringName(std::string_view(s))) {}

bool is_null() const { return index() == 0 || index() == std::variant_npos; }
bool is_bool() const { return index() == 1; }
Expand Down Expand Up @@ -106,9 +113,9 @@ class Property : public std::variant<
PropertyMap *get_map(PropertyMap *def = {}) const { return _get<PropertyMapWrapper>(std::move(def)); }

// helpers for common conversions
template<typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
operator T() const { return get_integer(); }
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
operator T() const { return get_number(); }

operator bool() const { return get_bool(); }
Expand All @@ -119,10 +126,10 @@ class Property : public std::variant<
operator Color() const { return get_color(); }
operator Quaternionf() const { return get_quat(); }
operator StringName() const { return get_string(); }
operator PropertyMap*() const { return get_map(); }
operator PropertyMap *() const { return get_map(); }

private:
template<typename T, typename U = T>
template <typename T, typename U = T>
U _get(T &&def) const
{
if (auto *ptr = std::get_if<U>(this))
Expand Down Expand Up @@ -177,10 +184,10 @@ class PropertyMap : public RefCounted {
bool Empty() const { return !m_entries; }

// Explicit literal string overload to ensure hashing is run at compile time
template<size_t N>
template <size_t N>
const Property &Get(const char str[N]) const
{
constexpr uint32_t hash = hash_32_fnv1a(str, N-1);
constexpr uint32_t hash = hash_32_fnv1a(str, N - 1);
return GetRef(hash).second;
}

Expand All @@ -195,10 +202,10 @@ class PropertyMap : public RefCounted {

void Clear();

iterator begin() { return iterator {this, 0}; }
iterator end() { return iterator {this, uint32_t(m_keys.size()) }; }
iterator cbegin() const { return iterator {this, 0}; }
iterator cend() const { return iterator {this, uint32_t(m_keys.size()) }; }
iterator begin() { return iterator{ this, 0 }; }
iterator end() { return iterator{ this, uint32_t(m_keys.size()) }; }
iterator cbegin() const { return iterator{ this, 0 }; }
iterator cend() const { return iterator{ this, uint32_t(m_keys.size()) }; }

operator PropertyMapWrapper() { return PropertyMapWrapper(this); }

Expand Down
2 changes: 1 addition & 1 deletion src/lua/LuaBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ void LuaObject<Body>::RegisterClass()
};

// const SerializerPair body_serializers(_body_serializer, _body_deserializer, _body_to_json, _body_from_json);
const SerializerPair body_serializers { pi_lua_body_serializer, pi_lua_body_deserializer };
const SerializerPair body_serializers{ pi_lua_body_serializer, pi_lua_body_deserializer };

LuaObjectBase::CreateClass(s_type, 0, l_methods, l_attrs, 0);
LuaObjectBase::RegisterPromotion(l_parent, s_type, LuaObject<Body>::DynamicCastPromotionTest);
Expand Down
38 changes: 19 additions & 19 deletions src/lua/LuaGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ static int l_game_start_game(lua_State *l)
}

/*
* Function: SaveGameStats
*
* Return stats about a game.
*
* > Game.SaveGameStats(filename)
*
* Parameters:
*
* filename - The filename of the save game to retrieve stats for.
* Stats will be loaded from the 'savefiles' directory in the user's game directory.
*
* Availability:
*
* 2018-02-10
*
* Status:
*
* experimental
*/
* Function: SaveGameStats
*
* Return stats about a game.
*
* > Game.SaveGameStats(filename)
*
* Parameters:
*
* filename - The filename of the save game to retrieve stats for.
* Stats will be loaded from the 'savefiles' directory in the user's game directory.
*
* Availability:
*
* 2018-02-10
*
* Status:
*
* experimental
*/
static int l_game_savegame_stats(lua_State *l)
{
std::string filename = LuaPull<std::string>(l, 1);
Expand Down
1 change: 0 additions & 1 deletion src/lua/LuaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,6 @@ bool LuaObjectBase::DeserializeFromJson(lua_State *l, const Json &obj)
return deserializer(l, obj["inner"]);
}


void *LuaObjectBase::Allocate(size_t n)
{
lua_State *l = Lua::manager->GetLuaState();
Expand Down
4 changes: 2 additions & 2 deletions src/lua/LuaPropertyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ static int l_iter_next(lua_State *l)
return 0;
}

template<>
template <>
const char *LuaObject<PropertyMap>::s_type = "PropertyMap";

template<>
template <>
void LuaObject<PropertyMap>::RegisterClass()
{
LuaMetaType<PropertyMap> metaType(s_type);
Expand Down
Loading

1 comment on commit 7b4144b

@ollobrains
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#!/usr/bin/env python3
import re
import sys
from pathlib import Path

Regex to find repeated comment blocks.

This pattern is intentionally flexible to capture repeated doc lines.

Adjust as needed for your doc style, e.g., capturing "/*" or " * " lines, etc.

DOC_COMMENT_PATTERN = re.compile(
r'(?P'
r'(?:/*[\s\S]?*/|//[^\n]\n+)' # A doc block (/* ... / or // ...)
r')\s
'
r'(?P=block)' # The same block repeated immediately after
, re.MULTILINE
)

def merge_repeated_comments(code_text: str) -> str:
"""
Merge repeated doc-comment blocks into a single instance.
Returns the transformed code text.
"""
# We repeatedly apply the regex until no more duplicates are found
# (handle scenarios with multiple repeated blocks).
merged = code_text
while True:
new_text, count = DOC_COMMENT_PATTERN.subn(r'\1', merged)
if count == 0:
break
merged = new_text
return merged

def unify_doc_spacing(code_text: str) -> str:
"""
Example function to enforce consistent spacing after doc comments.
Insert a blank line after each doc block if missing.
"""
# Insert a newline after a doc comment block if not already followed by one.
# This pattern looks for a closing '/' or last '//' line not already followed by blank line.
pattern = re.compile(r'(*/|//[^\n]
)(?P\n(?!\n))')
return pattern.sub(r'\1\n\n', code_text)

def process_file(path: Path):
original_text = path.read_text(encoding='utf-8')
merged = merge_repeated_comments(original_text)
spaced = unify_doc_spacing(merged)

# Only overwrite if there's an actual change
if spaced != original_text:
    path.write_text(spaced, encoding='utf-8')
    print(f"[UPDATED] {path}")
else:
    print(f"[NO CHANGE] {path}")

def main():
if len(sys.argv) < 2:
print("Usage: python merge_doc_comments.py [ ...]")
sys.exit(1)

for target in sys.argv[1:]:
    p = Path(target)
    if p.is_file():
        process_file(p)
    elif p.is_dir():
        for file in p.rglob('*.[ch]pp'):  # or '*.*' to match all files
            process_file(file)
    else:
        print(f"Not a valid file or directory: {p}")

if name == "main":
main()

Please sign in to comment.