From bd77f0c596d87e82ea73410e02d5d95ec50b0180 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pierre=20Pe=CC=81bay?= <pebay.pierre@gmail.com>
Date: Tue, 25 Apr 2023 13:45:58 -0400
Subject: [PATCH] #3: Add doxygen and key checking when detaching from set

---
 block.cc | 15 +++++++--------
 block.h  | 28 +++++++++++++++++++++++++---
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/block.cc b/block.cc
index a450b99cdd..7871c92abd 100644
--- a/block.cc
+++ b/block.cc
@@ -13,20 +13,19 @@ Block::Block(uint64_t b_id,
 {}
 
 uint64_t Block::detach_object_id(uint64_t o_id) {
-  try {
+  if(auto search = this->attached_object_ids.find(o_id);
+    search != this->attached_object_ids.end()) {
     this->attached_object_ids.erase(o_id);
   }
-  catch(const nb::type_error e) {
-    std::cerr << e.what() << '\n';
+  else {
+    throw nb::type_error(
+      "object id " + std::to_string(o_id) +
+      " is not attached to block " + std::to_string(this->get_id())
+    );
   }
   return this->attached_object_ids.size();
 }
 
-void Block::attach_object_id(uint64_t o_id) {
-  this->attached_object_ids.insert(o_id);
-}
-
-
 std::string Block::to_string() const {
   std::string out;
   out += "Block id: " + std::to_string(this->get_id())
diff --git a/block.h b/block.h
index 931980d6e6..b8661d8036 100644
--- a/block.h
+++ b/block.h
@@ -12,7 +12,6 @@
 #include <iostream>
 #include <list>
 #include <map>
-#include <format>
 #include <unordered_set>
 
 namespace nb = nanobind;
@@ -29,14 +28,37 @@ struct Block
   uint64_t home_id;
   double size = 0;
   std::unordered_set<uint64_t> attached_object_ids;
+
+  /**
+   * Return block object ids.
+   */
+  std::unordered_set<uint64_t> get_attached_object_ids() const { return this->attached_object_ids; };
 public:
+  /**
+   * Return block ID.
+   */
   uint64_t get_id() const { return this->index; };
+
+  /**
+   * Return block home ID.
+   */
   uint64_t get_home_id() const { return this->home_id; };
+
+  /**
+   * Return block size.
+   */
   double get_size() const { return this->size; };
-  std::unordered_set<uint64_t> get_attached_object_ids() const { return this->attached_object_ids; };
 
+  
+  /**
+   * Try to detach object ID from block and return length.
+   */
   uint64_t detach_object_id(uint64_t);
-  void attach_object_id(uint64_t);
+
+  /**
+   * Attach object ID to block.
+   */
+  void attach_object_id(uint64_t o_id) { this->attached_object_ids.insert(o_id); };
 
   std::string to_string() const;