Class: Baykit::BayServer::Util::ObjectStore
- Inherits:
-
Object
- Object
- Baykit::BayServer::Util::ObjectStore
- Includes:
- Reusable
- Defined in:
- lib/baykit/bayserver/util/object_store.rb
Direct Known Subclasses
Common::InboundShipStore, Common::WarpShipStore, Protocol::ProtocolHandlerStore
Instance Attribute Summary collapse
-
#active_list ⇒ Object
readonly
Returns the value of attribute active_list.
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#free_list ⇒ Object
readonly
Returns the value of attribute free_list.
Instance Method Summary collapse
-
#initialize(factory = nil) ⇒ ObjectStore
constructor
A new instance of ObjectStore.
- #print_usage(indent) ⇒ Object
- #rent ⇒ Object
- #reset ⇒ Object
- #Return(obj, reuse = true) ⇒ Object
Constructor Details
#initialize(factory = nil) ⇒ ObjectStore
Returns a new instance of ObjectStore.
12 13 14 15 16 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 12 def initialize(factory=nil) @free_list = [] @active_list = [] @factory = factory end |
Instance Attribute Details
#active_list ⇒ Object (readonly)
Returns the value of attribute active_list.
9 10 11 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 9 def active_list @active_list end |
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
10 11 12 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 10 def factory @factory end |
#free_list ⇒ Object (readonly)
Returns the value of attribute free_list.
8 9 10 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 8 def free_list @free_list end |
Instance Method Details
#print_usage(indent) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 62 def print_usage(indent) BayLog.info("%sfree list: %d", StringUtil.indent(indent), @free_list.length) BayLog.info("%sactive list: %d", StringUtil.indent(indent), @active_list.length) if BayLog.debug_mode? @active_list.each do |obj| BayLog.debug("%s%s", StringUtil.indent(indent+1), obj) end end end |
#rent ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 28 def rent() if @free_list.empty? if @factory.instance_of? ObjectFactory obj = @factory.create_object() else # lambda obj = @factory.call() end else obj = @free_list.delete_at(@free_list.length - 1) end if obj == nil raise Sink.new() end @active_list.append(obj) return obj end |
#reset ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 18 def reset() if @active_list.length > 0 BayLog.error("BUG?: There are %d active objects: %s", @active_list.length, @active_list) # for security @free_list.clear() @active_list.clear() end end |
#Return(obj, reuse = true) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/baykit/bayserver/util/object_store.rb', line 46 def Return(obj, reuse=true) if @free_list.include?(obj) raise Sink.new("This object already returned: " + obj.to_s) end if !@active_list.include?(obj) raise Sink.new("This object is not active: " + obj) end @active_list.delete(obj) if reuse @free_list.append(obj) obj.reset() end end |