aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorGravatar Sadie Powell2026-03-29 13:34:14 +0100
committerGravatar Sadie Powell2026-03-29 14:12:41 +0100
commit5095617d6c53081521efb673823946cbfc183753 (patch)
treebd444f9c6b1a317500fc01902e6a1953a729bea9 /include
parentGet rid of thismod/Parent in the mysql module. (diff)
Avoid the direct use of intptr_t wherever possible.
This may cause problems on systems like CheriBSD where the pointer type stores extra data.
Diffstat (limited to 'include')
-rw-r--r--include/extension.h51
-rw-r--r--include/modules/cap.h10
-rw-r--r--include/modules/ircv3_batch.h4
3 files changed, 56 insertions, 9 deletions
diff --git a/include/extension.h b/include/extension.h
index 18f3678f2..1a1a4f85a 100644
--- a/include/extension.h
+++ b/include/extension.h
@@ -404,7 +404,7 @@ public:
}
};
-/** An extension which has an integer value. */
+/** An extension which has a raw integer value. */
class CoreExport IntExtItem
: public ExtensionItem
{
@@ -412,15 +412,15 @@ protected:
/** Whether to sync this extension across the network. */
bool synced;
-public:
/** Initializes an instance of the IntExtItem class.
* @param owner The module which created the extension.
* @param key The name of the extension (e.g. foo-bar).
* @param exttype The type of extensible that the extension applies to.
* @param sync Whether this extension should be broadcast to other servers.
*/
- IntExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false);
+ IntExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync);
+public:
/** Retrieves the value for this extension of the specified container.
* @param container The container that this extension is set on.
* @return Either the value of this extension or 0 if it is not set.
@@ -453,6 +453,51 @@ public:
void Unset(Extensible* container, bool sync = true);
};
+/** An extension which has a typed integer value. */
+template <typename Value>
+class NumExtItem
+ : public IntExtItem
+{
+public:
+ /** The numeric type for this extension. */
+ using Numeric = std::conditional_t<std::is_enum_v<Value>, std::underlying_type<Value>, std::type_identity<Value>>::type;
+
+ /** Initializes an instance of the NumExtItem class.
+ * @param owner The module which created the extension.
+ * @param key The name of the extension (e.g. foo-bar).
+ * @param exttype The type of extensible that the extension applies to.
+ * @param sync Whether this extension should be broadcast to other servers.
+ */
+ NumExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false)
+ : IntExtItem(owner, key, exttype, sync)
+ {
+ }
+
+ /** @copydoc ExtensionItem::FromInternal */
+ void FromInternal(Extensible* container, const std::string& value) noexcept override
+ {
+ Set(container, ConvToNum<Numeric>(value), false);
+ }
+
+ /** @copydoc IntExtItem::Get */
+ Numeric Get(const Extensible* container) const
+ {
+ return static_cast<Numeric>(IntExtItem::Get(container));
+ }
+
+ /** @copydoc IntExtItem::Set */
+ void Set(Extensible* container, Numeric value, bool sync = true)
+ {
+ IntExtItem::Set(container, static_cast<intptr_t>(value), sync);
+ }
+
+ /** @copydoc ExtensionItem::ToInternal */
+ std::string ToInternal(const Extensible* container, const ExtensionPtr& item) const noexcept override
+ {
+ return ConvToStr(static_cast<Numeric>(reinterpret_cast<intptr_t>(item.get())));
+ }
+};
+
/** An extension which has a string value. */
class CoreExport StringExtItem
: public SimpleExtItem<std::string>
diff --git a/include/modules/cap.h b/include/modules/cap.h
index f349ef1e1..95ba99bbf 100644
--- a/include/modules/cap.h
+++ b/include/modules/cap.h
@@ -24,12 +24,14 @@
namespace Cap
{
- static constexpr unsigned int MAX_CAPS = (sizeof(intptr_t) * 8) - 1;
- static constexpr intptr_t CAP_302_BIT = (intptr_t)1 << MAX_CAPS;
+ using Ext = size_t;
+
+ static constexpr unsigned int MAX_CAPS = (sizeof(Ext) * 8) - 1;
+ static constexpr Ext CAP_302_BIT = (Ext)1 << MAX_CAPS;
static constexpr unsigned int MAX_VALUE_LENGTH = 100;
- using Ext = intptr_t;
- class ExtItem : public IntExtItem
+ class ExtItem final
+ : public NumExtItem<Ext>
{
public:
ExtItem(const WeakModulePtr& mod);
diff --git a/include/modules/ircv3_batch.h b/include/modules/ircv3_batch.h
index 6c5589346..b97a18241 100644
--- a/include/modules/ircv3_batch.h
+++ b/include/modules/ircv3_batch.h
@@ -34,7 +34,7 @@ namespace IRCv3
class API;
class CapReference;
- static constexpr unsigned int MAX_BATCHES = (sizeof(intptr_t) * 8) - 1;
+ static constexpr unsigned int MAX_BATCHES = (sizeof(size_t) * 8) - 1;
}
}
@@ -101,7 +101,7 @@ class IRCv3::Batch::Batch final
}
unsigned int GetId() const { return bit; }
- intptr_t GetBit() const { return reftag; }
+ size_t GetBit() const { return reftag; }
public:
/** Constructor.