Skip to content

Instantly share code, notes, and snippets.

@hutorny
Created April 4, 2017 11:11
Show Gist options
  • Save hutorny/629b2364c70d526d5045ec2c3ddf9653 to your computer and use it in GitHub Desktop.
Save hutorny/629b2364c70d526d5045ec2c3ddf9653 to your computer and use it in GitHub Desktop.
Cascaded Configuration Sets for C++1y
/************************************************************************/
/* anotherclass.ccs definition of configuration options and sets for */
/* MyAnotherClass */
/************************************************************************/
#include <ccs>
#pragma once
class MyAnotherClass;
namespace configuration {
template<>
struct Configuration<MyAnotherClass, Default> {
static constexpr short AnotherValue = 100;
typedef unsigned MyType;
};
template<>
struct Configuration<MyAnotherClass, Debug> :
Configuration<MyAnotherClass, Default> {
static constexpr short AnotherValue = 300;
typedef unsigned short MyType;
};
}
#include "configuration.h"
#pragma once
class MyAnotherClass {
public:
using config = configuration::Configuration<MyAnotherClass>;
using MyType = config::MyType;
static constexpr int MyValue = config::MyValue;
static constexpr bool MyOption = config::MyOption;
static constexpr bool NewOption = config::NewOption;
};
/************************************************************************/
/* ccs core configuration definitions */
/************************************************************************/
/* please visist this link for motivations and details */
/* http://hutorny.in.ua/research/cascaded-configuration-sets-for-c1y */
#pragma once
namespace configuration {
/* predefined configuration set identifiers */
class Default;
class Release;
class Debug;
/* selector of active configuration set */
template<class UserClass>
struct Selector {
/* default configuration set */
typedef Default type;
};
/* Declaration of main configuration template */
template<class UserClass, typename = typename Selector<UserClass>::type>
struct Configuration;
}
/************************************************************************/
/* configuration.h user's instantiation of configuration sets */
/************************************************************************/
#include <myclass.ccs>
#include <anotherclass.ccs>
#pragma once
class AltConfig; /* ID of an alternate configuration */
namespace configuration {
typedef AltConfig ActiveConfig;
//typedef Release ActiveConfig;
template<>
struct Selector<MyClass> {
typedef ActiveConfig type; /* configuration set selector */
};
/* Alternate configuration set */
template<>
struct Configuration<MyClass, AltConfig> :
Configuration<MyClass, Release> {
static constexpr int MyValue = 400;
};
}
#include <iostream>
#include <myclass.hpp>
int main() {
std::cout
<< "MyValue =" << MyClass::MyValue << std::endl
<< "MyOption =" << MyClass::MyOption << std::endl
<< "NewOption=" << MyClass::NewOption << std::endl;
}
/************************************************************************/
/* myclass.ccs definition of configuration options and sets for MyClass*/
/************************************************************************/
#include <ccs>
#pragma once
class MyClass;
namespace configuration {
template<>
struct Configuration<MyClass, Default> {
static constexpr int MyValue = 100;
static constexpr bool MyOption = true;
static constexpr bool NewOption = true;
typedef signed short MyType;
static bool myFunc(const char*,const char*);
};
template<>
struct Configuration<MyClass, Release> :
Configuration<MyClass, Default> {
static constexpr int MyValue = 200;
static constexpr bool MyOption = false;
};
template<>
struct Configuration<MyClass, Debug> :
Configuration<MyClass, Default> {
static constexpr int MyValue = 100;
static constexpr bool MyOption = true;
};
}
#include "configuration.h"
#pragma once
class MyClass {
public:
using config = configuration::Configuration<MyClass>;
using MyType = config::MyType;
static constexpr int MyValue = config::MyValue;
static constexpr bool MyOption = config::MyOption;
static constexpr bool NewOption = config::NewOption;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment