Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
item.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 ITEM_HPP
23#define ITEM_HPP
24
25#include <cassert>
26#include <cstdint>
27// #include <iostream>
28#include <map>
29#include <memory>
30#include <string>
31#include <vector>
32
33class Category;
34
41class Item {
42
43 friend class KitParser;
44 friend class Model;
45
46public:
48 Item() : name(), checked(true), categories(), category_map(), id() {}
49
51 std::string name;
52
59 bool checked;
60
65 int32_t get_id() const {
66 return id;
67 }
68
73 const std::string get_name() const {
74 return name;
75 }
76
82 bool is_checked() const {
83 return checked;
84 }
85
89 inline friend bool operator< (Item& lhs, Item& rhs) {
90 return (lhs.name == rhs.name) ? lhs.id < rhs.id : lhs.name < rhs.name;
91 }
92
96 inline friend bool operator< (std::shared_ptr<Item> lhs, std::shared_ptr<Item> rhs) {
97 return *lhs < *rhs;
98 }
99
100protected:
101
112 Item(int32_t id,
113 std::string name,
115 }
116
118 void set_checked(bool state) {
119 checked = state;
120 }
121
123 void set_name(const std::string& name) {
124 this->name = name;
125 }
126
127private:
129 std::vector<std::weak_ptr<Category>> categories;
130
132 std::map<int32_t, std::weak_ptr<Category>> category_map;
133
135 int32_t id;
136
137};
138
139#endif // ITEM_HPP
Represents a Category having a many-to-many relationship to zero or more Item instances.
Definition category.hpp:41
void set_name(const std::string &name)
Sets the descriptive name of the Item instance.
Definition item.hpp:123
friend bool operator<(Item &lhs, Item &rhs)
Compares two Item instances.
Definition item.hpp:89
Item()
Default constructor.
Definition item.hpp:48
friend class Model
Definition item.hpp:44
std::string name
The descriptive name of the Item.
Definition item.hpp:51
int32_t get_id() const
Definition item.hpp:65
std::map< int32_t, std::weak_ptr< Category > > category_map
A map containing all the categories, keyed by id.
Definition item.hpp:132
friend class KitParser
Definition item.hpp:43
bool is_checked() const
Whether this Item is checked.
Definition item.hpp:82
Item(int32_t id, std::string name, bool checked)
Constructor taking the id and name of the Item.
Definition item.hpp:112
bool checked
Whether the item is in a checked state.
Definition item.hpp:59
int32_t id
The unique ID of the Item within the Model instance.
Definition item.hpp:135
std::vector< std::weak_ptr< Category > > categories
List of Category instances this item belongs to.
Definition item.hpp:129
const std::string get_name() const
Definition item.hpp:73
void set_checked(bool state)
Sets the checked state of the Item instance.
Definition item.hpp:118
Copyright 2008-2025 Frank Dean