2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_SHAREABLE
|
|
|
|
#define INCLUDED_SHAREABLE
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
The Atlas UI DLL needs to share information with the game EXE. It's most
|
|
|
|
convenient if they can pass STL objects, like std::wstring and std::vector;
|
|
|
|
but that causes problems if the DLL and EXE were not compiled in exactly
|
|
|
|
the same way.
|
|
|
|
|
|
|
|
So, the Shareable<T> class is used to make things a bit safer:
|
2006-04-25 07:10:47 +02:00
|
|
|
Simple types (primitives, basic structs, etc) are passed as normal.
|
2006-04-24 01:14:18 +02:00
|
|
|
std::string is converted to an array, using a shared (and thread-safe) memory
|
2007-06-03 17:37:26 +02:00
|
|
|
allocation function so that it works when the DLL and EXE use different heaps.
|
2006-04-24 01:14:18 +02:00
|
|
|
std::vector is done the same, though its element type must be Shareable too.
|
|
|
|
|
|
|
|
This ought to protect against:
|
|
|
|
* Different heaps (msvcr71 vs msvcr80, debug vs release, etc).
|
|
|
|
* Different STL class layout.
|
|
|
|
It doesn't protect against:
|
|
|
|
* Different data type sizes/ranges.
|
|
|
|
* Different packing in our structs. (But they're very simple structs,
|
|
|
|
only storing size_t and pointer values.)
|
|
|
|
* Vtable layout - this code doesn't actually care, but the rest of Atlas does.
|
|
|
|
|
|
|
|
Usage should be fairly transparent - conversions from T to Shareable<T> are
|
|
|
|
automatic, and the opposite is automatic for primitive types.
|
2006-04-25 07:10:47 +02:00
|
|
|
For basic structs, use operator-> to access members (e.g. "msg->sharedstruct->value").
|
2006-04-24 01:14:18 +02:00
|
|
|
For more complex things (strings, vectors), use the unary operator* to get back
|
|
|
|
an STL object (e.g. "std::string s = *msg->sharedstring").
|
|
|
|
(That conversion to an STL object is potentially expensive, so
|
|
|
|
Shareable<string>.c_str() and Shareable<vector>.GetBuffer/GetSize() can be used
|
|
|
|
if that's all you need.)
|
|
|
|
|
|
|
|
The supported list of primitive types is below (SHAREABLE_PRIMITIVE).
|
|
|
|
Structs are made shareable by manually ensuring that all their members are
|
2006-04-25 07:10:47 +02:00
|
|
|
shareable (i.e. primitives and Shareable<T>s) and writing
|
|
|
|
SHAREABLE_STRUCTS(StructName);
|
2006-04-24 01:14:18 +02:00
|
|
|
after their definition.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SharedMemory.h"
|
|
|
|
|
2006-11-12 05:02:36 +01:00
|
|
|
#include <vector>
|
2007-09-02 18:50:20 +02:00
|
|
|
#include <string>
|
2006-11-12 05:02:36 +01:00
|
|
|
|
2008-01-19 12:33:11 +01:00
|
|
|
// we want to use placement new without grief
|
2006-04-24 01:14:18 +02:00
|
|
|
// (Duplicated in SharedMemory.h)
|
2008-01-19 12:33:11 +01:00
|
|
|
#undef new
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
namespace AtlasMessage
|
|
|
|
{
|
|
|
|
|
|
|
|
// By default, things are not shareable
|
|
|
|
template <typename T> class Shareable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Shareable();
|
|
|
|
enum { TypeIsShareable = 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
// Primitive types just need a very simple wrapper
|
|
|
|
#define SHAREABLE_PRIMITIVE(T) \
|
|
|
|
template<> class Shareable<T> \
|
|
|
|
{ \
|
|
|
|
T m; \
|
|
|
|
public: \
|
|
|
|
enum { TypeIsShareable = 1 }; \
|
|
|
|
Shareable() {} \
|
2007-01-03 16:45:13 +01:00
|
|
|
Shareable(T const& rhs) : m(rhs) {} \
|
|
|
|
operator T() const { return m; } \
|
|
|
|
T _Unwrap() const { return m; } \
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SHAREABLE_PRIMITIVE(unsigned char);
|
|
|
|
SHAREABLE_PRIMITIVE(int);
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
SHAREABLE_PRIMITIVE(size_t);
|
2006-11-24 20:45:04 +01:00
|
|
|
SHAREABLE_PRIMITIVE(long);
|
2006-04-24 01:14:18 +02:00
|
|
|
SHAREABLE_PRIMITIVE(bool);
|
|
|
|
SHAREABLE_PRIMITIVE(float);
|
|
|
|
SHAREABLE_PRIMITIVE(void*);
|
|
|
|
|
|
|
|
#undef SHAREABLE_PRIMITIVE
|
|
|
|
|
2006-04-25 07:10:47 +02:00
|
|
|
// Structs are similar to primitives, but with operator->
|
|
|
|
#define SHAREABLE_STRUCT(T) \
|
2006-04-24 01:14:18 +02:00
|
|
|
template<> class Shareable<T> \
|
|
|
|
{ \
|
|
|
|
T m; \
|
|
|
|
public: \
|
|
|
|
enum { TypeIsShareable = 1 }; \
|
|
|
|
Shareable() {} \
|
|
|
|
Shareable(T const& rhs) { m = rhs; } \
|
|
|
|
const T* operator->() const { return &m; } \
|
|
|
|
operator const T() const { return m; } \
|
|
|
|
const T _Unwrap() const { return m; } \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Shareable containers must have shareable contents - but it's easy to forget
|
|
|
|
// to declare them, so make sure the errors are almost readable, like:
|
|
|
|
// "use of undefined type 'REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE<T,__formal>
|
|
|
|
// with [ T=AtlasMessage::sTerrainGroupPreview, __formal=false ]"
|
|
|
|
//
|
|
|
|
// (Implementation based on boost/static_assert)
|
|
|
|
template <typename T, bool> struct REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE;
|
|
|
|
template <typename T> struct REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE<T, true>{};
|
|
|
|
template<int x> struct static_assert_test{};
|
|
|
|
#define ASSERT_TYPE_IS_SHAREABLE(T) typedef static_assert_test< \
|
|
|
|
sizeof(REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE< T, (bool)(Shareable<T>::TypeIsShareable) >)> \
|
|
|
|
static_assert_typedef_
|
|
|
|
|
2008-07-19 02:58:34 +02:00
|
|
|
template <typename T> inline const T* empty_str();
|
|
|
|
template <> inline const char* empty_str() { return ""; }
|
|
|
|
template <> inline const wchar_t* empty_str() { return L""; }
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
// Shareable strings:
|
|
|
|
template<typename C> class Shareable< std::basic_string<C> >
|
|
|
|
{
|
|
|
|
typedef std::basic_string<C> wrapped_type;
|
|
|
|
|
|
|
|
C* buf; // null-terminated string (perhaps with embedded nulls)
|
|
|
|
size_t length; // size of buf (including null)
|
|
|
|
public:
|
|
|
|
enum { TypeIsShareable = 1 };
|
|
|
|
|
|
|
|
Shareable() : buf(NULL), length(0) {}
|
|
|
|
|
|
|
|
Shareable(const wrapped_type& rhs)
|
|
|
|
{
|
|
|
|
length = rhs.length()+1;
|
|
|
|
buf = (C*)ShareableMallocFptr(sizeof(C)*length);
|
|
|
|
memcpy(buf, rhs.c_str(), sizeof(C)*length);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Shareable()
|
|
|
|
{
|
|
|
|
ShareableFreeFptr(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Shareable<wrapped_type>& operator=(const Shareable<wrapped_type>& rhs)
|
|
|
|
{
|
|
|
|
if (&rhs == this)
|
|
|
|
return *this;
|
|
|
|
ShareableFreeFptr(buf);
|
|
|
|
length = rhs.length;
|
|
|
|
buf = (C*)ShareableMallocFptr(sizeof(C)*length);
|
|
|
|
memcpy(buf, rhs.buf, sizeof(C)*length);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shareable(const Shareable<wrapped_type>& rhs)
|
|
|
|
: buf(NULL), length(0)
|
|
|
|
{
|
|
|
|
*this = rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapped_type _Unwrap() const
|
|
|
|
{
|
# Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.
This was SVN commit r4261.
2006-08-28 19:36:42 +02:00
|
|
|
return (buf && length) ? wrapped_type(buf, buf+length-1) : wrapped_type();
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const wrapped_type operator*() const
|
|
|
|
{
|
|
|
|
return _Unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minor optimisation for code that just wants to access the characters,
|
|
|
|
// without constructing a new std::basic_string then calling c_str on that
|
|
|
|
const C* c_str() const
|
|
|
|
{
|
2008-07-19 02:40:08 +02:00
|
|
|
return (buf && length) ? buf : empty_str<C>();
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Shareable vectors:
|
|
|
|
template<typename E> class Shareable<std::vector<E> >
|
|
|
|
{
|
|
|
|
ASSERT_TYPE_IS_SHAREABLE(E);
|
|
|
|
typedef std::vector<E> wrapped_type;
|
|
|
|
typedef Shareable<E> element_type;
|
|
|
|
element_type* array;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
// Since we're allocating with malloc (roughly), but storing non-trivial
|
|
|
|
// objects, we have to allocate with placement new and call destructors
|
|
|
|
// manually. (At least the objects are usually just other Shareables, so it's
|
|
|
|
// reasonably safe to assume there's no exceptions or other confusingness.)
|
|
|
|
void Unalloc()
|
|
|
|
{
|
|
|
|
if (array == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
array[i].~element_type();
|
|
|
|
ShareableFreeFptr(array);
|
|
|
|
|
|
|
|
array = NULL;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum { TypeIsShareable = 1 };
|
|
|
|
|
|
|
|
Shareable() : array(NULL), size(0) {}
|
|
|
|
|
|
|
|
Shareable(const wrapped_type& rhs)
|
|
|
|
{
|
|
|
|
size = rhs.size();
|
|
|
|
array = static_cast<element_type*> (ShareableMallocFptr( sizeof(element_type)*size ));
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
new (&array[i]) element_type (rhs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Shareable()
|
|
|
|
{
|
|
|
|
Unalloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
Shareable<wrapped_type>& operator=(const Shareable<wrapped_type>& rhs)
|
|
|
|
{
|
|
|
|
if (&rhs == this)
|
|
|
|
return *this;
|
|
|
|
Unalloc();
|
|
|
|
size = rhs.size;
|
|
|
|
array = static_cast<element_type*> (ShareableMallocFptr( sizeof(element_type)*size ));
|
2006-09-26 03:44:20 +02:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
new (&array[i]) element_type (rhs.array[i]);
|
2006-04-24 01:14:18 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Shareable(const Shareable<wrapped_type>& rhs)
|
|
|
|
: array(NULL), size(0)
|
|
|
|
{
|
|
|
|
*this = rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapped_type _Unwrap() const
|
|
|
|
{
|
|
|
|
wrapped_type r;
|
|
|
|
r.reserve(size);
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
// (/Wp64 causes a spurious warning here. see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=253172)
|
|
|
|
#if MSC_VERSION
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4267)
|
|
|
|
#endif
|
2006-04-24 01:14:18 +02:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
r.push_back(array[i]._Unwrap());
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
#if MSC_VERSION
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
2006-04-24 01:14:18 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapped_type operator*() const
|
|
|
|
{
|
|
|
|
return _Unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minor optimisation for code that just wants to access the buffer,
|
|
|
|
// without constructing a new std::vector
|
|
|
|
const element_type* GetBuffer() const
|
|
|
|
{
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
size_t GetSize() const
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-12-05 03:35:00 +01:00
|
|
|
|
|
|
|
// Shareable callbacks:
|
|
|
|
// (TODO - this is probably not really safely shareable, due to unspecified calling conventions)
|
|
|
|
template<typename T> struct Callback
|
|
|
|
{
|
2007-06-14 14:11:22 +02:00
|
|
|
Callback() : cb(NULL), cbdata(NULL) {}
|
2006-12-05 03:35:00 +01:00
|
|
|
Callback(void (*cb) (const T*, void*), void* cbdata) : cb(cb), cbdata(cbdata) {}
|
|
|
|
void (*cb) (const T*, void*);
|
|
|
|
void* cbdata;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> class Shareable<Callback<T> >
|
|
|
|
{
|
|
|
|
ASSERT_TYPE_IS_SHAREABLE(T);
|
|
|
|
public:
|
|
|
|
Shareable(Callback<T> cb) : cb(cb) {}
|
|
|
|
Callback<T> cb;
|
|
|
|
void Call(const T& data) const
|
|
|
|
{
|
|
|
|
cb.cb(&data, cb.cbdata);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#endif // INCLUDED_SHAREABLE
|