1

我有以下UtlSharedIPCWrapper我创建的模板类访问放置在进程间内存中的用户定义类型。模板元编程如何专注于集合

通常这个类是使用一个简单的类型,例如:

// construct a FaultReport - default to no faults 
auto faultWrapper = managed_shm.construct< 
    UtlSharedIPCWrapper<uint64_t>>("FaultReport")(0); 

,这工作得很好,但是我最近有需要使用升压共享存储器映射集合(boost::interprocess::map)作为模板参数)如:

using char_allocator = boost::interprocess::managed_shared_memory::allocator<char>::type; 
using shm_string = boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator>; 
using KeyType = shm_string; 
using ValueType = std::pair<const KeyType, shm_string>; 
using ShmemAllocator = boost::interprocess::allocator<ValueType, boost::interprocess::managed_shared_memory::segment_manager>; 
using SharedMemoryMap = boost::interprocess::map<shm_string, shm_string, std::less<KeyType>, ShmemAllocator>; 

... 

// create a new shared memory segment 2K size 
managed_shared_memory managed_shm(open_only, "sharedmemname"); 

//Initialize the shared memory STL-compatible allocator 
ShmemAllocator alloc(managed_shm.get_segment_manager()); 

auto pSharedNVPairs = managed_shm.find<UtlSharedIPCWrapper< 
    SharedMemoryMap>>("NameValuePairs").first; 

我的问题是如何改变下面的模板类定义通过收集::值类型的作为参数,而不是单独地读取整个地图作为一个操作通过pSharedNVPairs->getSharedData()更新的T并通过pSharedNVPairs->setSharedData(*pSharedNVPairs)再次将其写回共享内存。我知道这对于不是集合的类型会有所不同,因此一些模板元编程魔术将不得不被执行,如果选择性启用等,但我想有一种方法添加到我的类,沿着

// I don't know the correct signature here 
void setSharedDataValue(const T::value_type& rSharedDataValue) { 
    boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mMutex); 
    ... not sure what to do here to update the collection 
} 

template<typename T> 
struct UtlSharedIPCWrapper { 
private: 
    using upgradable_mutex_type = boost::interprocess::interprocess_upgradable_mutex; 

    mutable upgradable_mutex_type mMutex; 
    /*volatile*/ T mSharedData; 
public: 
    // explicit constructor used to initialize directly from existing memory 
    explicit UtlSharedIPCWrapper(const T& rInitialValue) 
     : mSharedData(rInitialValue) 
    {} 

    T getSharedData() const { 
     boost::interprocess::sharable_lock<upgradable_mutex_type> lock(mMutex); 
     return mSharedData; 
    } 

    void setSharedData(const T& rSharedData) { 
     boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mMutex); 
     // update the shared data copy mapped - scoped locked used if exception thrown 
     // a bit like the lock guard we normally use 
     this->mSharedData = rSharedData; 
    } 
}; 
+0

我中有你真要问怀疑 - 多 - 不同的问题 - 在这种情况下,请通过扩展的代码示例是自containted – sehe

+0

澄清@ sehe我同意这个问题可能有点模糊,但是我确实有两个不同的情况,我想要在上面工作,一个是采取一个简单的整数类型,另一个是一个集合。我想知道如何使用模板魔法使适当的模板专业化工作 – johnco3

+0

@Sehe使用GCC 4.9.1我得到下面的错误。它使我认为如果模板的实例化不是整数或更具体的类型容器,我真的需要一些std :: enable_if机制来有效地评论整个方法 - 您怎么看? - 我不知道如何做到这一点。这是一个缩写的编译器错误。在从'struct UtlSharedIPCWrapper '的In实例化中包含的文件中: ALBFDaemon.cpp:347:31:从这里需要UtlSharedIPCWrapper.h:49:10:error:'long long unsigned int'不是一个类,结构体或union类型 – johnco3

回答

1

为什么不

// I don't know the correct signature here 
void setSharedDataValue(const typename T::value_type& rSharedDataValue)  { 
    boost::interprocess::scoped_lock<upgradable_mutex_type> lock(mMutex); 
    mSharedData.insert(rSharedDataValue); 
} 
+0

我尝试了上面的visual studio 2015,但是得到:C2825:'T':必须是类或名称空间,后跟'::'。不确定的语法是。当然,提供T :: value_type的意图正是我想提供的,因为在这种情况下,我需要将std :: pair元素插入到std :: map中,但我不确定在此代码中会发生什么在这种情况下,我有一个UtlSharedIPCWrapper - 这种方法会消失 - 这是一个我不清楚的整个元编程的事情。 – johnco3

+1

MSVC具有一些与模板中的名称查找相关的非标准行为。你能否提供一个/ selfcontained /例子来重现你的问题? – sehe