An open API service indexing awesome lists of open source software.

https://github.com/maipa01/mstd

mstd (Maipa's Standard Library) is an extension of the C++ standard library, providing additional utility functions, new data types, and helpful templates for type comparisons. It aims to enhance the standard functionality with useful features while maintaining compatibility with modern C++.
https://github.com/maipa01/mstd

bsd-3-clause cpp cpp-library cpp20 cross-platform data-structures function-type-traits header-only macros macros-cpp open-source ordered-map ordered-set overflow-detection standard-library-extension string-utilities terminal-utilities utility-library

Last synced: 2 months ago
JSON representation

mstd (Maipa's Standard Library) is an extension of the C++ standard library, providing additional utility functions, new data types, and helpful templates for type comparisons. It aims to enhance the standard functionality with useful features while maintaining compatibility with modern C++.

Awesome Lists containing this project

README

        

# mstd - Maipa's Standard Library

## About

**mstd** (Maipa's Standard Library) is an extension of the C++ standard library, providing additional utility functions, new data types, and helpful templates for type comparisons. It aims to enhance the standard functionality with useful features while maintaining compatibility with modern C++.

## Features

- **Extended String Manipulation**: Additional functions for modifying and handling strings.
- **Terminal Utilities**: Functions for clearing the terminal and retrieving its dimensions.
- **New Data Structures**:
- `ordered_map`: A map that preserves the insertion order, similar to `vector`.
- `ordered_set`: A set that maintains insertion order, like `vector`.
- **Safe Arithmetic Operations**: Functions for performing arithmetic with overflow detection:
- `add_overflow`
- `sub_overflow`
- `mul_overflow`
- `div_overflow`
- **Type Comparisons for Functions**: Templates that allow comparing function types, treating lambdas and function pointers as equivalent when they share the same signature.

## Macros

Before including the `mstd.hpp` or `macros.hpp` header, you can define certain macros to enable specific sets of utility functions and features within the library.

### Available Macros:

- **USE_FOR_EACH_MACROS**: This macro enables access to the `DO_FOR_EACH` macro, which allows you to iterate over multiple elements and apply a function to each element.

Example usage:
```cpp
#define USE_FOR_EACH_MACROS
#include

#define TO_STR(x) #x
// Example of DO_FOR_EACH with TO_STR macro
DO_FOR_EACH(TO_STR, 1, 2, 3); // Outputs: "1" "2" "3"
```

- **USE_ENUMS_MACROS**: This macro enables access to the `ENUM` macro, which allows you to define enums or enum classes along with helper functions such as `to_string()` and `size()`.

Example usage:
```cpp
#define USE_ENUMS_MACROS
#include

// Defining an enum using ENUM macro
ENUM(Color, Red, Green, Blue);

// Accessing helper functions generated by ENUM macro
std::cout << to_string(Color::Red) << std::endl; // Outputs: Red
std::cout << size() << std::endl; // Outputs: 3 (the number of elements in the enum)
```

- **USE_CLONE_FUNC_MACROS**: This macro enables access to macros that help you define basic cloning functions like `Clone()` and `CloneTo()` for your custom classes. Specifically, it uses the `CloneFuncInClassDefinition` macro to automatically generate the `Clone()` function that returns a pointer to a cloned object.

Example usage:
```cpp
#define USE_CLONE_FUNC_MACROS
#include

// Example of a class with Clone() function returning a pointer using CloneFuncInClassDefinition macro
class MyClass {
public:
int value;
MyClass(int val) : value(val) {}

// Using CloneFuncInClassDefinition to define Clone() method
CloneFuncInClassDefinition(MyClass, value)
};

MyClass original(10);
MyClass* copy = original.Clone(); // Creates a deep copy of original and returns a pointer
std::cout << "Cloned value: " << copy->value << std::endl; // Outputs: Cloned value: 10
```

To enable these macros, define them before including `mstd.hpp`:

```cpp
#define USE_FOR_EACH_MACROS
#define USE_ENUMS_MACROS
#define USE_CLONE_FUNC_MACROS
#include
```

You can enable one or more of these macros depending on your needs.

## Requirements

- **C++ Standard**: The library is designed for **C++20** and later.
- **Supported Platforms**: The library is intended to work on **Windows, macOS, and Linux**.

## Installation

No additional dependencies are required. Simply include the **mstd** headers in your project.

## Usage Examples

### Ordered Map & Ordered Set

```cpp
#include
#include

using namespace std;
using namespace mstd;

int main() {
ordered_map om;
om[1] = "first";
om[2] = "second";
om[3] = "third";

for (const auto& [key, value] : om) {
cout << key << ": " << value << endl;
}
return 0;
}
```

### Overflow Detection

```cpp
#include
#include

using namespace std;
using namespace mstd;

int main() {
size_t result;
if (add_overflow(1, 2, result)) {
cout << "Overflow occurred!" << endl;
} else {
cout << "Result: " << result << endl;
}
return 0;
}
```

### Function Type Comparison

```cpp
#include
#include

using namespace std;
using namespace mstd;

bool testFunc(string s) { return !s.empty(); }

int main() {
auto lambda = [](string s) -> bool { return !s.empty(); };
static_assert(is_same_function_v);
cout << "Lambda and function pointer are of the same type!" << endl;
return 0;
}
```

## License

This project is licensed under the **BSD 3-Clause License with Attribution Requirement**. See the [`LICENSE`](./LICENSE) file for more details.