Modern C++ 11/14 – C# alike events

For a project of mine I needed a functionality like that found in C# events. Since my C++ is a bit rusty 🙂 I looked up some options and found this example which I edited to support variadic templates.

The original implementation(now modified with variadic templates) from: http://geekswithblogs.net/raccoon_tim/archive/2011/09/28/lambdas-and-events-in-c.aspx

template<typename… T1>
class Event
{
public:
typedef std::function<void(T1…)> Func;

public:
void Call(T1 …arg)
{
for (auto i = m_handlers.begin(); i != m_handlers.end(); i++)
{
(*i)(arg…);
}
}

void operator ()(T1 …arg)
{
Call(arg…);
}

Event& operator += (Func f)
{
if (typeid(f) != typeid(Func))
throw std::bad_function_call(“Error in type”);

m_handlers.push_back(f);
return *this;
}

Event& operator -= (Func f)
{
for (auto i = m_handlers.begin(); i != m_handlers.end(); i++)
{
if ((*i).target<void(T1)>() == f.target<void(T1)>())
{
m_handlers.erase(i);
break;
}
}

return *this;
}

private:
std::vector<Func> m_handlers;
};

 

To use the event class in you code:

Example 1 – Single parameter:

int stackValue = 200; Event<int> e; e += [=](int i) { printf( “%d\n”, i + stackValue ); }; e += [&](int i) { printf( “%d\n”, i + stackValue ); stackValue += 100; }; e += [&stackValue](int i) { printf( “%d\n”, i + stackValue ); }; e += [stackValue](int i) { printf( “%d\n”, i + stackValue ); }; e( 100 );

Example 2 – Multiple Parameters:
int stackValue = 200;
float stackValue2 = 12;
double stackValue3 = 200;
Event<int, float, double> e;
e += [=](int i, float j, double k) { printf(“%d\n”, i + stackValue + (int)j + (int)k); };
e += [&](int i, float j, double k) { printf(“%d\n”, i + stackValue + (int)j + (int)k); stackValue += 100; };
e += [&stackValue](int i, float j, double k) { printf(“%d\n”, i + stackValue + (int)j + (int)k); };
e += [stackValue](int i, float j, double k) { printf(“%d\n”, i + stackValue + (int)j + (int)k); };

e(100, stackValue2, 53);

Resources: SharePoint Site Definitions, Site/Web Templates and Provisioning

Creating a Custom SharePoint 2007 Portal Site Definition using the PortalProvisioningProvider Class – Paul Ballard’s WebLog

Understanding Onet.xml Files

Site Definition (Onet.xml) Files

Understanding Web*Temp.xml Files

Site Schema

Exploring The SharePoint 12 Hive :TEMPLATE Directory. – Windows Live

Supported and unsupported scenarios for working with custom site definitions and custom area definitions in SharePoint 2003, 2007 and 2010

Save a site as a site template – SharePoint Server – Microsoft Office

Adding a ‘Save site as template’ Link to Site Settings in WSS v3/MOSS 2007 using a CustomAction Feature — SharePoint Solutions Team Blog

How To Tell Which Template is in Use in SharePoint

Know the site template used for the SharePoint site | SharePoint 2010, SharePoint, C-sharp, ASP.Net, JQuery, SQL Server Solutions

SharePoint 2010 and web templates – Vesa “vesku” Juvonen – Site Home – MSDN Blogs

Understanding webtemp*.xml – Bill Baer – Site Home – TechNet Blogs

Extending the Project Workspace Template

Develop new custom site definitions and create upgrade definition files (Office SharePoint Server)

Site Definitions in SharePoint 2007 – Syrinx on SharePoint

MOSS 2007 – Save site as a template missing – The SharePoint Farmer’s Almanac

Finding site template names and ID’s in SharePoint using PowerShell | Get-SPScripts : PowerShell Scripts for SharePoint

The template you have chosen is invalid or cannot be found. – fengzhimei – 博客园

Microsoft SharePoint: Find the SharePoint web Template ID

SharePoint site creation using SPWebProvisioningProvider « Jamesemann’s Blog

WebTemplate Element (Upgrade)

SharePoint 2010 and web templates – Vesa “vesku” Juvonen – Site Home – MSDN Blogs

Modules

SPSite – The Web application at <address> could not be found – SharePoint