LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface - Shareable.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 68 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 35 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_SHAREABLE
      19             : #define INCLUDED_SHAREABLE
      20             : 
      21             : /*
      22             : 
      23             : The Atlas UI DLL needs to share information with the game EXE. It's most
      24             : convenient if they can pass STL objects, like std::wstring and std::vector;
      25             : but that causes problems if the DLL and EXE were not compiled in exactly
      26             : the same way.
      27             : 
      28             : So, the Shareable<T> class is used to make things a bit safer:
      29             : Simple types (primitives, basic structs, etc) are passed as normal.
      30             : std::string is converted to an array, using a shared (and thread-safe) memory
      31             : allocation function so that it works when the DLL and EXE use different heaps.
      32             : std::vector is done the same, though its element type must be Shareable too.
      33             : 
      34             : This ought to protect against:
      35             : * Different heaps (msvcr71 vs msvcr80, debug vs release, etc).
      36             : * Different STL class layout.
      37             : It doesn't protect against:
      38             : * Different data type sizes/ranges.
      39             : * Different packing in our structs. (But they're very simple structs,
      40             : only storing size_t and pointer values.)
      41             : * Vtable layout - this code doesn't actually care, but the rest of Atlas does.
      42             : 
      43             : Usage should be fairly transparent - conversions from T to Shareable<T> are
      44             : automatic, and the opposite is automatic for primitive types.
      45             : For basic structs, use operator-> to access members (e.g. "msg->sharedstruct->value").
      46             : For more complex things (strings, vectors), use the unary operator* to get back
      47             : an STL object (e.g. "std::string s = *msg->sharedstring").
      48             : (That conversion to an STL object is potentially expensive, so
      49             : Shareable<string>.c_str() and Shareable<vector>.GetBuffer/GetSize() can be used
      50             : if that's all you need.)
      51             : 
      52             : The supported list of primitive types is below (SHAREABLE_PRIMITIVE).
      53             : Structs are made shareable by manually ensuring that all their members are
      54             : shareable (i.e. primitives and Shareable<T>s) and writing
      55             :     SHAREABLE_STRUCTS(StructName);
      56             : after their definition.
      57             : 
      58             : */
      59             : 
      60             : // we want to use placement new without grief
      61             : // (Duplicated in SharedMemory.h)
      62             : #undef new
      63             : 
      64             : #include "tools/atlas/GameInterface/SharedMemory.h"
      65             : 
      66             : #include <cstring>
      67             : #include <new>
      68             : #include <string>
      69             : #include <vector>
      70             : 
      71             : namespace AtlasMessage { template <typename T> class Shareable; }
      72             : 
      73             : namespace AtlasMessage
      74             : {
      75             : 
      76             : // By default, things are not shareable
      77             : template <typename T> class Shareable
      78             : {
      79             : public:
      80             :     Shareable();
      81             :     enum { TypeIsShareable = 0 };
      82             : };
      83             : 
      84             : // Primitive types just need a very simple wrapper
      85             : #define SHAREABLE_PRIMITIVE(T) \
      86             :     template<> class Shareable<T> \
      87             :     { \
      88             :         T m; \
      89             :     public: \
      90             :         enum { TypeIsShareable = 1 }; \
      91             :         Shareable() {} \
      92             :         Shareable(T const& rhs) : m(rhs) {} \
      93             :         operator T() const { return m; } \
      94             :         T _Unwrap() const { return m; } \
      95             :     }
      96             : 
      97           0 : SHAREABLE_PRIMITIVE(unsigned char);
      98           0 : SHAREABLE_PRIMITIVE(int);
      99           0 : SHAREABLE_PRIMITIVE(unsigned int);
     100             : SHAREABLE_PRIMITIVE(long);
     101           0 : SHAREABLE_PRIMITIVE(bool);
     102           0 : SHAREABLE_PRIMITIVE(float);
     103           0 : SHAREABLE_PRIMITIVE(double);
     104           0 : SHAREABLE_PRIMITIVE(void*);
     105             : 
     106             : #undef SHAREABLE_PRIMITIVE
     107             : 
     108             : // Structs are similar to primitives, but with operator->
     109             : #define SHAREABLE_STRUCT(T) \
     110             :     template<> class Shareable<T> \
     111             :     { \
     112             :         T m; \
     113             :     public: \
     114             :         enum { TypeIsShareable = 1 }; \
     115             :         Shareable() {} \
     116             :         Shareable(T const& rhs) { m = rhs; } \
     117             :         const T* operator->() const { return &m; } \
     118             :         operator T() const { return m; } \
     119             :         const T _Unwrap() const { return m; } \
     120             :     }
     121             : 
     122             : 
     123             : // Shareable containers must have shareable contents - but it's easy to forget
     124             : // to declare them, so make sure the errors are almost readable, like:
     125             : //   "use of undefined type 'REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE<T,__formal>
     126             : //      with [ T=AtlasMessage::sTerrainTexturePreview, __formal=false ]"
     127             : //
     128             : // (Implementation based on boost/static_assert)
     129             : template <typename T, bool> struct REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE;
     130             : template <typename T> struct REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE<T, true>{};
     131             : template<int x> struct static_assert_test{};
     132             : #define ASSERT_TYPE_IS_SHAREABLE(T) typedef static_assert_test< \
     133             :     sizeof(REQUIRE_TYPE_TO_BE_SHAREABLE_FAILURE< T, (bool)(Shareable<T>::TypeIsShareable) >)> \
     134             :     static_assert_typedef_
     135             : 
     136             : template <typename T> inline const T* empty_str();
     137             : template <> inline const char* empty_str() { return ""; }
     138             : template <> inline const wchar_t* empty_str() { return L""; }
     139             : 
     140             : // Shareable strings:
     141             : template<typename C> class Shareable< std::basic_string<C> >
     142             : {
     143             :     typedef std::basic_string<C> wrapped_type;
     144             : 
     145             :     C* buf; // null-terminated string (perhaps with embedded nulls)
     146             :     size_t length; // size of buf (including null)
     147             : public:
     148             :     enum { TypeIsShareable = 1 };
     149             : 
     150           0 :     Shareable() : buf(NULL), length(0) {}
     151             : 
     152           0 :     Shareable(const wrapped_type& rhs)
     153             :     {
     154           0 :         length = rhs.length()+1;
     155           0 :         buf = (C*)ShareableMallocFptr(sizeof(C)*length);
     156           0 :         memcpy(buf, rhs.c_str(), sizeof(C)*length);
     157           0 :     }
     158             : 
     159           0 :     ~Shareable()
     160             :     {
     161           0 :         ShareableFreeFptr(buf);
     162           0 :     }
     163             : 
     164           0 :     Shareable<wrapped_type>& operator=(const Shareable<wrapped_type>& rhs)
     165             :     {
     166           0 :         if (&rhs == this)
     167             :             return *this;
     168           0 :         ShareableFreeFptr(buf);
     169           0 :         length = rhs.length;
     170           0 :         buf = (C*)ShareableMallocFptr(sizeof(C)*length);
     171           0 :         memcpy(buf, rhs.buf, sizeof(C)*length);
     172           0 :         return *this;
     173             :     }
     174             : 
     175           0 :     Shareable(const Shareable<wrapped_type>& rhs)
     176           0 :         : buf(NULL), length(0)
     177             :     {
     178           0 :         *this = rhs;
     179             :     }
     180             : 
     181           0 :     const wrapped_type _Unwrap() const
     182             :     {
     183           0 :         return (buf && length) ? wrapped_type(buf, buf+length-1) : wrapped_type();
     184             :     }
     185             : 
     186           0 :     const wrapped_type operator*() const
     187             :     {
     188           0 :         return _Unwrap();
     189             :     }
     190             : 
     191             :     // Minor optimisation for code that just wants to access the characters,
     192             :     // without constructing a new std::basic_string then calling c_str on that
     193           0 :     const C* c_str() const
     194             :     {
     195           0 :         return (buf && length) ? buf : empty_str<C>();
     196             :     }
     197             : };
     198             : 
     199             : // Shareable vectors:
     200             : template<typename E> class Shareable<std::vector<E> >
     201             : {
     202             :     ASSERT_TYPE_IS_SHAREABLE(E);
     203             :     typedef std::vector<E> wrapped_type;
     204             :     typedef Shareable<E> element_type;
     205             :     element_type* array;
     206             :     size_t size;
     207             : 
     208             :     // Since we're allocating with malloc (roughly), but storing non-trivial
     209             :     // objects, we have to allocate with placement new and call destructors
     210             :     // manually. (At least the objects are usually just other Shareables, so it's
     211             :     // reasonably safe to assume there's no exceptions or other confusingness.)
     212           0 :     void Unalloc()
     213             :     {
     214           0 :         if (array == NULL)
     215             :             return;
     216             : 
     217           0 :         for (size_t i = 0; i < size; ++i)
     218           0 :             array[i].~element_type();
     219           0 :         ShareableFreeFptr(array);
     220             : 
     221           0 :         array = NULL;
     222           0 :         size = 0;
     223             :     }
     224             : 
     225             : public:
     226             :     enum { TypeIsShareable = 1 };
     227             : 
     228           0 :     Shareable() : array(NULL), size(0) {}
     229             : 
     230           0 :     Shareable(const wrapped_type& rhs)
     231             :     {
     232           0 :         size = rhs.size();
     233           0 :         array = static_cast<element_type*> (ShareableMallocFptr( sizeof(element_type)*size ));
     234           0 :         for (size_t i = 0; i < size; ++i)
     235           0 :             new (&array[i]) element_type (rhs[i]);
     236           0 :     }
     237             : 
     238           0 :     ~Shareable()
     239             :     {
     240           0 :         Unalloc();
     241           0 :     }
     242             : 
     243           0 :     Shareable<wrapped_type>& operator=(const Shareable<wrapped_type>& rhs)
     244             :     {
     245           0 :         if (&rhs == this)
     246             :             return *this;
     247           0 :         Unalloc();
     248           0 :         size = rhs.size;
     249           0 :         array = static_cast<element_type*> (ShareableMallocFptr( sizeof(element_type)*size ));
     250           0 :         for (size_t i = 0; i < size; ++i)
     251           0 :             new (&array[i]) element_type (rhs.array[i]);
     252             :         return *this;
     253             :     }
     254             : 
     255           0 :     Shareable(const Shareable<wrapped_type>& rhs)
     256           0 :         : array(NULL), size(0)
     257             :     {
     258           0 :         *this = rhs;
     259             :     }
     260             : 
     261           0 :     const wrapped_type _Unwrap() const
     262             :     {
     263           0 :         wrapped_type r;
     264           0 :         r.reserve(size);
     265             :         // (/Wp64 causes a spurious warning here. see https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=253172)
     266             : #ifdef _MSC_VER // (can't use MSC_VERSION here since this file is included by Atlas too)
     267             : #pragma warning(push)
     268             : #pragma warning(disable:4267)
     269             : #endif
     270           0 :         for (size_t i = 0; i < size; ++i)
     271           0 :             r.push_back(array[i]._Unwrap());
     272             : #ifdef _MSC_VER
     273             : #pragma warning(pop)
     274             : #endif
     275           0 :         return r;
     276           0 :     }
     277             : 
     278           0 :     const wrapped_type operator*() const
     279             :     {
     280           0 :         return _Unwrap();
     281             :     }
     282             : 
     283             :     // Minor optimisation for code that just wants to access the buffer,
     284             :     // without constructing a new std::vector
     285             :     const element_type* GetBuffer() const
     286             :     {
     287             :         return array;
     288             :     }
     289             :     size_t GetSize() const
     290             :     {
     291             :         return size;
     292             :     }
     293             : };
     294             : 
     295             : 
     296             : // Shareable callbacks:
     297             : // (TODO - this is probably not really safely shareable, due to unspecified calling conventions)
     298             : template<typename T> struct Callback
     299             : {
     300             :     Callback() : cb(NULL), cbdata(NULL) {}
     301             :     Callback(void (*cb) (const T*, void*), void* cbdata) : cb(cb), cbdata(cbdata) {}
     302             :     void (*cb) (const T*, void*);
     303             :     void* cbdata;
     304             : };
     305             : 
     306             : template<typename T> class Shareable<Callback<T> >
     307             : {
     308             :     ASSERT_TYPE_IS_SHAREABLE(T);
     309             : public:
     310             :     Shareable(Callback<T> cb) : cb(cb) {}
     311             :     Callback<T> cb;
     312             :     void Call(const T& data) const
     313             :     {
     314             :         cb.cb(&data, cb.cbdata);
     315             :     }
     316             : };
     317             : 
     318             : 
     319             : }
     320             : 
     321             : #endif // INCLUDED_SHAREABLE

Generated by: LCOV version 1.16