3 回答

TA貢獻1785條經驗 獲得超8個贊
為了進行編譯時間累積,您必須具有一個編譯時間序列。
一種簡單的方法是使用可變參數模板。每個條目將是特定元素的標識符和大小,或特定元素的標識符和類型。
頂級條目將是Layout:
template<std::size_t offset, typename Key, typename... Entries>
struct LayoutHelper {
typedef std::tuple<> type;
};
template<typename Key, typename... Entries>
struct Layout:LayoutHelper<0, Key, Entries...> {};
每個條目將是:
template<typename Key, Key identifier, typename Data>
struct Entry {};
然后,我們執行以下操作:
template<typename Key, Key identifier, typename Data, std::size_t Offset>
struct ProcessedEntry {};
template<std::size_t offset, typename Key, Key id0, typename D0, typename... Entries>
struct LayoutHelper<offset, Key, Entry<Key, id0, D0>, Entries...>
{
typedef typename prepend
< ProcessedEntry< Key, id0, D0, offset >
, typename LayoutHelper<offset+sizeof(D0), Key, Entries...>::type
>::type type;
};
使用看起來像:
Layout< FooEnum, Entry< FooEnum, eFoo, char[10] >, Entry< FooEnum, eFoo2, double > > layout;
在編寫或找到一個prepend包含一個元素和一個tuple并在最前面添加該元素的a之后,這意味著Layout<blah>::type將包含一個tuple描述數據布局的a 。
template<typename T, typename Pack>
struct prepend;
template<typename T, template<typename...>class Pack, typename... Ts>
struct prepend<T, Pack<Ts...>> {
typedef Pack<T, Ts...> type;
};
// use: prepend<int, std::tuple<double>::type is std::tuple<int, double>
// this removes some ::type and typename boilerplate, if it works in your compiler:
template<typename T, typename Pack>
using Prepend = typename prepend<T, Pack>::type;
然后,如果需要,可以將其解壓縮tuple為一個std::array。您可以使用索引技巧來做到這一點(堆棧溢出中有許多示例以不同的方式使用了相同的技巧)。
或者,您可以使用ProcessedEntry并添加方法來訪問數據,然后編寫一個Key搜索編譯時程序,該程序遍歷tuple,尋找匹配項Key,然后返回offsetand size(甚至類型)作為編譯時代碼。也許將an array<N, unsigned char>作為參數并執行reintepret_cast,返回對的引用data。
FooEnum通過using別名刪除重復項會很好。

TA貢獻1802條經驗 獲得超4個贊
“然后您可以將該元組解壓縮為std :: array”,可以在此進行詳細說明。我沒有為此使用什么數組元素類型。提供的基本類型ProcessedEntry
,僅僅持續了數據id
,offset
和size
。我需要在運行時通過id訪問特定的布局條目,以使適當的訪問器類可以訪問特定的布局條目,并計算要從具體的持久性存儲設備讀取的地址和數據大小(或者我是否需要對它進行運行時計算(不確定) ?)

TA貢獻1858條經驗 獲得超8個贊
“會花點時間,很難在手機上鍵入代碼”哈!您正在尋找書呆子; o),因此應為此提供一個應用程序!不,問題需要在下個星期左右找到合適的解決方案。然后看看我的更新。其實我上了幾天假節日; O)...
- 3 回答
- 0 關注
- 563 瀏覽
添加回答
舉報