Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 80 lines (60 sloc) 1.685 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#ifndef RBX_ADDRESS_H
#define RBX_ADDRESS_H


namespace memory {

  /**
* A wonderful little class for working with memory addresses that is used
* always as a value, never a reference or pointer. It basically allows us
* the ability to implement our own pointer logic.
*/

  struct Address {

    /// The pointer this Address struct encapsulates
    uintptr_t address_;

    Address()
      : address_(0)
    {}

    Address(void* addr)
      : address_(reinterpret_cast<intptr_t>(addr))
    {}

    /**
* Type conversion operator; converts this Address to a void*, allowing
* the implicit type conversion rules to handle comparisons etc.
*/
    operator void*() {
      return reinterpret_cast<void*>(address_);
    }

    Address operator+(int change) {
      return Address(reinterpret_cast<void*>(address_ + change));
    }

    Address operator+=(int change) {
      address_ += change;
      return *this;
    }

    size_t operator-(Address change) {
      return address_ - change.address_;
    }

    Address operator-(int change) {
      return Address(reinterpret_cast<void*>(address_ - change));
    }

    Address operator&(uintptr_t mask) {
      return Address(reinterpret_cast<void*>(address_ & mask));
    }

    bool is_null() {
      return address_ == 0;
    }

    static Address null() {
      return Address(0);
    }

    intptr_t as_int() {
      return address_;
    }

    /**
* Converts the memory address represented by this instance to a pointer
* of the specified type.
*/
    template <typename T>
      T* as() {
        return reinterpret_cast<T*>(reinterpret_cast<void*>(address_));
      }
  };

}

#endif
Something went wrong with that request. Please try again.