を使用して代替ソリューションを探していた私は、あなたが望むものについてそれほど明確でないんだけど、あなたのコメントのいずれかから、それはのように思えますこれは役に立つかもしれません。 (または少なくとも コードからいくつかのアイデアを得るかもしれません)
TPunクラスは、ポインタやオブジェクトへの参照を使用してメモリ内のある場所をポイントし、任意のオフセットから値を読み取ることができますバッファの先頭から
main()
のデモンストレーションを見て、どのように使用できるかを見てください。
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <cstddef>
class TPun {
const void* pbuffer;
template <typename IT>
const void* offset_ptr(IT byte_offset) const {
return static_cast<const void*>(static_cast<const char*>(pbuffer) + byte_offset);
}
public:
TPun() : pbuffer(nullptr) {}
template <typename T>
TPun(const T* pbuffer) : pbuffer(static_cast<const void*>(pbuffer)) {}
template <typename T>
TPun(T* pbuffer) : pbuffer(static_cast<const void*>(pbuffer)) {}
template <typename T>
TPun(const T& struct_obj) : pbuffer(static_cast<const void*>(&struct_obj)) {}
template <typename T>
TPun& operator = (const T* pbuf) { pbuffer = static_cast<const void*>(pbuf); return *this; }
template <typename T>
TPun& operator = (T* pbuf) { pbuffer = static_cast<const void*>(pbuf); return *this; }
template <typename T>
TPun& operator = (const T& struct_obj) { pbuffer = static_cast<const void*>(&struct_obj); return *this; }
template <typename T>
struct is_cchar_ptr {
template <typename U> static U& dval();
static int test(char const * const &) { return true; }
template <typename U>
static char test(const U&) { return false; }
static const bool value = sizeof(int) == sizeof(test(dval<T>()));
};
// Returns a reference for non-pointer types (only used for const char*)
template <typename T> struct cptr_pass { static const T& get(const void* p) { return *static_cast<const T*>(p); } };
template <typename T> struct cptr_pass<const T*> { static const T* get(const void* p) { return static_cast<const T*>(p); } };
// at only works for POD types or classes consisting of only POD members
template <typename T, typename IT>
T at(IT byte_offset) const {
if(is_cchar_ptr<T>::value) {
// special handling when T is const char*
// returns a pointer to the buffer instead of a pointer created from buffer data
// using char to access the buffer avoids the strict aliasing rule
return cptr_pass<T>::get(offset_ptr(byte_offset));
}
T value;
std::memcpy(&value, offset_ptr(byte_offset), sizeof(T));
return value;
}
// This version of at() works with any type, but sports Undefined Behavior,
// because it violates the strict aliasing rule.
// This can also cause problems if the offset you give, along with the buffer's
// base address result in a pointer which is not aligned properly for the type.
template <typename T, typename IT>
const T& the_at_of_many_dangers(IT byte_offset) {
return *static_cast<const T&>(offset_ptr(byte_offset));
}
std::ostream& manipulate(std::ostream& is) { return is; }
template<typename M, typename ...Ms>
std::ostream& manipulate(std::ostream& is, const M& manipulator, Ms ...manips) {
return manipulate(is << manipulator, manips...);
}
template <typename T, typename IT, typename ...Ms>
std::string string_at(IT byte_offset, Ms ...manips) {
std::stringstream ss;
manipulate(ss, manips...) << at<T>(byte_offset);
return ss.str();
}
};
void no_warning_strcpy(char* d, const char* s) { while(*(d++) = *(s++)) {} }
struct test_struct {
char text[10];
float number;
int some_int;
double a_double;
test_struct() {
no_warning_strcpy(text, "hi there");
number = 52.25;
some_int = 1000000000;
a_double = 1.2e-14;
}
};
int main() {
using std::cout;
using std::string;
// create a test_struct (values are filled in automatically)
test_struct ts;
// copy ts's memory image to a char buffer
char* memory_buffer = new char[sizeof(ts)];
std::memcpy(memory_buffer, &ts, sizeof(ts));
// Create a TPun object, start off pointing at the char buffer
TPun tpun(memory_buffer);
// send some field values to cout
// the "offsetof" macro (defined in <cstddef>) gives the
// byte offset of a field in a struct
cout << tpun.at<const char*>(offsetof(test_struct, text)) << '\n';
cout << tpun.at<float>(offsetof(test_struct, number)) << '\n';
// format a value into a string. Optionally, add extra ostream manipulator arguments.
// This makes a string with the data from some_int, and formats it as a hex string
string hex_string = tpun.string_at<int>(offsetof(test_struct, some_int), std::hex);
cout << hex_string << '\n';
// I screwed this one up -- I used the text field's data to make an int
// Using the manipulator this way, instead of going directly through
// cout, keeps cout's flags intact
cout << tpun.string_at<int>(offsetof(test_struct, text), std::hex) << '\n';
// change a_double in the original struct
ts.a_double = -1;
// tpun is pointing at the buffer copy, so it shows the original value
cout << tpun.at<double>(offsetof(test_struct, a_double)) << '\n';
// tpun's target can be chaged on-the-fly like this, and it can be set to
// view an object's memory directly, without creating a pointer.
tpun = ts;
// outputs -1
cout << tpun.at<double>(offsetof(test_struct, a_double)) << '\n';
}
このデモプリントは:
hi there
52.25
3b9aca00
74206968
1.2e-14
-1
あなたが実際にそれをどうするかしたいですか? – xaxxon
_ "テンプレートを使用して代替ソリューションを探していたためです" _実際のタイプ(例:列挙型)の識別テンプレートパラメータによっては、特殊化が必要でした。 –
checkout 'boost:variant' –