Browse Source

Named arguments in callback types for readability.

David Piuva 2 years ago
parent
commit
c3801553c0
3 changed files with 25 additions and 24 deletions
  1. 1 1
      Source/DFPSR/api/stringAPI.cpp
  2. 4 4
      Source/DFPSR/api/stringAPI.h
  3. 20 19
      Source/DFPSR/gui/InputEvent.h

+ 1 - 1
Source/DFPSR/api/stringAPI.cpp

@@ -975,7 +975,7 @@ void dsr::string_unassignMessageHandler() {
 	globalMessageAction = defaultMessageAction;
 }
 
-void dsr::string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace) {
+void dsr::string_split_callback(std::function<void(ReadableString separatedText)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace) {
 	int64_t sectionStart = 0;
 	for (int64_t i = 0; i < source.length; i++) {
 		DsrChar c = source[i];

+ 4 - 4
Source/DFPSR/api/stringAPI.h

@@ -212,7 +212,7 @@ ReadableString string_after(const ReadableString& source, int64_t exclusiveStart
 // Split source into a list of strings.
 // Post-condition:
 //   Returns a list of strings from source by splitting along separator.
-//   If removeWhiteSpace is true then surrounding white-space will be removed, otherwise white-space is kept.
+//   If removeWhiteSpace is true then surrounding white-space will be removed, otherwise all white-space is kept.
 // The separating characters are excluded from the resulting strings.
 // The number of strings returned in the list will equal the number of separating characters plus one, so the result may contain empty strings.
 // Each string in the list clones content to its own dynamic buffer. Use string_split_callback if you don't need long term storage.
@@ -220,10 +220,10 @@ List<String> string_split(const ReadableString& source, DsrChar separator, bool
 // Split a string without needing a list to store the result.
 // Use string_splitCount on the same source and separator if you need to know the element count in advance.
 // Side-effects:
-//   Calls action for each sub-string divided by separator in source.
-void string_split_callback(std::function<void(ReadableString)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
+//   Calls action for each sub-string divided by separator in source given as the separatedText argument.
+void string_split_callback(std::function<void(ReadableString separatedText)> action, const ReadableString& source, DsrChar separator, bool removeWhiteSpace = false);
 // An alternative overload for having a very long lambda at the end.
-inline void string_split_callback(const ReadableString& source, DsrChar separator, bool removeWhiteSpace, std::function<void(ReadableString)> action) {
+inline void string_split_callback(const ReadableString& source, DsrChar separator, bool removeWhiteSpace, std::function<void(ReadableString separatedText)> action) {
 	string_split_callback(action, source, separator, removeWhiteSpace);
 }
 // Split source using separator, only to return the number of splits.

+ 20 - 19
Source/DFPSR/gui/InputEvent.h

@@ -1,6 +1,6 @@
 // zlib open source license
 //
-// Copyright (c) 2018 to 2022 David Forsgren Piuva
+// Copyright (c) 2018 to 2023 David Forsgren Piuva
 // 
 // This software is provided 'as-is', without any express or implied
 // warranty. In no event will the authors be held liable for any damages
@@ -62,11 +62,11 @@ String& string_toStreamIndented(String& target, const DsrKey& source, const Read
 
 class KeyboardEvent : public InputEvent {
 public:
-	// What the user did to the key
+	// What the user did to the key.
 	KeyboardEventType keyboardEventType;
-	// The raw unicode value without any encoding
+	// The raw unicode value without any encoding.
 	DsrChar character;
-	// Minimal set of keys for portability
+	// Minimal set of keys for portability.
 	DsrKey dsrKey;
 	KeyboardEvent(KeyboardEventType keyboardEventType, DsrChar character, DsrKey dsrKey)
 	 : keyboardEventType(keyboardEventType), character(character), dsrKey(dsrKey) {}
@@ -112,26 +112,27 @@ public:
 	: windowEventType(windowEventType), width(width), height(height) {}
 };
 
-// A macro for declaring a virtual callback from the base method
-//   Use the getter for registering methods so that they can be forwarded to a wrapper without inheritance
-//   Use the actual variable beginning with `callback_` when calling the method from inside
+// A macro for declaring a virtual callback from the base method.
+//   Use the getter for registering methods so that they can be forwarded to a wrapper without inheritance.
+//   Use the actual variable beginning with `callback_` when calling the method from inside.
 #define DECLARE_CALLBACK(NAME, LAMBDA) \
 	decltype(LAMBDA) callback_##NAME = LAMBDA; \
 	decltype(LAMBDA)& NAME() { return callback_##NAME; }
 
-// The callback templates and types
-static std::function<void()> emptyCallback = []() {};
-using EmptyCallback = decltype(emptyCallback) ;
-static std::function<void(int)> indexCallback = [](int64_t index) {};
-using IndexCallback = decltype(indexCallback);
-static std::function<void(int, int)> sizeCallback = [](int width, int height) {};
-using SizeCallback = decltype(sizeCallback);
-static std::function<void(const KeyboardEvent&)> keyboardCallback = [](const KeyboardEvent& event) {};
-using KeyboardCallback = decltype(keyboardCallback);
-static std::function<void(const MouseEvent&)> mouseCallback = [](const MouseEvent& event) {};
-using MouseCallback = decltype(mouseCallback);
+// The callback types.
+using EmptyCallback = std::function<void()>;
+using IndexCallback = std::function<void(int index)>;
+using SizeCallback = std::function<void(int width, int height)>;
+using KeyboardCallback = std::function<void(const KeyboardEvent& event)>;
+using MouseCallback = std::function<void(const MouseEvent& event)>;
+
+// The default functions to call until a callback has been selected.
+static EmptyCallback emptyCallback = []() {};
+static IndexCallback indexCallback = [](int64_t index) {};
+static SizeCallback sizeCallback = [](int width, int height) {};
+static KeyboardCallback keyboardCallback = [](const KeyboardEvent& event) {};
+static MouseCallback mouseCallback = [](const MouseEvent& event) {};
 
 }
 
 #endif
-