我会将文件 A 从 cli 复制到文件 B,就像 gforth script.fs -- 文件 A 文件 B 一样,'--' 似乎是强制性的,以使 gforth 尝试将其作为附加 forth 代码官方文档考试加载……
我会从 cli 复制文件 A 到文件 B,就像
gforth script.fs -- fileA fileB
'--' 似乎是强制的,以使 gforth 无效,试图将其加载为附加的 forth 代码
官方文件示例说
0 Value fd-in
0 Value fd-out
: open-input ( addr u -- ) r/o open-file throw to fd-in ;
: open-output ( addr u -- ) w/o create-file throw to fd-out ;
s" foo.in" open-input
s" foo.out" open-output
: copy-file ( -- )
begin
line-buffer max-line fd-in read-line throw
while
line-buffer swap fd-out write-line throw
repeat ;
but I would get filenames from CLI
so I tryied many things around
\ Define buffer size for filenames and IO operations
256 Constant filename-len
4096 Constant io-buffer-len
\ Allocate buffers for filenames
Create source-filename filename-len allot
Create dest-filename filename-len allot
Create io-buffer io-buffer-len allot
\ Variables to store file IDs
Variable source-file-id
Variable dest-file-id
\ Define a word to calculate the length of a string up to a maximum length
: str-len ( c-addr max-len -- n )
0 swap ( n c-addr )
begin dup 1+ ( n c-addr+1 )
over + c@ ( n c-addr+1 c-addr+1[i] )
while 1+ ( n+1 )
repeat drop ;
\ Safely store command-line arguments into buffers
: safe-store ( addr len arg# -- )
arg swap ( c-addr u arg# )
2dup >r str-len r> min ( c-addr u n )
move ;
: store-filenames
\ Store first filename, ensure it doesn't exceed buffer size
source-filename filename-len 1 safe-store
\ Store second filename, ensure it doesn't exceed buffer size
dest-filename filename-len 2 safe-store ;
\ Open the source file for reading
: open-source-file
source-filename filename-len s" r" open-file throw source-file-id ! ;
\ Open the destination file for writing (create if not exists)
: open-dest-file
dest-filename filename-len s" w" open-file throw dest-file-id ! ;
\ Copy content from the source file to the destination file
: copy-file
begin
source-file-id @ io-buffer io-buffer-len read-file throw
while
io-buffer swap dest-file-id @ write-file throw
repeat ;
\ Close the files
: close-files
source-file-id @ close-file throw
dest-file-id @ close-file throw ;
\ Main routine
: main
store-filenames
open-source-file
open-dest-file
copy-file
close-files ;
\ Execute the main routine
main
class A
{
int m_A = 1;
public:
void assign(const A& other)
{
m_A = other.m_A;
}
};
class B : public A
{
int m_B = 2;
public:
void assign(const B& other)
{
A::assign(other);
m_B = other.m_B;
}
};
class C : public B
{
int m_C = 3;
public:
void assign(const C& other)
{
B::assign(other);
m_C = other.m_C;
}
};
//... and so on
class Berr1 : public A
{
int m_Berr1 = 2;
public:
//void assign(const Berr1& other) // Oops!
};
class Berr2 : public A
{
int m_Berr2 = 2;
public:
void assign(const Berr2& other)
{
//A::assign(other); // Oops!
//...
}
};
这非常脆弱,我想知道是否有一种技术可以保证继承自的子类 A 实现成员函数 assign() ,观察:
成员 assign() 函数没有相同的原型。将父对象(例如 A )分配给子对象(例如 B )是错误的,必然会导致编译错误。
public class ServiceA {
ResponseA getResponse(PayloadA){...}
}
public class ServiceB {
ResponseB getResponse(PayloadB){...}
}
public class ServiceC {
ResponseC getResponse(PayloadC){...}
}
方法主体逐字逐句都相同,但因为它是使用 WebClient 的发布请求,并且方法以 结尾 ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(response, new TypeReference<>() {}); ,所以我们只是一遍又一遍地复制该方法。
public abstract class ParentService<R extends IResponse, P extends IPayload>{
public abstract R getPayload(P);
}
public class ServiceA extends ParentService<ResponseA, PayloadA> {
@Override
public ResponseA getPayload(PayloadA){
/***/
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(response, new TypeReference<>() {});
}
}
public class GeneralService {
public <T> T getResponse(Object payload, Class<T> clazz){
// do sth with payload
return mapper.readValue(response, clazz);
}
}
如果您希望能够反序列化集合,则可以 TypeReference 使用 Class :
public class GeneralService {
public <T> T getResponse(Object payload, TypeReference<T> type){
// do sth with payload
return mapper.readValue(response, type);
}
}
然后使用类型调用它,例如字符串列表:
generalService.getResponse(payload, new TypeReference<List<String>>(){})