我有一个要求,我需要通过2个应用程序之间的线路(二进制over tcp)传输信息.一个是Java,另一个是C++.我需要一个协议实现来在这两个应用程序之间传输对象.Object类存在于两个应用程序中(相应地映射).我只需要在一侧保留一些编码方案,在一侧保留Object表示,并在另一侧解码为完整的Object.
例如,
C++类
class Person
{
int age;
string name;
};
Run Code Online (Sandbox Code Playgroud)
Java类
class Person
{
int age;
String name;
}
Run Code Online (Sandbox Code Playgroud)
C++编码
Person p;
p.age = 20;
p.name = "somename";
char[] arr = SomeProtocolEncoder.encode(p);
socket.send(arr);
Run Code Online (Sandbox Code Playgroud)
Java解码
byte[] arr = socket.read();
SomeProtocolIntermediateObject object = SomeProtocolDecoder.decode(arr);
Person p = (Person)ReflectionUtil.get(object);
Run Code Online (Sandbox Code Playgroud)
协议应该提供一些维护对象表示状态的中间对象,以便使用反射我可以稍后返回对象.
为什么下面的小样本在Linux64下失败但在Windows32下失败?
module test;
import std.string, std.stdio;
void main(string[] args)
{
string a = "abcd=1234";
auto b = &a;
auto Index = indexOf(*b, '=');
if (Index != -1)
*cast (char*) (b.ptr + Index) = '#';
writeln(*b);
readln;
}
Run Code Online (Sandbox Code Playgroud) 注意:这几乎与此问题相同:访问所有节点的最短路径
但我有一个完整的图表.
问题:考虑具有非负边长的完整无向图.问题:计算至少访问每个节点一次的最短路径.
注意:这不是TSP问题.路径没有结束节点,路径可以多次通过节点.
其他说明:
节点数很少(小于20).
当我将一个函数传递给另一个函数时,出现以下错误?
const std = @import("std");
const St = struct { a: usize };
fn returnFunc(print: fn (str: []const u8, st: St) void) void {
print("Hello", St{ .a = 1 });
}
fn toTest(str: []const u8, st: St) void {
std.debug.print("{s}: {d}\n", .{str, st.a});
}
pub fn main() !void {
returnFunc(toTest);
}
Run Code Online (Sandbox Code Playgroud)
返回以下错误:
error: parameter of type 'fn([]const u8, main.St) void' must be declared comptime
Run Code Online (Sandbox Code Playgroud)
机器详细信息: Zig 版本:0.10.0-dev.4588+9c0d975a0 M1 Mac、MAC OS Ventura
我确定我在这里遗漏了一些明显的东西 - 剩下的D(甚至是编译器错误)都非常明智且易于理解.我有一个std.containers.Array类似的结构,我想对它进行排序.该std.containers文件指出,为了使用的东西,在std.algorithm你需要切它,要么array[]或array.opSlice().好的没问题.
但是,如果我制作了Array两个非常简单的类型,它就不会排序 - 相反,它告诉我在Phobos内部的一个例程并不是非常的(?)
B:\lib\D\dmd2\windows\bin\..\..\src\phobos\std\range\package.d(7189): Error: 'std.range.SortedRange!(RangeT!(Array!(MyInt)), "a < b").SortedRange.dbgVerifySorted' is not nothrow
B:\lib\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\sorting.d(982): Error: template instance std.range.assumeSorted!("a < b", RangeT!(Array!(MyInt))) error instantiating
main.d(21): instantiated from here: sort!("a < b", cast(SwapStrategy)0, RangeT!(Array!(MyInt)))
Run Code Online (Sandbox Code Playgroud)
下面的最小例子.第一个sort(自动生成的两个值的标准数组)排序很好.其他sort调用因上述编译器错误而失败.使用VS Community 2015中的DMD2构建,我找不到编译器版本标识符,但这只是昨天下载的.
import std.array;
import std.container.array;
import std.algorithm.sorting;
struct MyInt
{
int data;
int opCmp(MyInt o)
{
return data - o.data;
}
}
int main(string[] argv)
{
MyInt ami, …Run Code Online (Sandbox Code Playgroud) 我想在编译时加入文件名和图像格式.以下示例不起作用,因为string[]无法在编译时评估我想...
immutable imageFormats = ["bmp", "jpg", "gif", "png"];
template fileNamesWithImageFormat(string[] fileNames)
{
string[] fileNamesWithImageFormat() {
string[] ret;
ret.length = imageFormats.length * fileNames.length;
for (int j = 0; j < fileNames.length) {
for (int i = 0; i < imageFormats.length; ++i) {
ret[j * fileNames.length + i] = fileNames[j] ~ "." ~ imageFormats[i];
}
}
return ret;
}
}
Run Code Online (Sandbox Code Playgroud)
它失败并显示错误消息:
Error: arithmetic/string type expected for value-parameter, not string[]
Run Code Online (Sandbox Code Playgroud)
我需要最终加入import().如何解决错误?
我试图获取目录的内容,按修改时间排序.我不认为有一种方法可以在dirEntries调用中直接执行此操作,因此我的策略是收集所有文件时间和名称,然后按锁定步骤对两个数组进行排序.
问题1:我无法弄清楚如何将a sysTime转换为整数.
问题2:我无法弄清楚如何并行排序两个数组.
与D中的每个问题一样,不可能找到如何做到这一点:(
这是我的代码:
import std.file;
import std.stdio : writeln;
import std.algorithm;
import std.datetime;
void main() {
string[] myFiles;
double[] myTimes;
foreach (DirEntry e; dirEntries("c:/users/istaffel/", SpanMode.shallow)) {
// calculate unix time (this doesn't work)
auto dur = (cast(Date)e.timeLastModified) - Date(1970,1,1);
// store modified time and filename
myTimes ~= dur.seconds;
myFiles ~= e.name;
}
// now find a way to sort myFiles in order of ascending myTimes...
// print in order
for (int i = 0; …Run Code Online (Sandbox Code Playgroud) ref void init_board (ref int side, ref char[][] board) //make empty symbol chessboard
{
const char black = ' ';
const char white = 0xB0;
board[0][0] = ' ';
for (int i = 1; i <= side; i++)
{
board[i][0] = 0x30 + i; //Setting nums; "Error: Cannot convert int to char"
board[0][i] = 0x40 + i; //Setting letters; same here
for (int j = 1; j <= side; j++)
board[i][j] = (i+j)%2 == 0 ? black : white; //making …Run Code Online (Sandbox Code Playgroud)