Always use struct
This commit is contained in:
@@ -83,7 +83,7 @@
|
||||
* @warning Do not share ArenaAllocator instances between threads. Use separate
|
||||
* instances per thread or per logical unit of work.
|
||||
*/
|
||||
class ArenaAllocator {
|
||||
struct ArenaAllocator {
|
||||
private:
|
||||
/**
|
||||
* @brief Internal block structure for the intrusive linked list.
|
||||
|
||||
@@ -60,7 +60,7 @@ struct Operation {
|
||||
* memory management and ownership. This class has no knowledge of any
|
||||
* specific serialization formats or encoding schemes.
|
||||
*/
|
||||
class CommitRequest {
|
||||
struct CommitRequest {
|
||||
private:
|
||||
ArenaAllocator arena_;
|
||||
std::optional<std::string_view> request_id_;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* and streaming parsing (for incremental data processing). This allows
|
||||
* efficient handling of network protocols where data may arrive in chunks.
|
||||
*/
|
||||
class CommitRequestParser {
|
||||
struct CommitRequestParser {
|
||||
public:
|
||||
/**
|
||||
* @brief Status returned by streaming parse operations.
|
||||
|
||||
@@ -83,7 +83,7 @@ struct Config {
|
||||
* auto config2 = ConfigParser::parse_toml_string(toml);
|
||||
* ```
|
||||
*/
|
||||
class ConfigParser {
|
||||
struct ConfigParser {
|
||||
public:
|
||||
/**
|
||||
* @brief Load configuration from a TOML file.
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "connection.hpp"
|
||||
#include "server.hpp" // Need this for releaseBackToServer implementation
|
||||
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "server.hpp" // Need this for releaseBackToServer implementation
|
||||
|
||||
// Static thread-local storage for iovec buffer
|
||||
static thread_local std::vector<struct iovec> g_iovec_buffer{IOV_MAX};
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "arena_allocator.hpp"
|
||||
#include "connection_handler.hpp"
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
@@ -10,6 +8,9 @@
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "arena_allocator.hpp"
|
||||
#include "connection_handler.hpp"
|
||||
|
||||
#ifndef __has_feature
|
||||
#define __has_feature(x) 0
|
||||
#endif
|
||||
@@ -39,7 +40,7 @@
|
||||
* private.
|
||||
*/
|
||||
// Forward declaration
|
||||
class Server;
|
||||
struct Server;
|
||||
|
||||
struct Connection {
|
||||
// No public constructor or factory method - only Server can create
|
||||
@@ -305,7 +306,7 @@ struct Connection {
|
||||
|
||||
private:
|
||||
// Server is a friend and can access all networking internals
|
||||
friend class Server;
|
||||
friend struct Server;
|
||||
|
||||
/**
|
||||
* @brief Private constructor - only accessible by Server.
|
||||
|
||||
@@ -17,7 +17,7 @@ struct Connection;
|
||||
* The networking layer manages connection lifecycle, I/O multiplexing,
|
||||
* and efficient data transfer, while handlers focus purely on protocol logic.
|
||||
*/
|
||||
class ConnectionHandler {
|
||||
struct ConnectionHandler {
|
||||
public:
|
||||
virtual ~ConnectionHandler() = default;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ struct Connection;
|
||||
* allocated on-demand as connections are created.
|
||||
*
|
||||
*/
|
||||
class ConnectionRegistry {
|
||||
struct ConnectionRegistry {
|
||||
public:
|
||||
/**
|
||||
* Initialize the connection registry.
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
#include <llhttp.h>
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "connection_handler.hpp"
|
||||
#include "perfetto_categories.hpp"
|
||||
#include "server.hpp"
|
||||
#include "thread_pipeline.hpp"
|
||||
#include <llhttp.h>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
/**
|
||||
* HTTP routes supported by WeaselDB server.
|
||||
@@ -60,14 +62,7 @@ struct HttpConnectionState {
|
||||
* HTTP/1.1 server implementation using llhttp for parsing.
|
||||
* Supports the WeaselDB REST API endpoints with enum-based routing.
|
||||
*/
|
||||
class HttpHandler : public ConnectionHandler {
|
||||
static constexpr int kFinalStageThreads = 2;
|
||||
static constexpr int kLogSize = 12;
|
||||
ThreadPipeline<std::unique_ptr<Connection>> pipeline{kLogSize,
|
||||
{kFinalStageThreads}};
|
||||
std::vector<std::thread> finalStageThreads;
|
||||
|
||||
public:
|
||||
struct HttpHandler : ConnectionHandler {
|
||||
HttpHandler() {
|
||||
for (int threadId = 0; threadId < kFinalStageThreads; ++threadId) {
|
||||
finalStageThreads.emplace_back([this, threadId]() {
|
||||
@@ -124,6 +119,12 @@ public:
|
||||
static int onMessageComplete(llhttp_t *parser);
|
||||
|
||||
private:
|
||||
static constexpr int kFinalStageThreads = 2;
|
||||
static constexpr int kLogSize = 12;
|
||||
ThreadPipeline<std::unique_ptr<Connection>> pipeline{kLogSize,
|
||||
{kFinalStageThreads}};
|
||||
std::vector<std::thread> finalStageThreads;
|
||||
|
||||
// Route handlers
|
||||
void handleGetVersion(Connection &conn, const HttpConnectionState &state);
|
||||
void handlePostCommit(Connection &conn, const HttpConnectionState &state);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
* This parser uses the weaseljson library to parse JSON-formatted
|
||||
* commit requests into CommitRequest objects.
|
||||
*/
|
||||
class JsonCommitRequestParser : public CommitRequestParser {
|
||||
struct JsonCommitRequestParser : CommitRequestParser {
|
||||
public:
|
||||
// Parser state
|
||||
enum class ParseState {
|
||||
|
||||
@@ -33,7 +33,7 @@ struct JsonToken {
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
class PerfectHash {
|
||||
struct PerfectHash {
|
||||
public:
|
||||
/**
|
||||
* @brief Look up a JSON token by name using perfect hash.
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "server.hpp"
|
||||
#include "connection.hpp"
|
||||
#include "connection_registry.hpp"
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -17,6 +16,9 @@
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "connection_registry.hpp"
|
||||
|
||||
// Static thread-local storage for read buffer (used across different functions)
|
||||
static thread_local std::vector<char> g_read_buffer;
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "config.hpp"
|
||||
#include "connection_handler.hpp"
|
||||
#include "connection_registry.hpp"
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "connection_handler.hpp"
|
||||
#include "connection_registry.hpp"
|
||||
|
||||
/**
|
||||
* High-performance multi-threaded server for handling network connections.
|
||||
*
|
||||
@@ -32,8 +33,7 @@
|
||||
* - Prevention of accidental stack allocation that would break safety
|
||||
* guarantees
|
||||
*/
|
||||
class Server : public std::enable_shared_from_this<Server> {
|
||||
public:
|
||||
struct Server : std::enable_shared_from_this<Server> {
|
||||
/**
|
||||
* Factory method to create a Server instance.
|
||||
*
|
||||
|
||||
27
style.md
27
style.md
@@ -72,13 +72,22 @@ void add_block(size_t size);
|
||||
uint32_t initial_block_size_;
|
||||
```
|
||||
|
||||
### Classes and Structs
|
||||
- **PascalCase** for class and struct names
|
||||
- **Prefer struct over class** - public members at the top, avoid `class { public:` pattern
|
||||
### Structs
|
||||
- **PascalCase** for struct names
|
||||
- **Always use struct** - eliminates debates about complexity and maintains consistency
|
||||
- **Public members first, private after** - leverages struct's default public access
|
||||
- Use `private:` sections when encapsulation is needed
|
||||
```cpp
|
||||
struct ArenaAllocator {};
|
||||
struct CommitRequest {};
|
||||
struct JsonCommitRequestParser {};
|
||||
struct ArenaAllocator {
|
||||
// Public interface first
|
||||
explicit ArenaAllocator(size_t initial_size = 1024);
|
||||
void* allocate_raw(size_t size);
|
||||
|
||||
private:
|
||||
// Private members after
|
||||
uint32_t initial_block_size_;
|
||||
Block* current_block_;
|
||||
};
|
||||
```
|
||||
|
||||
### Enums
|
||||
@@ -173,8 +182,8 @@ std::unique_ptr<Parser> parser;
|
||||
|
||||
## Code Structure
|
||||
|
||||
### Class Design
|
||||
- **Move-only semantics** for resource-owning classes
|
||||
### Struct Design
|
||||
- **Move-only semantics** for resource-owning structs
|
||||
- **Explicit constructors** to prevent implicit conversions
|
||||
- **Delete copy operations** when inappropriate
|
||||
```cpp
|
||||
@@ -308,7 +317,7 @@ static_assert(std::is_trivially_destructible_v<T>, "Arena requires trivially des
|
||||
## Documentation
|
||||
|
||||
### Doxygen Style
|
||||
- **/** for class and public method documentation
|
||||
- **/** for struct and public method documentation
|
||||
- **@brief** for short descriptions
|
||||
- **@param** and **@return** for function parameters
|
||||
- **@note** for important implementation notes
|
||||
|
||||
Reference in New Issue
Block a user