From c7658b691c4b071fb54512a4f195eea8ba604236 Mon Sep 17 00:00:00 2001 From: Alexander Kromm Date: Wed, 13 May 2020 18:48:03 +0700 Subject: [PATCH] add service interface --- wadsrc/static/zscript.txt | 2 + wadsrc/static/zscript/service.zs | 99 ++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 wadsrc/static/zscript/service.zs diff --git a/wadsrc/static/zscript.txt b/wadsrc/static/zscript.txt index b6257f1c9c4..f04c8e11946 100644 --- a/wadsrc/static/zscript.txt +++ b/wadsrc/static/zscript.txt @@ -37,6 +37,8 @@ version "4.5" #include "zscript/destructible.zs" #include "zscript/level_postprocessor.zs" #include "zscript/level_compatibility.zs" +#include "zscript/dictionary.zs" +#include "zscript/service.zs" #include "zscript/actors/actor.zs" #include "zscript/actors/checks.zs" diff --git a/wadsrc/static/zscript/service.zs b/wadsrc/static/zscript/service.zs new file mode 100644 index 00000000000..b5da3925011 --- /dev/null +++ b/wadsrc/static/zscript/service.zs @@ -0,0 +1,99 @@ +/** + * This is Service interface. + */ +class Service abstract +{ + virtual String Get(String request) + { + return ""; + } +} + +/** + * Use this class to find and iterate over services. + * + * Example usage: + * + * @code + * ServiceIterator i = ServiceIterator.Find("MyService"); + * + * if (!i.ServiceExists()) { return; } + * + * Service s; + * while (s = i.Next()) + * { + * String request = ... + * String answer = s.Get(request); + * ... + * } + * @endcode + * + * ServiceExists() call is optional and is provided for convenience. + * + * If no services are found, the all calls to Next() will return NULL. + */ +class ServiceIterator +{ + /** + * @param serviceType class name of service to find. + */ + static ServiceIterator Find(String serviceType) + { + let result = new("ServiceIterator"); + + result.mType = serviceType; + + if (result.ServiceExists()) + { + result.mClassIndex = 0; + result.FindNextService(); + } + else + { + // Class doesn't exist, don't even try to find it. + result.mClassIndex = AllClasses.Size(); + } + + return result; + } + + /** + * @returns true if the requested service exists, false otherwise. + */ + bool ServiceExists() + { + return (mType != NULL); + } + + /** + * Gets the service and advances the iterator. + * + * @returns service instance, or NULL if no more servers found. + * + * @note Each ServiceIterator will return new instances of services. + */ + Service Next() + { + uint classesNumber = AllClasses.Size(); + Service result = (mClassIndex == classesNumber) + ? NULL + : Service(new(AllClasses[mClassIndex])); + + ++mClassIndex; + findNextService(); + + return result; + } + + private void FindNextService() + { + uint classesNumber = AllClasses.size(); + while (mClassIndex < classesNumber && !(AllClasses[mClassIndex] is mType)) + { + ++mClassIndex; + } + } + + private class mType; + private uint mClassIndex; +}