Initial commit udt_conversion_example.cpp

This commit is contained in:
Beman
2013-05-14 14:54:11 -04:00
parent c16fd1384f
commit 72880a1416
3 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
// udt_conversion_example.cpp --------------------------------------------------------//
// Copyright Beman Dawes 2013
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
//--------------------------------------------------------------------------------------//
#define _CRT_SECURE_NO_WARNINGS
#include <boost/endian/detail/disable_warnings.hpp>
#include <boost/endian/converters.hpp>
#include <iostream>
#include <cstring>
using namespace boost::endian;
using std::cout;
using std::endl;
using boost::int32_t;
class UDT
{
public:
UDT() : id_(0), value_(0.0) {desc_[0] = '\0';}
UDT(int32_t id, float value, const char* desc) : id_(id), value_(value)
{
std::strncpy(desc_, desc, sizeof(desc_)-1);
desc_[sizeof(desc_)-1] = '\0';
}
int32_t id() const {return id_;}
float value() const {return value_;}
const char* desc() const {return desc_;}
void id(int32_t x) {id_ = x;}
void value(float v) {value_ = v;}
void desc(const char* s)
{
std::strncpy(desc_, s, sizeof(desc_)-1);
desc_[sizeof(desc_-1)] = '\0';
}
friend void reverse(UDT&);
private:
int32_t id_;
float value_;
char desc_[56]; // '/0'
};
void reverse(UDT& x)
{
reverse(x.id_);
reverse(x.value_);
}
int main(int, char* [])
{
UDT x(1, 1.2345f, "Bingo!");
cout << std::hex;
cout << x.id() << ' ' << x.value() << ' ' << x.desc() << endl;
reverse(x);
cout << x.id() << ' ' << x.value() << ' ' << x.desc() << endl;
reverse(x);
cout << x.id() << ' ' << x.value() << ' ' << x.desc() << endl;
big_endian(x);
cout << x.id() << ' ' << x.value() << ' ' << x.desc() << endl;
}
#include <boost/endian/detail/disable_warnings_pop.hpp>

View File

@@ -17,6 +17,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "converter_test", "converter
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pair_test", "pair_test\pair_test.vcxproj", "{5223CCD9-F1D4-4778-9BFD-242533DFE380}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "udt_conversion_example", "udt_conversion_example\udt_conversion_example.vcxproj", "{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@@ -53,6 +55,10 @@ Global
{5223CCD9-F1D4-4778-9BFD-242533DFE380}.Debug|Win32.Build.0 = Debug|Win32
{5223CCD9-F1D4-4778-9BFD-242533DFE380}.Release|Win32.ActiveCfg = Release|Win32
{5223CCD9-F1D4-4778-9BFD-242533DFE380}.Release|Win32.Build.0 = Release|Win32
{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}.Debug|Win32.ActiveCfg = Debug|Win32
{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}.Debug|Win32.Build.0 = Debug|Win32
{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}.Release|Win32.ActiveCfg = Release|Win32
{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{27A53564-D32B-4A32-8A6E-2F3BD252EEBA}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>udt_conversion_example</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\common.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="..\common.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\..\example\udt_conversion_example.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>