STD

Various utilities such as mutex, thread, ...

  • Buffer — Binary buffer with cursor-based reading and writing
  • DynamicLibrary — Cross-platform shared library loader
  • List — Generic doubly-linked list
  • Mutex — Mutual exclusion primitive for thread synchronization
  • String — UTF-8 string with value semantics and Kotlin-style API
  • Thread — Lightweight thread creation and joining
  • Time — Cross-platform time utilities
#include <uhcstd.uhh>
#define UHC_LITTLE_ENDIAN 0
#define UHC_BIG_ENDIAN 1

namespace Mutex {
  #if defined(_WIN32) || defined(_WIN64)
    self {
      CRITICAL_SECTION handle
    }
  #else
    self {
      pthread_mutex_t handle
    }
  #endif
  Mutex create()
  U0 destroy(Mutex mutex)
  U0 lock(Mutex mutex)
  U0 unlock(Mutex mutex)
}

namespace Buffer {
  self {
      U0* handle
      U8 order
      U32 cursor
  }
  U0 setOrder(Buffer buffer, U8 order)
  U0 reset(Buffer buffer)
  I8 getI8(Buffer buffer)
  U8 getU8(Buffer buffer)
  I16 getI16(Buffer buffer)
  U16 getU16(Buffer buffer)
  I32 getI32(Buffer buffer)
  U32 getU32(Buffer buffer)
  I64 getI64(Buffer buffer)
  U64 getU64(Buffer buffer)
  F32 getF32(Buffer buffer)
  F64 getF64(Buffer buffer)
  U0 putI8(Buffer buffer, I8 value)
  U0 putU8(Buffer buffer, U8 value)
  U0 putI16(Buffer buffer, I16 value)
  U0 putU16(Buffer buffer, U16 value)
  U0 putI32(Buffer buffer, I32 value)
  U0 putU32(Buffer buffer, U32 value)
  U0 putI64(Buffer buffer, I64 value)
  U0 putU64(Buffer buffer, U64 value)
  U0 putF32(Buffer buffer, F32 value)
  U0 putF64(Buffer buffer, F64 value)
}

namespace Thread {
  typedef U0* (*Fn)(U0*)
  #if defined(_WIN32) || defined(_WIN64)
    self {
      HANDLE handle
    }
  #else
    self {
      pthread_t handle
    }
  #endif
  Thread start(Thread.Fn method, U0* arg)
  U0 join(Thread thread)
}

namespace List {
  self;
  U0 add(List<T> list, T value);
  U0 addAt(List<T> list, T value, I32 index);
  U8 remove(List<T> list, T value);
  U0 removeAt(List<T> list, I32 index);
  T& get(List<T> list, I32 index);
  U0 set(List<T> list, I32 index, T value);
  T& first(List<T> list);
  T& last(List<T> list);
  U64 size(List<T> list);
  U0 clear(List<T> list);
}

namespace HashMap {
  self;
  U0 put(HashMap<K, V> map, K key, V value);
  V& get(HashMap<K, V> map, K key);
  V getOrDefault(HashMap<K, V> map, K key, V defaultValue);
  U8 remove(HashMap<K, V> map, K key);
  U8 containsKey(HashMap<K, V> map, K key);
  U8 containsValue(HashMap<K, V> map, V value);
  U64 size(HashMap<K, V> map);
  U8 isEmpty(HashMap<K, V> map);
  U0 clear(HashMap<K, V> map);
  List<K> keys(HashMap<K, V> map);
  List<V> values(HashMap<K, V> map);
  U0 putAll(HashMap<K, V> map, HashMap<K, V> other);
  U8 putIfAbsent(HashMap<K, V> map, K key, V value);
  U0 forEach(HashMap<K, V> map, lambda block(K, V) -> U0);
}

namespace String {
  self {
      std::string handle;
      It();
      It(const I8* s) : handle(s ? s : "") {}
      It(const std::string& s) : handle(s) {}
      It operator+(const It& o) { return It(handle + o.handle); }
      It operator+(const I8* s) { return It(handle + (s ? s : "")); }
      friend It operator+(const I8* s, const It& o) { return It(std::string(s ? s : "") + o.handle); }
      U8 operator{ return handle; }
      U8 operator!{ return handle !; }
  }
  String from(const I8* s)
  String fromI32(I32 n)
  String fromI64(I64 n)
  String fromF32(F32 f)
  String fromF64(F64 f)
  const I8* c(const String s)
  I8* strdup(const String s)
  U64 length(const String s)
  U8 isEmpty(const String s)
  I8 charAt(const String s, I32 i)
  U8 equals(const String a, const String b)
  U8 contains(const String s, const String sub)
  U8 startsWith(const String s, const String prefix)
  U8 endsWith(const String s, const String suffix)
  I64 indexOf(const String s, const String sub)
  I64 lastIndexOf(const String s, const String sub)
  String toUpperCase(const String s)
  String toLowerCase(const String s)
  String trim(const String s)
  String trimStart(const String s)
  String trimEnd(const String s)
  String replace(const String s, const String from, const String to)
  String substring(const String s, I32 start)
  String substring(const String s, I32 start, I32 end)
  String repeat(const String s, I32 n)
  String reverse(const String s)
  I32 toI32(const String s)
  I64 toI64(const String s)
  F32 toF32(const String s)
  F64 toF64(const String s)
  String format(const I8* fmt, ...)
  List<String> split(const String s, const String delim)
  String join(List<String> parts, const String sep)
  const I8* toString(const String s)
}

namespace std {
  struct It {
      std::size_t operator()(const String::It& s) const noexcept {
      return std::hash<std::string>{}(s.handle);
  };
}

namespace DynamicLibrary {
  #if defined(_WIN32) || defined(_WIN64)
    self {
      HINSTANCE handle
      String name
      U8 isValid
    }
  #else
    self {
      U0* handle
      String name
      U8 isValid
    }
  #endif
  DynamicLibrary load(String name)
  U0* get(DynamicLibrary dll, String functionName)
  U0 unload(DynamicLibrary dll)
}

namespace Time {
  U0 sleep(U64 ms)
  U64 millis()
}