Kstars

starblockfactory.h
1 /*
2  SPDX-FileCopyrightText: 2008 Akarsh Simha <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #pragma once
8 
9 #include "typedef.h"
10 
11 class StarBlock;
12 
13 /**
14  * @class StarBlockFactory
15  *
16  * @short A factory that creates StarBlocks and recycles them in an LRU Cache
17  * @author Akarsh Simha
18  * @version 0.1
19  */
20 
22 {
23  public:
24  static StarBlockFactory *Instance();
25 
26  /**
27  * Destructor
28  * Deletes the linked list that maintains the Cache, sets the pointer to nullptr
29  */
31 
32  /**
33  * @short Return a StarBlock available for use
34  *
35  * This method first checks if there are any cached StarBlocks that are not in use.
36  * If such a StarBlock is found, it returns the same for use. Else it freshly allocates
37  * a StarBlock and returns the same. It also moves the StarBlock to the front, marking it
38  * as the most recently used. If the StarBlock had a parent StarBlockList, this method
39  * detaches the StarBlock from the StarBlockList
40  *
41  * @return A StarBlock that is available for use
42  */
43  std::shared_ptr<StarBlock> getBlock();
44 
45  /**
46  * @short Mark a StarBlock as most recently used and sync its drawID with the current drawID
47  *
48  * @return true on success, false if the StarBlock supplied was not on our list at all
49  */
50  bool markFirst(std::shared_ptr<StarBlock>& block);
51 
52  /**
53  * @short Rank a given StarBlock after another given StarBlock in the LRU list
54  * and sync its drawID with the current drawID
55  *
56  * @param after The block after which 'block' should be put
57  * @param block The block to mark for use
58  * @return true on success, false on failure
59  */
60  bool markNext(std::shared_ptr<StarBlock>& after, std::shared_ptr<StarBlock>& block);
61 
62  /**
63  * @short Returns the number of StarBlocks currently produced
64  *
65  * @return Number of StarBlocks currently allocated
66  */
67  inline int getBlockCount() const { return nBlocks; }
68 
69  /**
70  * @short Frees all StarBlocks that are in the cache
71  * @return The number of StarBlocks freed
72  */
73  inline int freeAll() { return deleteBlocks(nBlocks); }
74 
75  /**
76  * @short Frees all StarBlocks that are not used in this draw cycle
77  * @return The number of StarBlocks freed
78  */
79  int freeUnused();
80 
81  /**
82  * @short Prints the structure of the cache, for debugging
83  */
84  void printStructure() const;
85 
86  quint32 drawID; // A number identifying the current draw cycle
87 
88  private:
89  /**
90  * Constructor
91  * Initializes first and last StarBlock pointers to nullptr
92  */
94 
95  /**
96  * @short Deletes the N least recently used blocks
97  *
98  * @param nblocks Number of blocks to delete
99  * @return Number of blocks successfully deleted
100  */
101  int deleteBlocks(int nblocks);
102 
103  std::shared_ptr<StarBlock> first, last; // Pointers to the beginning and end of the linked list
104  int nBlocks; // Number of blocks we currently have in the cache
105  int nCache; // Number of blocks to start recycling cached blocks at
106 
107  static StarBlockFactory *pInstance;
108 };
int freeAll()
Frees all StarBlocks that are in the cache.
bool markFirst(std::shared_ptr< StarBlock > &block)
Mark a StarBlock as most recently used and sync its drawID with the current drawID.
std::shared_ptr< StarBlock > getBlock()
Return a StarBlock available for use.
void printStructure() const
Prints the structure of the cache, for debugging.
bool markNext(std::shared_ptr< StarBlock > &after, std::shared_ptr< StarBlock > &block)
Rank a given StarBlock after another given StarBlock in the LRU list and sync its drawID with the cur...
int freeUnused()
Frees all StarBlocks that are not used in this draw cycle.
~StarBlockFactory()
Destructor Deletes the linked list that maintains the Cache, sets the pointer to nullptr.
int getBlockCount() const
Returns the number of StarBlocks currently produced.
A factory that creates StarBlocks and recycles them in an LRU Cache.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.