2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "ValueHandlerRoster.h"
11 #include <AutoDeleter.h>
12 #include <AutoLocker.h>
14 #include "AddressValueHandler.h"
15 #include "BoolValueHandler.h"
16 #include "EnumerationValueHandler.h"
17 #include "FloatValueHandler.h"
18 #include "StringValueHandler.h"
22 /*static*/ ValueHandlerRoster
* ValueHandlerRoster::sDefaultInstance
= NULL
;
25 ValueHandlerRoster::ValueHandlerRoster()
27 fLock("value handler roster")
32 ValueHandlerRoster::~ValueHandlerRoster()
36 /*static*/ ValueHandlerRoster
*
37 ValueHandlerRoster::Default()
39 return sDefaultInstance
;
44 ValueHandlerRoster::CreateDefault()
46 if (sDefaultInstance
!= NULL
)
49 ValueHandlerRoster
* roster
= new(std::nothrow
) ValueHandlerRoster
;
52 ObjectDeleter
<ValueHandlerRoster
> rosterDeleter(roster
);
54 status_t error
= roster
->Init();
58 error
= roster
->RegisterDefaultHandlers();
62 sDefaultInstance
= rosterDeleter
.Detach();
68 ValueHandlerRoster::DeleteDefault()
70 ValueHandlerRoster
* roster
= sDefaultInstance
;
71 sDefaultInstance
= NULL
;
77 ValueHandlerRoster::Init()
79 return fLock
.InitCheck();
84 ValueHandlerRoster::RegisterDefaultHandlers()
88 #undef REGISTER_HANDLER
89 #define REGISTER_HANDLER(name) \
91 name##ValueHandler* handler \
92 = new(std::nothrow) name##ValueHandler; \
93 if (handler == NULL) \
95 BReference<name##ValueHandler> handlerReference(handler, true); \
97 error = handler->Init(); \
101 if (!RegisterHandler(handler)) \
102 return B_NO_MEMORY; \
105 REGISTER_HANDLER(Address
)
106 REGISTER_HANDLER(Bool
)
107 REGISTER_HANDLER(Enumeration
)
108 REGISTER_HANDLER(Float
)
109 REGISTER_HANDLER(Integer
)
110 REGISTER_HANDLER(String
)
117 ValueHandlerRoster::FindValueHandler(Value
* value
, ValueHandler
*& _handler
)
119 // find the best-supporting handler
120 AutoLocker
<BLocker
> locker(fLock
);
122 ValueHandler
* bestHandler
= NULL
;
123 float bestSupport
= 0;
125 for (int32 i
= 0; ValueHandler
* handler
= fValueHandlers
.ItemAt(i
); i
++) {
126 float support
= handler
->SupportsValue(value
);
127 if (support
> 0 && support
> bestSupport
) {
128 bestHandler
= handler
;
129 bestSupport
= support
;
133 if (bestHandler
== NULL
)
134 return B_ENTRY_NOT_FOUND
;
136 bestHandler
->AcquireReference();
137 _handler
= bestHandler
;
143 ValueHandlerRoster::GetValueFormatter(Value
* value
,
144 ValueFormatter
*& _formatter
)
146 // get the best supporting value handler
147 ValueHandler
* handler
;
148 status_t error
= FindValueHandler(value
, handler
);
151 BReference
<ValueHandler
> handlerReference(handler
, true);
153 // create the formatter
154 return handler
->GetValueFormatter(value
, _formatter
);
159 ValueHandlerRoster::GetTableCellValueRenderer(Value
* value
,
160 TableCellValueRenderer
*& _renderer
)
162 // get the best supporting value handler
163 ValueHandler
* handler
;
164 status_t error
= FindValueHandler(value
, handler
);
167 BReference
<ValueHandler
> handlerReference(handler
, true);
169 // create the renderer
170 return handler
->GetTableCellValueRenderer(value
, _renderer
);
175 ValueHandlerRoster::RegisterHandler(ValueHandler
* handler
)
177 if (!fValueHandlers
.AddItem(handler
))
180 handler
->AcquireReference();
186 ValueHandlerRoster::UnregisterHandler(ValueHandler
* handler
)
188 if (fValueHandlers
.RemoveItem(handler
))
189 handler
->ReleaseReference();