Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
category.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of Kitlist, a program to maintain a simple list
4 of items and assign items to one or more categories.
5
6 Copyright (C) 2008-2025 Frank Dean
7
8 Kitlist is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Kitlist is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20
21*/
22#ifndef CATEGORY_HPP
23#define CATEGORY_HPP
24
25#include "item.hpp"
26#include <algorithm>
27#include <cassert>
28#include <cstdint>
29// #include <iostream>
30#include <map>
31#include <memory>
32#include <string>
33#include <vector>
34
41class Category {
42 friend class KitParser;
43 friend class Model;
44 friend class XmlWriter;
45
47 int32_t id;
48
49public:
51 Category() : id(), name(), items(), item_map() {
52 }
53
55 std::string name;
56
61 int32_t get_id() const {
62 return id;
63 }
64
69 const std::string get_name() const {
70 return name;
71 }
72
77 void sort_items() {
78 std::sort(items.begin(), items.end(),
79 [](std::shared_ptr<Item> a, std::shared_ptr<Item> b) {
80 return *a < *b;
81 });
82 }
83
87 inline friend bool operator< (const Category& lhs, const Category& rhs) {
88 return lhs.name == rhs.name ? lhs.id < rhs.id : lhs.name < rhs.name;
89 }
90
94 inline friend bool operator< (std::shared_ptr<Category> lhs, std::shared_ptr<Category> rhs) {
95 return *lhs < *rhs;
96 }
97
101 inline friend bool operator< (std::weak_ptr<Category> lhs, std::weak_ptr<Category> rhs) {
102 assert(!lhs.expired());
103 assert(!rhs.expired());
104 if (lhs.expired() || rhs.expired())
105 return true;
106 return *(lhs.lock()) < *(rhs.lock());
107 }
108
109protected:
110
120 Category(int32_t id,
121 std::string name) : id(id), name(name), items(), item_map() {
122 }
123
125 void set_name(const std::string& name) {
126 this->name = name;
127 }
128
129private:
131 std::vector<std::shared_ptr<Item>> items;
132
134 std::map<int32_t, std::shared_ptr<Item>> item_map;
135
136};
137
138#endif // CATEGORY_HPP
friend class Model
Definition category.hpp:43
std::map< int32_t, std::shared_ptr< Item > > item_map
A map containing all the items, keyed by id.
Definition category.hpp:134
friend class KitParser
Definition category.hpp:42
std::string name
The descriptive name of the Category.
Definition category.hpp:55
Category(int32_t id, std::string name)
Constructor taking the id and name of the Category.
Definition category.hpp:120
int32_t id
The unique ID of the Category within the Model instance.
Definition category.hpp:47
void set_name(const std::string &name)
Sets the descriptive name of the Category instance.
Definition category.hpp:125
friend bool operator<(const Category &lhs, const Category &rhs)
Compares two Category instances.
Definition category.hpp:87
const std::string get_name() const
Definition category.hpp:69
int32_t get_id() const
Definition category.hpp:61
void sort_items()
Sorts the list of items using the Item::operator< for comparison.
Definition category.hpp:77
friend class XmlWriter
Definition category.hpp:44
Category()
Default constructor.
Definition category.hpp:51
std::vector< std::shared_ptr< Item > > items
Zero or more related Item instances.
Definition category.hpp:131
Copyright 2008-2025 Frank Dean