Git Product home page Git Product logo

Comments (2)

snake-biscuits avatar snake-biscuits commented on July 23, 2024

Proposal:

give members a Bitfield class from a dict
bitfield repr can show each member, bitfield members can be accessed by name as top level class members

MappedArray:

class FirstExample(base.MappedArray):
    _mapping = ["member", "bitfield"]
    _format = "hi"
    _bitfields = {"bitfield": [("a", 8), ("b", 16), ("c", 8)]}
struct FirstExample {
    uint16_t  member;
    uint32_t  a: 8, b: 16, c: 8;
}

Struct:

class SecondExample(base.Struct):
    __slots__ = ["member", "child"]
    _format = "2hi"
    _arrays = {"child": ["member", "bitfield"]}
    _bitfields = {"child.bitfield": [("a", 8), ("b", 16), ("c", 8)]}
    # if "." in _bitfields_key, add _bitfields to child
    # else _bitfields_key = Bitfield(...)
struct SecondExample {
    uint16_t  member;
    struct {
        uint16_t  member;
        uint32_t  a: 8, b: 16, c: 8;
    } child;
};

Use:

>>> fe = FirstExample.frombytes(b"\x11\x11\xCC\xBB\xBB\xAA")
>>> fe.bitfield
<Bitfield (a: 0xAA, b: 0xBBBB, c: 0xCC)>
>>> fe.a
0xAA

All of this will require some checks (or at least tests w/ warnings):

  • Fields to cover the whole bitfield type (need to match struct type char to bitfield)
  • Bitfield must know it's type to create a inverted mask
    • in @b.setter: bitfield = b << 8 | bitfield & (2 ** 32 - 1 ^ (2 ** 8 - 1) << 8)
      could probably build a mask dict in __init__ ...
      number of calculations per lookup should be minimal, so calculate constants in __init__

We'll also be overlapping with other planned MappedArray & Struct features:

  • subclasses (e.g. DisplacementNeighbour(base.Struct) or _subclasses = {"origin": vec3})
  • C struct conversion / repr (be sure to use stdint)
  • unpacking _format & matching each member to a type (part of C conversion)

If the BitField class handles all the getting & setting, the LumpClass can just access the bitfield members

from bsp_tool.

snake-biscuits avatar snake-biscuits commented on July 23, 2024

commit 810cb7e introduces this feature

from bsp_tool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.