ma-agents 1.9.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ma-agents",
3
- "version": "1.9.0",
3
+ "version": "2.0.0",
4
4
  "description": "NPX tool to install skills for AI coding agents (Claude Code, Gemini, Copilot, Kilocode, Cline, Cursor)",
5
5
  "main": "index.js",
6
6
  "bin": {
package/skills/README.md CHANGED
@@ -240,6 +240,68 @@ Enforces Modern C++ practices (RAII, Smart Pointers) to prevent memory leaks and
240
240
 
241
241
  ---
242
242
 
243
+ ### 9. C++ Robust Interfaces
244
+ **Directory:** `cpp-robust-interfaces/`
245
+
246
+ Enforces contract-based design and strong typing in C++ signatures (C++14+).
247
+
248
+ **Key Features:**
249
+ - ✅ **Contracts**: Enforces `Expects()` and `Ensures()` validation.
250
+ - ✅ **Strong Typing**: Discourages boolean blindness in favor of enums.
251
+ - ✅ **Ownership Clarity**: Forbids owning raw pointers in interfaces.
252
+ - ✅ **Span Support**: Uses `gsl::span` for safe sequence passing.
253
+
254
+ ---
255
+
256
+ ### 10. C++ Modern Composition
257
+ **Directory:** `cpp-modern-composition/`
258
+
259
+ Replaces legacy C-style patterns with modern STL and Ranges-based composition (C++14+).
260
+
261
+ **Key Features:**
262
+ - ✅ **STL Algorithms**: Mandates `<algorithm>` over manual loops.
263
+ - ✅ **Zero Macros/C-Casts**: Enforces `constexpr` and `static_cast`.
264
+ - ✅ **Universal Init**: Uses `{}` to prevent narrowing and parsing errors.
265
+
266
+ ---
267
+
268
+ ### 11. C++ Const-Correctness & Logic
269
+ **Directory:** `cpp-const-correctness/`
270
+
271
+ Enforces immutability-by-default and pushes logic to compile-time (C++14+).
272
+
273
+ **Key Features:**
274
+ - ✅ **Immutable Default**: Mandates `const` for all local variables.
275
+ - ✅ **Const Members**: Ensures inspectors are correctly marked `const`.
276
+ - ✅ **constexpr Logic**: Pushes mathematical and config logic to compile-time.
277
+
278
+ ---
279
+
280
+ ### 12. C++ Safety-First Concurrency
281
+ **Directory:** `cpp-concurrency-safety/`
282
+
283
+ Enforces safe multi-threading using RAII locking and task-based parallelism (C++14+).
284
+
285
+ **Key Features:**
286
+ - ✅ **RAII Locking**: Mandates `std::lock_guard` and `std::scoped_lock`.
287
+ - ✅ **Task-Based**: Prefers `std::async` over raw `std::thread`.
288
+ - ✅ **Race Prevention**: Enforces "No shared mutable state" policies.
289
+
290
+ ---
291
+
292
+ ### 13. Modern CMake Best Practices
293
+ **Directory:** `cmake-best-practices/`
294
+
295
+ Enforces target-based, property-oriented CMake patterns (CMake 3.0+ philosophy).
296
+
297
+ **Key Features:**
298
+ - ✅ **Target-Centric**: Bans global commands (e.g., `include_directories`) in favor of `target_*`.
299
+ - ✅ **Visibility Hygiene**: Enforces `PUBLIC`, `PRIVATE`, and `INTERFACE` scopes.
300
+ - ✅ **Feature-Based**: Uses `target_compile_features` for C++ standard management.
301
+ - ✅ **Clean Flags**: Bans global `CMAKE_CXX_FLAGS` manipulation.
302
+
303
+ ---
304
+
243
305
  ## Requirements
244
306
 
245
307
  ### All Skills
@@ -0,0 +1,60 @@
1
+ # Modern CMake Best Practices (Target-Based Approach)
2
+
3
+ This skill ensures that CMake definitions follow the "Modern CMake" philosophy (3.0+), focusing on targets and properties rather than global variables.
4
+
5
+ ## Policies
6
+
7
+ ### 1. Target-Centric Philosophy
8
+ * **Rule**: Treat targets as objects. Use `target_*` commands instead of global commands.
9
+ * **Action**:
10
+ - **Forbidden**: `include_directories()`, `link_libraries()`, `add_definitions()`.
11
+ - **Mandatory**: `target_include_directories()`, `target_link_libraries()`, `target_compile_definitions()`.
12
+ * **Rationale**: Encapsulates requirements and prevents property leakage to unrelated targets.
13
+
14
+ ### 2. Visibility Hygiene (`PUBLIC`, `PRIVATE`, `INTERFACE`)
15
+ * **Rule**: Always specify the scope of target properties.
16
+ * **Action**:
17
+ - `PRIVATE`: Requirement only for building the target.
18
+ - `INTERFACE`: Requirement only for consumers of the target.
19
+ - `PUBLIC`: Requirement for both.
20
+ * **Rationale**: Ensures that internal dependencies (like a private logging library) don't bleed into the usage requirements of your high-level API.
21
+
22
+ ### 3. Feature-Based C++ Standards
23
+ * **Rule**: Do not manually set `CMAKE_CXX_STANDARD` or `-std=c++XX` flags.
24
+ * **Action**: Use `target_compile_features(my_target PUBLIC cxx_std_17)`.
25
+ * **Rationale**: Allows CMake to handle compiler-specific flags and ensures the compiler actually supports the requested features.
26
+
27
+ ### 4. No Global Variable Manipulation
28
+ * **Rule**: Ban direct modification of `CMAKE_CXX_FLAGS` or `CMAKE_EXE_LINKER_FLAGS`.
29
+ * **Action**: Use `target_compile_options()` for specific compiler warnings or flags.
30
+ * **Rationale**: Global flags make it impossible to have different settings for different targets in the same project.
31
+
32
+ ### 5. Namespaced Alias Targets
33
+ * **Rule**: Always provide an alias for library targets using a namespace.
34
+ * **Action**: `add_library(MyLib::MyLib ALIAS my_lib_target)`.
35
+ * **Rationale**: Makes exported targets look consistent with external dependencies (like those found via `find_package`).
36
+
37
+ ## Examples
38
+
39
+ ### Before (Legacy Procedural CMake)
40
+ ```cmake
41
+ include_directories(${PROJECT_SOURCE_DIR}/include)
42
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall")
43
+ add_library(mylib mylib.cpp)
44
+ ```
45
+
46
+ ### After (Modern Target-Based CMake)
47
+ ```cmake
48
+ add_library(mylib mylib.cpp)
49
+
50
+ target_compile_features(mylib PUBLIC cxx_std_17)
51
+ target_compile_options(mylib PRIVATE -Wall)
52
+
53
+ target_include_directories(mylib
54
+ PUBLIC
55
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
56
+ $<INSTALL_INTERFACE:include>
57
+ )
58
+
59
+ add_library(MyProject::MyLib ALIAS mylib)
60
+ ```
@@ -0,0 +1,59 @@
1
+ # Modern CMake Examples
2
+
3
+ ### 1. Project Structure and App
4
+ ```cmake
5
+ cmake_minimum_required(VERSION 3.14)
6
+ project(MyAwesomeApp VERSION 1.0 LANGUAGES CXX)
7
+
8
+ # Use target_compile_features for the whole project standard if needed
9
+ # but target-specific is better.
10
+ add_executable(app main.cpp)
11
+
12
+ target_compile_features(app PRIVATE cxx_std_17)
13
+ target_link_libraries(app PRIVATE MyProject::Core)
14
+ ```
15
+
16
+ ### 2. Library with Internal/External Dependencies
17
+ ```cmake
18
+ add_library(core src/core.cpp)
19
+
20
+ # Internal include dir
21
+ target_include_directories(core
22
+ PUBLIC include
23
+ PRIVATE src
24
+ )
25
+
26
+ # Link against a 3rd party library found via find_package
27
+ find_package(fmt REQUIRED)
28
+ target_link_libraries(core
29
+ PUBLIC fmt::fmt
30
+ PRIVATE Threads::Threads
31
+ )
32
+
33
+ add_library(MyProject::Core ALIAS core)
34
+ ```
35
+
36
+ ### 3. Proper Header-Only Library
37
+ ```cmake
38
+ add_library(utils INTERFACE)
39
+
40
+ target_include_directories(utils
41
+ INTERFACE
42
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
43
+ $<INSTALL_INTERFACE:include>
44
+ )
45
+
46
+ # Requirement: anyone using utils MUST have C++14
47
+ target_compile_features(utils INTERFACE cxx_std_14)
48
+
49
+ add_library(MyProject::Utils ALIAS utils)
50
+ ```
51
+
52
+ ### 4. Compiler-Specific Options Safely
53
+ ```cmake
54
+ if(MSVC)
55
+ target_compile_options(core PRIVATE /W4 /WX)
56
+ else()
57
+ target_compile_options(core PRIVATE -Wall -Wextra -Werror)
58
+ endif()
59
+ ```
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "Modern CMake Best Practices",
3
+ "description": "Enforce target-based, property-oriented CMake patterns (CMake 3.0+).",
4
+ "version": "1.0.0",
5
+ "author": "Antigravity",
6
+ "tags": [
7
+ "cmake",
8
+ "cpp",
9
+ "build-system",
10
+ "devops"
11
+ ],
12
+ "applies_when": [
13
+ "creating or modifying CMakeLists.txt files",
14
+ "managing C++ project dependencies via CMake",
15
+ "defining C++ build configurations"
16
+ ],
17
+ "always_load": true
18
+ }
@@ -0,0 +1,56 @@
1
+ # C++ Safety-First Concurrency (Core Guidelines Section CP)
2
+
3
+ This skill prevents common multi-threading defects like data races, deadlocks, and shared-state corruption.
4
+
5
+ ## Policies
6
+
7
+ ### 1. RAII Locking Only
8
+ * **Rule**: Never call `mutex.lock()` or `mutex.unlock()` manually.
9
+ * **Action**:
10
+ - Use `std::lock_guard` for single mutexes.
11
+ - Use `std::unique_lock` if you need deferred locking or condition variables.
12
+ - Use `std::scoped_lock` (C++17) for multiple mutexes to avoid deadlocks.
13
+ * **Rationale**: Ensures locks are released even if an exception is thrown.
14
+
15
+ ### 2. Task-Based Parallelism
16
+ * **Rule**: Prefer tasks (`std::async`, `std::packaged_task`, `std::future`) over raw threads (`std::thread`).
17
+ * **Action**: Use `auto result = std::async(std::launch::async, func, args...);`
18
+ * **Rationale**: Automates thread management and handles value return/exception propagation naturally.
19
+
20
+ ### 3. Minimize Shared Mutable State
21
+ * **Rule**: Data should ideally be either "Thread-Local" or "Read-Only".
22
+ * **Action**:
23
+ - Pass data to threads by value where possible.
24
+ - Use `const` for data shared between threads.
25
+ - Group mutexes with the data they protect (e.g., in a struct).
26
+ * **Rationale**: If data isn't shared or isn't mutable, it cannot have a race condition.
27
+
28
+ ### 4. Never Sleep/Wait without a Condition
29
+ * **Rule**: Avoid `std::this_thread::sleep_for` for synchronization.
30
+ * **Action**: Use `std::condition_variable` with a predicate to wait for work.
31
+ * **Rationale**: Sleeping is inefficient and bug-prone; condition variables are precise and responsive.
32
+
33
+ ## Examples
34
+
35
+ ### Before (Dangerous Concurrency)
36
+ ```cpp
37
+ std::mutex mtx;
38
+ int sharedData = 0;
39
+
40
+ void worker() {
41
+ mtx.lock();
42
+ sharedData++;
43
+ mtx.unlock(); // What if an exception happened above?
44
+ }
45
+ ```
46
+
47
+ ### After (Safe Concurrency)
48
+ ```cpp
49
+ std::mutex mtx;
50
+ int sharedData = 0;
51
+
52
+ void worker() {
53
+ std::lock_guard<std::mutex> lock(mtx);
54
+ sharedData++;
55
+ } // Lock automatically released here
56
+ ```
@@ -0,0 +1,73 @@
1
+ # Concurrency Safety Examples (C++14+)
2
+
3
+ ### 1. Task-Based Parallelism (Async)
4
+ Avoids manual thread joining and handles return values safely.
5
+
6
+ ```cpp
7
+ #include <future>
8
+ #include <vector>
9
+ #include <numeric>
10
+
11
+ int computeLargeSum(const std::vector<int>& data) {
12
+ auto part1 = std::async(std::launch::async, [&data]() {
13
+ return std::accumulate(data.begin(), data.begin() + data.size()/2, 0);
14
+ });
15
+
16
+ auto part2 = std::accumulate(data.begin() + data.size()/2, data.end(), 0);
17
+
18
+ return part1.get() + part2;
19
+ }
20
+ ```
21
+
22
+ ### 2. Multi-Lock Safety (C++17 scoped_lock)
23
+ Prevents deadlocks when acquiring multiple resources.
24
+
25
+ ```cpp
26
+ #include <mutex>
27
+
28
+ struct Account {
29
+ std::mutex mtx;
30
+ double balance;
31
+ };
32
+
33
+ void transfer(Account& from, Account& to, double amount) {
34
+ // Acquire both locks simultaneously in a deadlock-free manner
35
+ std::scoped_lock lock(from.mtx, to.mtx);
36
+
37
+ if (from.balance >= amount) {
38
+ from.balance -= amount;
39
+ to.balance += amount;
40
+ }
41
+ }
42
+ ```
43
+
44
+ ### 3. Condition Variables with Predicates
45
+ Always use a predicate to protect against "spurious wakeups."
46
+
47
+ ```cpp
48
+ #include <mutex>
49
+ #include <condition_variable>
50
+ #include <queue>
51
+
52
+ std::queue<int> workQueue;
53
+ std::mutex workMtx;
54
+ std::condition_variable workCv;
55
+ bool finished = false;
56
+
57
+ void consumer() {
58
+ while (true) {
59
+ std::unique_lock<std::mutex> lock(workMtx);
60
+
61
+ // Wait until there is work OR we are finished
62
+ workCv.wait(lock, []{ return !workQueue.empty() || finished; });
63
+
64
+ if (workQueue.empty() && finished) break;
65
+
66
+ int task = workQueue.front();
67
+ workQueue.pop();
68
+ lock.unlock(); // Release lock before processing
69
+
70
+ process(task);
71
+ }
72
+ }
73
+ ```
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "C++ Safety-First Concurrency",
3
+ "description": "Enforce safe multi-threading patterns using RAII locking and task-based parallelism (C++14+).",
4
+ "version": "1.0.0",
5
+ "author": "Antigravity",
6
+ "tags": [
7
+ "cpp",
8
+ "concurrency",
9
+ "threads",
10
+ "mutex",
11
+ "raii"
12
+ ],
13
+ "applies_when": [
14
+ "working with C++ threads",
15
+ "using C++ mutexes or locking primitives",
16
+ "performing C++ asynchronous operations",
17
+ "designing multi-threaded C++ systems"
18
+ ]
19
+ }
@@ -0,0 +1,59 @@
1
+ # C++ Const-Correctness & Logic (Core Guidelines Section Con & ES)
2
+
3
+ This skill ensures that C++ code is "immutable by default," reducing side effects and enabling better compiler optimizations.
4
+
5
+ ## Policies
6
+
7
+ ### 1. By Default, Make Objects Immutable
8
+ * **Rule**: Declare all variables `const` unless they *must* be modified.
9
+ * **Action**:
10
+ - Use `const auto x = ...;` instead of `auto x = ...;`.
11
+ - Use `const` for function results that won't be modified.
12
+ * **Rationale**: Prevents accidental state mutation and improves code reasoning.
13
+
14
+ ### 2. Const Member Functions
15
+ * **Rule**: Any member function that does not modify the object's observable state must be `const`.
16
+ * **Action**: Append `const` to the function signature: `T getValue() const { ... }`.
17
+ * **Rationale**: Necessary for calling functions on `const` objects and `const&` parameters.
18
+
19
+ ### 3. Compute at Compile-Time (`constexpr`)
20
+ * **Rule**: Prefer `constexpr` for any value or simple logic that *can* be computed at compile-time.
21
+ * **Action**:
22
+ - Use `constexpr` for constants.
23
+ - Declare mathematical or configuration functions as `constexpr` (C++14 allows complex logic in `constexpr`).
24
+ * **Rationale**: Moves work from runtime to compile-time, reducing binary size and execution time.
25
+
26
+ ### 4. Consistent Parameter Logic
27
+ * **Rule**: Avoid passing by value for large types; avoid passing by non-const reference unless it's an "out" or "in-out" parameter.
28
+ * **Action**:
29
+ - **In parameters**: Pass cheap types (int, double, pointers, `string_view`) by value. Pass large types by `const&`.
30
+ - **Out/In-out parameters**: Pass by `T&`.
31
+ - **Sinks**: Pass by `T&&` (rvalue reference).
32
+
33
+ ## Examples
34
+
35
+ ### Before (Mutable/Runtime Logic)
36
+ ```cpp
37
+ int getMultiplier(int x) {
38
+ return x * 10;
39
+ }
40
+
41
+ void process() {
42
+ auto data = loadData();
43
+ auto mult = getMultiplier(5); // mutable by accident
44
+ data.apply(mult);
45
+ }
46
+ ```
47
+
48
+ ### After (Const/Compile-time Logic)
49
+ ```cpp
50
+ constexpr int getMultiplier(int x) {
51
+ return x * 10;
52
+ }
53
+
54
+ void process() {
55
+ const auto data = loadData(); // data won't change
56
+ constexpr auto mult = getMultiplier(5); // computed at compile time
57
+ data.apply(mult);
58
+ }
59
+ ```
@@ -0,0 +1,54 @@
1
+ # Const-Correctness Examples (C++14+)
2
+
3
+ ### 1. Complex Const Initialization (Lambda)
4
+ Sometimes you need to conditionally initialize a variable, but you want it to be `const` afterward.
5
+
6
+ **Modern (C++14):**
7
+ ```cpp
8
+ const auto path = []() {
9
+ if (auto env = std::getenv("APP_PATH")) {
10
+ return std::string(env);
11
+ }
12
+ return std::string("/default/path");
13
+ }(); // Immediately invoked lambda
14
+ ```
15
+
16
+ ### 2. Const-Correct Member Functions
17
+ Ensure getters and inspectors are `const`.
18
+
19
+ ```cpp
20
+ class Config {
21
+ int timeout = 30;
22
+ public:
23
+ // This is an inspector, must be const
24
+ int getTimeout() const { return timeout; }
25
+
26
+ // This is a mutator, cannot be const
27
+ void setTimeout(int t) { timeout = t; }
28
+ };
29
+ ```
30
+
31
+ ### 3. C++14 constexpr Logic
32
+ C++14 expanded `constexpr` significantly (loops, branches allowed).
33
+
34
+ ```cpp
35
+ constexpr int fibonacci(int n) {
36
+ if (n <= 1) return n;
37
+ int a = 0, b = 1;
38
+ for (int i = 2; i <= n; ++i) {
39
+ int next = a + b;
40
+ a = b;
41
+ b = next;
42
+ }
43
+ return b;
44
+ }
45
+
46
+ // Result is computed by the compiler
47
+ constexpr int fib10 = fibonacci(10);
48
+ ```
49
+
50
+ ### 4. Parameter Passing Guide
51
+ - `void f(int)` : Fast to copy (Value)
52
+ - `void f(const LargeObject&)` : Expensive to copy (Const Ref)
53
+ - `void f(LargeObject&)` : I will modify this (Ref)
54
+ - `void f(LargeObject&&)` : I will MOVE/OWN this (Rvalue Ref)
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "C++ Const-Correctness & Logic",
3
+ "description": "Enforce immutability-by-default and push logic to compile-time using constexpr (C++14+).",
4
+ "version": "1.0.0",
5
+ "author": "Antigravity",
6
+ "tags": [
7
+ "cpp",
8
+ "const",
9
+ "constexpr",
10
+ "immutability",
11
+ "optimization"
12
+ ],
13
+ "applies_when": [
14
+ "declaring C++ variables",
15
+ "defining C++ member functions",
16
+ "performing C++ compile-time calculations",
17
+ "optimizing C++ logic"
18
+ ]
19
+ }
@@ -0,0 +1,60 @@
1
+ # C++ Modern Composition (Core Guidelines Section STL, CPL, ES)
2
+
3
+ This skill forces the transition from legacy "C-style" C++ to modern, declarative, and safer C++ idioms.
4
+
5
+ ## Policies
6
+
7
+ ### 1. STL Algorithms over Manual Loops
8
+ * **Rule**: Never write a raw `for` or `while` loop for operations supported by `<algorithm>` or `<numeric>`.
9
+ * **Action**:
10
+ - Use `std::any_of`, `std::all_of`, `std::find_if`, `std::transform`, `std::accumulate`.
11
+ - In C++20 projects, prefer `std::ranges` versions.
12
+ * **Rationale**: Reduces "off-by-one" errors and improves intent readability.
13
+
14
+ ### 2. Eradicate C-style Casts and Macros
15
+ * **Rule**: Ban `(type)value` and `#define` for logic/constants.
16
+ * **Action**:
17
+ - Use `static_cast`, `reinterpret_cast`, or `const_cast`.
18
+ - Replace macros with `constexpr` variables or `inline` functions/templates.
19
+ * **Rationale**: Named casts are searchable and safer; `constexpr` respects namespaces and scopes.
20
+
21
+ ### 3. Universal Initialization
22
+ * **Rule**: Prefer `{}` initialization to avoid the "Most Vexing Parse".
23
+ * **Action**: Use `auto x = Type{args...};` or `Type x{args...};`.
24
+ * **Rationale**: Prevents narrowing conversions (e.g., `int x{3.5}` is a compiler error, while `int x = 3.5` is not).
25
+
26
+ ### 4. Zero Raw Memory Manipulation
27
+ * **Rule**: Ban `memset`, `memcpy`, and `malloc`.
28
+ * **Action**:
29
+ - Use `std::fill`, `std::copy`, or `container.assign()`.
30
+ - Use `std::vector` or `std::array` instead of raw arrays.
31
+ * **Rationale**: Standard containers are bounds-checked (via `.at()`) and manage their own memory.
32
+
33
+ ## Examples
34
+
35
+ ### Before (C-style C++)
36
+ ```cpp
37
+ #define MAX_BUFFER 1024
38
+ int arr[MAX_BUFFER];
39
+ memset(arr, 0, sizeof(arr));
40
+
41
+ bool found = false;
42
+ for (int i = 0; i < MAX_BUFFER; ++i) {
43
+ if (arr[i] == (int)someFloat) {
44
+ found = true;
45
+ break;
46
+ }
47
+ }
48
+ ```
49
+
50
+ ### After (Modern C++)
51
+ ```cpp
52
+ constexpr size_t MaxBuffer = 1024;
53
+ std::array<int, MaxBuffer> arr;
54
+ arr.fill(0);
55
+
56
+ auto target = static_cast<int>(someFloat);
57
+ bool found = std::any_of(arr.begin(), arr.end(), [target](int v) {
58
+ return v == target;
59
+ });
60
+ ```
@@ -0,0 +1,51 @@
1
+ # Modern Composition Examples (C++14+)
2
+
3
+ ### 1. Data Transformation (Transform)
4
+ **Legacy:**
5
+ ```cpp
6
+ std::vector<int> inputs = {1, 2, 3};
7
+ std::vector<int> outputs;
8
+ for (size_t i = 0; i < inputs.size(); ++i) {
9
+ outputs.push_back(inputs[i] * 2);
10
+ }
11
+ ```
12
+
13
+ **Modern (C++14):**
14
+ ```cpp
15
+ std::vector<int> inputs = {1, 2, 3};
16
+ std::vector<int> outputs;
17
+ outputs.reserve(inputs.size());
18
+ std::transform(inputs.begin(), inputs.end(), std::back_inserter(outputs),
19
+ [](int n) { return n * 2; });
20
+ ```
21
+
22
+ ### 2. Summation and Reduction (Accumulate)
23
+ **Modern (C++14):**
24
+ ```cpp
25
+ #include <numeric>
26
+
27
+ std::vector<double> prices = {10.5, 20.0, 5.25};
28
+ double total = std::accumulate(prices.begin(), prices.end(), 0.0);
29
+ ```
30
+
31
+ ### 3. Safe Narrowing Protection
32
+ **Logic:**
33
+ ```cpp
34
+ double d = 7.9;
35
+ // int i{d}; // Error: narrowing conversion from 'double' to 'int'
36
+ int i = static_cast<int>(d); // Explicit and safe
37
+ ```
38
+
39
+ ### 4. Replacing Function Macros
40
+ **Legacy:**
41
+ ```cpp
42
+ #define SQUARE(x) ((x)*(x))
43
+ ```
44
+
45
+ **Modern (C++14):**
46
+ ```cpp
47
+ template<typename T>
48
+ constexpr T square(T t) {
49
+ return t * t;
50
+ }
51
+ ```
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "C++ Modern Composition",
3
+ "description": "Replace legacy C patterns with STL, Ranges, and modern C++ abstractions (C++14+).",
4
+ "version": "1.0.0",
5
+ "author": "Antigravity",
6
+ "tags": [
7
+ "cpp",
8
+ "stl",
9
+ "ranges",
10
+ "algorithms",
11
+ "refactoring"
12
+ ],
13
+ "applies_when": [
14
+ "writing C++ logic or algorithms",
15
+ "performing C++ refactoring",
16
+ "handling C++ data transformations",
17
+ "working with C++ collections"
18
+ ],
19
+ "always_load": true
20
+ }
@@ -0,0 +1,51 @@
1
+ # C++ Robust Interfaces (Core Guidelines Section I & E)
2
+
3
+ This skill ensures that C++ interfaces follow contract-based design principles to improve safety, clarity, and maintainability.
4
+
5
+ ## Policies
6
+
7
+ ### 1. Make Interfaces Explicit
8
+ * **Rule**: Functions must explicitly state their requirements (pre-conditions) and guarantees (post-conditions).
9
+ * **Action**: Use `Expects()` and `Ensures()` (from GSL) or standardized comments if GSL is unavailable.
10
+ * **Rationale**: Detects integration bugs at the call site rather than deep in the implementation.
11
+
12
+ ### 2. Strong Typing over Primitive Types
13
+ * **Rule**: Avoid "Boolean Blindness" and `void*`.
14
+ * **Action**:
15
+ - Use `enum class` for options instead of `bool`.
16
+ - Use `std::chrono` for time instead of `int`.
17
+ - Use specific types/structs instead of a long list of primitive arguments.
18
+
19
+ ### 3. Clear Ownership and Lifetime
20
+ * **Rule**: A raw pointer (`T*`) in an interface **must never** represent ownership.
21
+ * **Action**:
22
+ - Use `gsl::not_null<T*>` for pointers that cannot be null.
23
+ - Use `std::unique_ptr` or `std::shared_ptr` ONLY if the function is participating in ownership/lifetime management.
24
+ - Prefer `T&` for arguments that must exist and aren't owned by the callee.
25
+
26
+ ### 4. Sequence Safety
27
+ * **Rule**: Never pass an array as a raw pointer + size.
28
+ * **Action**:
29
+ - Use `gsl::span<T>` (C++14/17 via GSL) or `std::span<T>` (C++20).
30
+ - Use `std::string_view` for read-only strings.
31
+
32
+ ## Examples
33
+
34
+ ### Before (Fragile Interface)
35
+ ```cpp
36
+ // What does 'flag' mean? Is data allowed to be null? Is it an array?
37
+ void processData(char* data, int size, bool flag);
38
+ ```
39
+
40
+ ### After (Robust Interface)
41
+ ```cpp
42
+ #include <gsl/gsl>
43
+
44
+ enum class ProcessingMode { Fast, Thorough };
45
+
46
+ // Explicit contract: data must not be null, size is handled by span
47
+ void processData(gsl::span<const char> data, ProcessingMode mode) {
48
+ Expects(!data.empty());
49
+ // ...
50
+ }
51
+ ```
@@ -0,0 +1,56 @@
1
+ # Robust Interface Examples (C++14+)
2
+
3
+ ### 1. Avoiding Boolean Blindness
4
+ **Incorrect:**
5
+ ```cpp
6
+ void setWindowVisible(bool visible, bool animate);
7
+ // Call site: setWindowVisible(true, false); // What is false?
8
+ ```
9
+
10
+ **Correct (C++14):**
11
+ ```cpp
12
+ enum class Visibility { Visible, Hidden };
13
+ enum class Animation { Enabled, Disabled };
14
+
15
+ void setWindowVisible(Visibility v, Animation a);
16
+ // Call site: setWindowVisible(Visibility::Visible, Animation::Disabled);
17
+ ```
18
+
19
+ ### 2. Contract-Based Validation
20
+ Using GSL (Guidelines Support Library) style:
21
+
22
+ ```cpp
23
+ #include <gsl/gsl>
24
+
25
+ class User {
26
+ public:
27
+ // Ensure name is never null or empty
28
+ void setName(gsl::not_null<const char*> name) {
29
+ Expects(std::strlen(name) > 0);
30
+ this->name = name;
31
+ }
32
+ private:
33
+ std::string name;
34
+ };
35
+ ```
36
+
37
+ ### 3. Safe Sequences with Span
38
+ Replaces pointer + length pairs which are prone to buffer overflows.
39
+
40
+ ```cpp
41
+ #include <gsl/gsl> // Use gsl::span in C++14
42
+
43
+ float calculateAverage(gsl::span<const float> values) {
44
+ Expects(!values.empty());
45
+
46
+ float sum = 0.0f;
47
+ for (float v : values) {
48
+ sum += v;
49
+ }
50
+ return sum / values.size();
51
+ }
52
+
53
+ // Usage:
54
+ // float arr[] = {1, 2, 3};
55
+ // calculateAverage(arr); // Automatic conversion to span
56
+ ```
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "C++ Robust Interfaces",
3
+ "description": "Enforce contract-based design and strong typing in C++ interfaces (C++14+).",
4
+ "version": "1.0.0",
5
+ "author": "Antigravity",
6
+ "tags": [
7
+ "cpp",
8
+ "interfaces",
9
+ "contracts",
10
+ "gsl",
11
+ "type-safety"
12
+ ],
13
+ "applies_when": [
14
+ "creating or modifying C++ header files",
15
+ "designing C++ function signatures",
16
+ "defining C++ APIs or public interfaces"
17
+ ]
18
+ }