Skip to content

Commit

Permalink
AdsLib: OOI: fix AdsHandle to be usable with VS2015
Browse files Browse the repository at this point in the history
fixes #38
  • Loading branch information
pbruenn committed Sep 21, 2016
1 parent a4f2064 commit b1d306e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
43 changes: 27 additions & 16 deletions AdsLib/AdsClient/AdsHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@

#include "AdsException.h"
#include "..\AdsLib.h"
#include <functional>
#include <memory>

class AdsHandle {
static void ReleaseHandleDummy(const AmsAddr address, long port, uint32_t* handle) {}
static void ReleaseHandle(const AmsAddr address, long port, uint32_t* handle)
{
uint32_t error = AdsSyncWriteReqEx(
port,
&address,
ADSIGRP_SYM_RELEASEHND, 0,
sizeof(*handle), handle
);
struct HandleDeleter {
HandleDeleter(const AmsAddr __address = AmsAddr {}, long __port = 0) : address(__address),
port(__port)
{}

void operator()(uint32_t* handle)
{
uint32_t error = 0;
if (port) {
error = AdsSyncWriteReqEx(
port,
&address,
ADSIGRP_SYM_RELEASEHND, 0,
sizeof(*handle), handle
);
}
delete handle;

if (error) {
throw AdsException(error);
}
}

using AdsHandleGuard =
std::unique_ptr<uint32_t,
decltype(std::bind(& ReleaseHandle, std::declval<const AmsAddr>(),
std::declval<long>(), std::placeholders::_1))>;
private:
const AmsAddr address;
const long port;
};

class AdsHandle {
using AdsHandleGuard = std::unique_ptr<uint32_t, HandleDeleter>;

static AdsHandleGuard GetHandle(const AmsAddr address, long port,
const std::string& symbolName)
Expand All @@ -44,13 +55,13 @@ class AdsHandle {
throw AdsException(error);
}

return AdsHandleGuard {new uint32_t {handle}, std::bind(&ReleaseHandle, address, port, std::placeholders::_1)};
return AdsHandleGuard {new uint32_t {handle}, HandleDeleter {address, port}};
}

AdsHandleGuard m_Handle;
public:
AdsHandle(uint32_t offset)
: m_Handle{new uint32_t {offset}, std::bind(&ReleaseHandleDummy, AmsAddr {}, 0L, std::placeholders::_1)}
: m_Handle{new uint32_t {offset}, HandleDeleter {}}
{}

AdsHandle(const AmsAddr address, long port,
Expand Down
6 changes: 3 additions & 3 deletions AdsLib/AdsLib.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -18,13 +18,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "AdsLib.h"

#include <iostream>
#include <array>

static void NotifyCallback(const AmsAddr* pAddr, const AdsNotificationHeader* pNotification, uint32_t hUser)
{
Expand Down
6 changes: 3 additions & 3 deletions example/example.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -18,13 +18,13 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v120</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
Expand Down

0 comments on commit b1d306e

Please sign in to comment.