Skip to content
/ mstd Public

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++.

License

Notifications You must be signed in to change notification settings

MAIPA01/mstd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

    #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 as to_string() and size<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() 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:

    #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.

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

#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;
}

Overflow Detection

#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;
}

Function Type Comparison

#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;
}

License

This project is licensed under the BSD 3-Clause License with Attribution Requirement. See the LICENSE file for more details.

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++.

Topics

Resources

License

Stars

Watchers

Forks