File indexing completed on 2024-05-05 04:38:10

0001 /*
0002     SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef ITEMREPOSITORYEXAMPLEITEM_H
0008 #define ITEMREPOSITORYEXAMPLEITEM_H
0009 
0010 #include "itemrepository.h"
0011 
0012 namespace KDevelop {
0013 /**
0014  * This is the actual data that is stored in the repository. All the data that is not directly in the class-body,
0015  * like the text of a string, can be stored behind the item in the same memory region. The only important thing is
0016  * that the Request item (@see ExampleItemRequest) correctly advertises the space needed by this item.
0017  */
0018 class ExampleItem
0019 {
0020     /// @returns The item's hash.
0021     /// @warning The hash returned shall be exactly same as the return value of @ref ExampleItemRequest::hash()
0022     ///          of the item request used to create this item.
0023     unsigned int hash() const
0024     {
0025         return 0;
0026     }
0027 
0028     //Every item has to implement this function, and return the complete size this item takes in memory.
0029     //Must be exactly the same value as ExampleItemRequest::itemSize() has returned while creating the item.
0030     /// @returns The item's size.
0031     /// @warning The size returned shall be exactly same as the return value @ref ExampleItemRequest::itemSize()
0032     ///          of the item request used to created this item.
0033     unsigned short int itemSize() const
0034     {
0035         return 0;
0036     }
0037 };
0038 
0039 /**
0040  * A request represents the information that is searched in the repository.
0041  * It must be able to compare itself to items stored in the repository, and it must be able to
0042  * create items in the. The item-types can also be basic data-types, with additional information stored behind.
0043  * It must have a static destroy() member, that does any action that needs to be done before the item is removed from
0044  * the repository again.
0045  */
0046 class ExampleItemRequest
0047 {
0048     enum {
0049         AverageSize = 10 //! This should be the approximate average size of an Item requested.
0050     };
0051 
0052     using HashType = unsigned int;
0053 
0054     /// @returns The hash associated with this request (e. g. the hash of a string).
0055     HashType hash() const
0056     {
0057         return 0;
0058     }
0059 
0060     /// @returns The size of an item created with @ref createItem().
0061     uint itemSize() const
0062     {
0063         return 0;
0064     }
0065 
0066     /// Should create an item where the information of the requested item is permanently stored.
0067     /// @param item A pointer an allocated range with the size of @ref itemSize().
0068     /// @warning    Never call non-constant functions on the repository from within this function!
0069     void createItem(ExampleItem* item) const
0070     {
0071         Q_UNUSED(item);
0072     }
0073 
0074     static void destroy(ExampleItem* item, AbstractItemRepository&)
0075     {
0076         Q_UNUSED(item);
0077     }
0078 
0079     /// @returns Whether this item should be disk-persistent.
0080     static bool persistent(ExampleItem*)
0081     {
0082         return true;
0083     }
0084 
0085     /// @returns Whether the requested item equals the given one (@p item).
0086     bool equals(const ExampleItem* item) const
0087     {
0088         Q_UNUSED(item);
0089         return false;
0090     }
0091 };
0092 }
0093 
0094 #endif // ITEMREPOSITORYEXAMPLEITEM_H