컴파일 타임에 키 -> 유형 맵을 생성 할 수 있으며, 각 키 - 값은 variadic의 인스턴스 함수가 호출됩니까?variadic 함수에 대한 호출로 채워지는 컴파일 타임 키 - 타입 맵 생성
template <typename T, typename ... Args>
void writeToQueue(Args... args) {
//Do something with args.
// Insert to map. something akin to:
// CODEMAP[T] = Args...
// T -> Args... mapped when foo<T,Args...> is called.
}
또는 위에서
template <int code, typename ... Args>
void writeToQueue(Args... args) {
//Do something with args.
// Insert to map. something akin to:
// CODEMAP[code] = Args...
// code -> Args... mapped when foo<code,Args...> is called.
}
는 요구 사항 매핑 대해 codemap이 다음 중 하나입니다 타입 -> 입력하거나 (가능한 중)하지만지도가 채워집니다 INT-> 입력 할 때 기능 foo가 호출되므로 코드와 args를 미리 아는 것은 필수 사항이 아닙니다.
이 모든 것이 가능합니까? boost/preprocessor/template 프로그래밍을 통해?
편집 : CODEMAP은 명시된 바와 같이 코드 -> 유형 정보를 저장하는지도입니다. 런타임 중에 리더 블록 (예 : R)은 foo()가 저장/처리 한 메시지를 읽고 메시지 시작 부분의 코드를 기반으로 구문 분석합니다. 코드는 항상 고정 크기 (4 문자 또는 1 int)입니다.
번역 단위가 같습니다.
편집 :
프로듀서 : 그래서 여기 거래의 FIFO 큐 (중요한 코드 핫 경로)에 데이터를 기록 -> 소비자 스레드가 읽기 및 대기열에서 정보를 처리합니다.
의사 코드는 다음과 같습니다 :
프로듀서 :
void Producer::run() {
// This guy shouldn't worry about the type of data being written.
// So, encapsulating data into structs and passing it to queue is
// out of question.
writeToQueue<Code1>(1,2,"123123",'a', 3.1416);
writeToQueue<Code2>(4,1,'b');
template <int Code, typename ...Args>
void writeToQueue(Args... args) {
queue.insert(args...);
// Need code to args... mapping. So, decided to create static
// instantiation of a formatspecifier class.
static formatspecifier<Code, args...> f{};
}
// To encode the type information to be used in run time.
template <int Code, typename ... Args>
class formatspecifier{
formatspecifier() {
global::codemap[Code] = encodeTypeInfo<Args...>();
}
};
}
소비자 :
이void Consumer::readfromQueue() {
while(true) {
if (queue.dataAvailable()) {
const auto code = queue.getCode();
// get encoded type info format from global::codemap map.
const auto fmt = global::codemap[code];
for (int i=0; i < fmt.len; i++) {
// I am unsure how this part should look.
process<fmt[0]::type>(queue.getData<fmt[0]::type>());
}
}
}
}
당신의 목표가 대해 codemap 무엇입니까? 사용 예를 제공하십시오. 또한 여러 번역 단위를 어떻게 다룰 것입니까? (전체 프로젝트에 대해 하나의 단일 코드 맵을 원하십니까? 아니면 각 TU에 대해 하나의 코드 맵이 충분한가요?) – Dutow
그래서 멀티 맵을 사용하고 있으며'code' 키에 대해 임의의 수의 값을 한번에 삽입하고자합니다. 너가 원하는게 그거야? – Rerito
"이전"CODEMAP이'foo'에 대한 모든 호출에 전달되고 새로운 CODEMAP이 반환되면 문제는 함수형 프로그래밍 언어로지도 데이터 구조를 구현하는 것으로 줄어 듭니다. 그러나 그것은 당신이 불변 값만을 가지고있는 프로그래밍 언어 (즉, TMP)로 가변 전역을 구현하려고하는 것처럼 보입니다 ... –