我想创建轻型对象数据包以在客户端和服务器应用程序之间传递.
这是一个如此简单的任务,我只能控制1个字节,因此一个字节中的每个位都有不同的含义,
仅使用位
0 = False 1 = True
Itens我现在需要:
1 - Loaded from database 2 - Persisted 3 - Changed 4 - Marked to Delete 5 - 6 - 7 - Null Value 8 - Read Only 1) How do I use bit operators in Delphi to check each bit value? 2) How do I set the bit Values?
解
在所有帮助之后,我会使用下一个Set
TStateType = ( stLoaded = 0, // loaded from persistance stNative = 2, // value loaded and converted to native type stPersisted = 3, // saved stChanged = 4, // object or member changed stToDelete = 5, // marked to delete stReadOnly = 6, // read only object, will not allow changes stNull = 7 // value is null ); TState = Set of TStateType;
对于流 - >持久性,这将是要使用的记录:
TDataPackage = record Data: TBytes; TypeInfo: TMetaInfo; State: Byte; Instance: TBuffer; end;
谢谢大家,感谢所有的答案和评论.
我真的为此使用了一套.但是,我看到你真的想要一个字节.在任何地方使用集合,然后在最后将类型转换为一个字节.
Barry Kelly指出,这种解决方案需要更少的打字,支持标准的delphi运营商并且不会带来任何性能损失.
procedure Test; type TSetValues = ( TSetValue1 = 0, TSetValue2 = 1, TSetValue4 = 2, TSetValue8 = 3, TSetValue16 = 4, TSetValue32 = 5, TSetValue64 = 6, TSetValue128 = 7 ); TMySet = set of TSetValues; var myValue: byte; mySet: TMySet; begin mySet := [TSetValue2, TSetValue16, TSetValue128]; myValue := byte(mySet); ShowMessage(IntToStr(myValue)); // <-- shows 146 end;
我会用一套:
type TMyDatum = (mdLoaded, mdPersisted, mdChanged, mdMarkedToDelete, ...); TMyData = set of TMyDatum; var Foo: TMyData; begin Foo := [mdLoaded, mdChanged]; if (mdPersisted in Foo) then ...
它们以整数形式实现,因此您可以轻松地传递它们.我发现代码比按位运算符更容易读取.