allow partial match for finding services by name

This commit is contained in:
Alexander Kromm 2020-11-04 02:27:58 +07:00 committed by Christoph Oelckers
parent 988f83a73f
commit 120c8b77d1

View file

@ -22,8 +22,6 @@ class Service abstract
* @code * @code
* ServiceIterator i = ServiceIterator.Find("MyService"); * ServiceIterator i = ServiceIterator.Find("MyService");
* *
* if (!i.ServiceExists()) { return; }
*
* Service s; * Service s;
* while (s = i.Next()) * while (s = i.Next())
* { * {
@ -33,43 +31,29 @@ class Service abstract
* } * }
* @endcode * @endcode
* *
* ServiceExists() call is optional and is provided for convenience.
*
* If no services are found, the all calls to Next() will return NULL. * If no services are found, the all calls to Next() will return NULL.
*/ */
class ServiceIterator class ServiceIterator
{ {
/** /**
* @param serviceType class name of service to find. * Creates a Service iterator for a service name. It will iterate over all existing Services
* with names that match @a serviceName or have it as a part of their names.
*
* Matching is case-independent.
*
* @param serviceName class name of service to find.
*/ */
static ServiceIterator Find(String serviceType) static ServiceIterator Find(String serviceName)
{ {
let result = new("ServiceIterator"); let result = new("ServiceIterator");
result.mType = serviceType; result.mServiceName = serviceName;
if (result.ServiceExists())
{
result.mClassIndex = 0; result.mClassIndex = 0;
result.FindNextService(); result.FindNextService();
}
else
{
// Class doesn't exist, don't even try to find it.
result.mClassIndex = AllClasses.Size();
}
return result; return result;
} }
/**
* @returns true if the requested service exists, false otherwise.
*/
bool ServiceExists()
{
return (mType != NULL);
}
/** /**
* Gets the service and advances the iterator. * Gets the service and advances the iterator.
* *
@ -85,7 +69,7 @@ class ServiceIterator
: Service(new(AllClasses[mClassIndex])); : Service(new(AllClasses[mClassIndex]));
++mClassIndex; ++mClassIndex;
findNextService(); FindNextService();
return result; return result;
} }
@ -93,12 +77,23 @@ class ServiceIterator
private void FindNextService() private void FindNextService()
{ {
uint classesNumber = AllClasses.size(); uint classesNumber = AllClasses.size();
while (mClassIndex < classesNumber && !(AllClasses[mClassIndex] is mType)) while (mClassIndex < classesNumber && !ServiceNameContains(AllClasses[mClassIndex], mServiceName))
{ {
++mClassIndex; ++mClassIndex;
} }
} }
private class<Service> mType; private static bool ServiceNameContains(class aClass, String substring)
{
if (!(aClass is "Service")) return false;
String className = aClass.GetClassName();
String lowerClassName = className.MakeLower();
String lowerSubstring = substring.MakeLower();
bool result = lowerClassName.IndexOf(lowerSubstring) != -1;
return result;
}
private String mServiceName;
private uint mClassIndex; private uint mClassIndex;
} }