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++.
- 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 tovector
.ordered_set
: A set that maintains insertion order, likevector
.
- 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.
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.
-
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:
#define USE_FOR_EACH_MACROS #include <mstd/mstd.hpp> #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 asto_string()
andsize<Enum>()
.Example usage:
#define USE_ENUMS_MACROS #include <mstd/mstd.hpp> // 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<Color>() << 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()
andCloneTo()
for your custom classes. Specifically, it uses theCloneFuncInClassDefinition
macro to automatically generate theClone()
function that returns a pointer to a cloned object.Example usage:
#define USE_CLONE_FUNC_MACROS #include <mstd/mstd.hpp> // 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
:
#define USE_FOR_EACH_MACROS
#define USE_ENUMS_MACROS
#define USE_CLONE_FUNC_MACROS
#include <mstd/mstd.hpp>
You can enable one or more of these macros depending on your needs.
- C++ Standard: The library is designed for C++20 and later.
- Supported Platforms: The library is intended to work on Windows, macOS, and Linux.
No additional dependencies are required. Simply include the mstd headers in your project.
#include <mstd/mstd.hpp>
#include <iostream>
using namespace std;
using namespace mstd;
int main() {
ordered_map<int, string> om;
om[1] = "first";
om[2] = "second";
om[3] = "third";
for (const auto& [key, value] : om) {
cout << key << ": " << value << endl;
}
return 0;
}
#include <mstd/mstd.hpp>
#include <iostream>
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;
}
#include <mstd/mstd.hpp>
#include <iostream>
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<decltype(lambda), bool(*)(string)>);
cout << "Lambda and function pointer are of the same type!" << endl;
return 0;
}
This project is licensed under the BSD 3-Clause License with Attribution Requirement. See the LICENSE
file for more details.