How to use std::optional in C++?

This wrapper is used to represent nullable types.

Example

#include <iostream>
#include <optional>

void f1(std::optional<std::string> v)
{
  std::cout << v.has_value() << std::endl;
}

int main()
{
  f1(std::nullopt);
  f1("assigned");
  return 0;
}

To build the example in VS C++17 should be enabled.

How to enable C++17 in Visual Studio
How to enable C++17 in Visual Studio.

How to enable C++17 compiling in Visual Studio?

std::optional

Read more

How to create data serializer/deserializer according to XSD-description with Visual Studio?

Step 1

Create XSD-definition of classes containing data.

Example

<xs:complexType name="ParagraphExtractorType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

Step 2

Create XSD-definition of container class representing entire data.

Example

<xs:element name="HTMLCleanupConfig">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="TextProcessorConfig" 
        type="htmlCleanup:TextProcessorType">
      </xs:element>
      <xs:element name="ParagraphExtractorConfig" 
        type="htmlCleanup:ParagraphExtractorType">
      </xs:element>
      <xs:element name="SpecialHTMLRemoverConfig" 
        type="htmlCleanup:SpecialHTMLRemoverType">
      </xs:element>
      <xs:element name="InnerTagRemoverConfig" 
        type="htmlCleanup:InnerTagRemoverType">
      </xs:element>
      <xs:element name="TagWithTextRemoverConfig" 
        type="htmlCleanup:TagWithTextRemoverType">
      </xs:element>
      <xs:element name="URLFormatterConfig" 
        type="htmlCleanup:URLFormatterType">
      </xs:element>
      <xs:element name="TextFormatterConfig" 
        type="htmlCleanup:TextFormatterType">
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Step 3

Generate classes from XSD-schema.

How to create classes from XSD-schema with Visual Studio?

Example

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, 
  Namespace="urn:xmlns:HTMLCleanupConfig")]
[System.Xml.Serialization.XmlRootAttribute(
  Namespace="urn:xmlns:HTMLCleanupConfig", IsNullable=false)]
public partial class HTMLCleanupConfig {
  private TextProcessorType textProcessorConfigField;
  private ParagraphExtractorType paragraphExtractorConfigField;
  private SpecialHTMLRemoverType specialHTMLRemoverConfigField;
  private InnerTagRemoverType innerTagRemoverConfigField;
  private TagWithTextRemoverType tagWithTextRemoverConfigField;
  private URLFormatterType uRLFormatterConfigField;
  private TextFormatterType textFormatterConfigField;

/// <remarks/>
public TextProcessorType TextProcessorConfig {
  get {
    return this.textProcessorConfigField;
  }
  set {
    this.textProcessorConfigField = value;
  }
}

/// <remarks/>
public ParagraphExtractorType ParagraphExtractorConfig {
  get {
    return this.paragraphExtractorConfigField;
  }
  set {
    this.paragraphExtractorConfigField = value;
  }
}

/// <remarks/>
public SpecialHTMLRemoverType SpecialHTMLRemoverConfig {
  get {
    return this.specialHTMLRemoverConfigField;
  }
  set {
    this.specialHTMLRemoverConfigField = value;
  }
}

/// <remarks/>
public InnerTagRemoverType InnerTagRemoverConfig {
  get {
    return this.innerTagRemoverConfigField;
  }
  set {
    this.innerTagRemoverConfigField = value;
  }
}

/// <remarks/>
public TagWithTextRemoverType TagWithTextRemoverConfig {
  get {
    return this.tagWithTextRemoverConfigField;
  }
  set {
    this.tagWithTextRemoverConfigField = value;
  }
}

/// <remarks/>
public URLFormatterType URLFormatterConfig {
  get {
    return this.uRLFormatterConfigField;
  }
  set {
    this.uRLFormatterConfigField = value;
  }
}

/// <remarks/>
public TextFormatterType TextFormatterConfig {
  get {
    return this.textFormatterConfigField;
  }
  set {
    this.textFormatterConfigField = value;
  }
}
}

Step 4

Create class implementing methods for serialization / deserialization of container object.

Example

public void Deserialize(string fileName)
{
  var config = new HTMLCleanupConfig();
  using (var reader = new StreamReader(fileName))
  {
    var serializer = new XmlSerializer(typeof(HTMLCleanupConfig));
    config = (HTMLCleanupConfig)serializer.Deserialize(reader);
  }
}

public void Serialize(string fileName)
{
  var config = new HTMLCleanupConfig();
  using (var writer = new StreamWriter(fileName))
  {
    var serializer = new XmlSerializer(typeof(HTMLCleanupConfig));
    serializer.Serialize(writer, config);
  }
}

Examples are taken from pdf service .

 

How to create classes from XSD-schema with Visual Studio?

Open Developer Command Prompt for Visual Studio and execute

> xsd xsd_file_name /classes /namespace:resulting_namespace

For example

> xsd config.xsd /classes /namespace:HTMLCleanup.Config

As result you will get config.cs containing set of classes having structure according to XSD-description.

XML Schema Definition Tool (Xsd.exe)

Comparison of XSD Code Generators

Xsd2Code

How to enable LF line endings in Visual Studio

Step 1

Open “Tools” -> “Customize” -> “Commands” and select the “File” item.

Visual Studio menu customization
Visual Studio menu customization.

Step 2

Press “Add Command…” button and add “Advanced Save Options…” item to the “File” menu.

Adding new command to Visual Studio menu
Adding new command to Visual Studio menu.

Step 3

Select “Advanced Save Options” menu and set up the “Line endings” drop-down.

Selecting line endings in Visual Studio
Selecting line endings in Visual Studio.

 

How to fill build number with Visual Studio

Step 1

Create resulting files, include them into project. Example below shows cshtml files but they can be text files of any type. Create files with names build and version. Initialize them with desired starting build value and version string.

Step 2

Create bat-file _update_version.bat and add it to the pre-build event in project properties of Visual Studio.

cd $(ProjectDir)
_update_version.bat

An example of bat-file.

@set _date=%date%
@set _time=%time:~0,8%
@set _year=%_date:~10,4%

@rem reads build number from file
@set /p _build=< build  
@rem increments build number 
@set /a _build=%_build%+1  
@rem saves build number into file 
@echo %_build%>build

@rem fills resulting files
@echo %_build%>Views/Shared/Build.cshtml
@echo %_year%>Views/Shared/Year.cshtml
@echo %date% %_time%>Views/Shared/Date.cshtml

@rem cleans variables
@set _build=
@set _date=
@set _time=
@set _year=

Step 3

Rebuild project.

How to fill revision number with TortoiseSVN and Visual Studio

How to fill revision number with TortoiseSVN and Visual Studio

Step 1

Create template file and resulting file, include resulting file into project. Example below shows cshtml files but they can be text files of any type.

Step 2

Create bat-file _update_version.bat and add it to the pre-build event in project properties of Visual Studio.

cd $(ProjectDir)
_update_version.bat

Use subwcrev utility provided by TortoiseSvn.

An example of bat-file.

subwcrev . Views\Mobile\AboutTmpl.cshtml Views\Mobile\About.cshtml
set ERRORLEVEL=0

Step 3

Put into template files keywords which will be replaced by subwcrev utility

Rev. $WCREV$
Last updated $WCDATE=%d %b %Y at %H:%M:%S$

Step 4

Rebuild project.

The SubWCRev Program

How to fill build number with Visual Studio