ITIL: potentially provide a defined and structured argument for IT strategy and using IT to support and promote the business - as opposed to just being a cost centre.
http://en.wikipedia.org/wiki/Information_Technology_Infrastructure_Library
This blog serves as a dumping ground for my own interests. On it you will find anything which I want to keep track of; links, articles, tips and tricks. Mostly it focuses on C++, Javascript and HTML, linux and performance.
Wednesday, 14 May 2014
Tuesday, 13 May 2014
angular-fullstack yeoman generator - use less version of bootstrap
I use the angular-fullstack yeoman generator, but it installs the compiled bootstrap css and I want to use the less version.
Here is how I hack the yeomen generated source to change it to use the less version of bootstrap:
# install less and less grunt task:
sudo npm install -g less
# rename the old app/styles/main.css to app/styles/inputs.less:
mv app/styles/main.css app/styles/inputs.less
# create less grunt tasks in Gruntfile.js:
files: ['<%= yeoman.app %>/styles/{,*/}*.{css,less}'],
# add 'less:dev' to tasks in the styles section of the watch grunt task:
tasks: ['less:dev', 'newer:copy:styles', 'autoprefixer']
Here is how I hack the yeomen generated source to change it to use the less version of bootstrap:
# install less and less grunt task:
sudo npm install -g less
npm install --save-dev grunt-contrib-less
# create app/styles/main.less file:
@import "../bower_components/bootstrap/less/bootstrap.less";
@import "inputs.less";
@import "../bower_components/bootstrap/less/utilities.less";
# rename the old app/styles/main.css to app/styles/inputs.less:
mv app/styles/main.css app/styles/inputs.less
# create less grunt tasks in Gruntfile.js:
// build our css
less: {
dev: {
files: {
"<%= yeoman.app %>/styles/main.css": "<%= yeoman.app %>/styles/main.less"
}
},
dist: {
options: {
cleancss: true
},
files: {
"<%= yeoman.dist %>/styles/main.css": "<%= yeoman.app %>/styles/main.less"
}
}
},
# add 'less' file extension to files in the styles section of the watch grunt task:
# add 'less:dev' to tasks in the styles section of the watch grunt task:
# also, add 'less:dev' to the debug and serve grunt tasks, and 'less:dist' to the build grunt task
# add exclude entry to the bower-install grunt task to prevent it from being injected into index.html:
exclude: [ 'bower_components/bootstrap/dist/css/bootstrap.css' ]
exclude: [ 'bower_components/bootstrap/dist/css/bootstrap.css' ]
# add generated css to .gitignore:
echo app/styles/main.css >> .gitignore
echo app/styles/main.css >> .gitignore
Thursday, 1 May 2014
Solving SFINAE issues when you have overlapping conditions
Sometimes we have function templates which we want to use SFINAE on, but some of them have overlapping conditions, creating ambiguity
template<unsigned N, enable_if_t<is_multiple_of<N, 3>>...>
void print_fizzbuzz(){ std::cout << "fizz\n"; }
template<unsigned N, enable_if_t<is_multiple_of<N, 5>>...>
void print_fizzbuzz(){ std::cout << "buzz\n"; }
template<unsigned N, enable_if_t<is_multiple_of<N, 15>>...> // this is ambiguous
void print_fizzbuzz(){ std::cout << "fizzbuzz\n"; }
By using derived-to-base conversions we can create a total ordering for selecting SFINAE overloads.
That is, we resolve ambiguity by using the following inheritance hierarchy:
template<unsigned I> struct choice : choice<I+1>{};
template<class C, class T = int>
using enable_if_t = typename std::enable_if<C::value, T>::type;
template<int N, int M>
struct is_multiple_of : std::integral_constant<bool, N % M == 0>{};
//-------------------------------
template<unsigned I> struct choice : choice<I+1>{};
template<> struct choice<10>{}; // suitably high terminating condition
struct otherwise{ otherwise(...){} };
struct select_overload : choice<0>{};
//-------------------------------
template<unsigned N, enable_if_t< is_multiple_of<N, 15> >...>
void print_fizzbuzz(choice<0>) { std::cout << "fizzbuzz\n"; }
template<unsigned N, enable_if_t< is_multiple_of<N, 3> >...>
void print_fizzbuzz(choice<1>) { std::cout << "fizz\n"; }
template<unsigned N, enable_if_t< is_multiple_of<N, 5> >...>
void print_fizzbuzz(choice<2>) { std::cout << "buzz\n"; }
template<unsigned N>
void print_fizzbuzz(otherwise){ std::cout << N << "\n"; }
template<unsigned N = 1>
void do_fizzbuzz()
{
print_fizzbuzz<N>(select_overload{});
do_fizzbuzz<N+1>();
}
template<>
void do_fizzbuzz<50>()
{
print_fizzbuzz<50>(select_overload{});
}
//-------------------------------
int main()
{
do_fizzbuzz();
}
template<unsigned N, enable_if_t<is_multiple_of<N, 3>>...>
void print_fizzbuzz(){ std::cout << "fizz\n"; }
template<unsigned N, enable_if_t<is_multiple_of<N, 5>>...>
void print_fizzbuzz(){ std::cout << "buzz\n"; }
template<unsigned N, enable_if_t<is_multiple_of<N, 15>>...> // this is ambiguous
void print_fizzbuzz(){ std::cout << "fizzbuzz\n"; }
By using derived-to-base conversions we can create a total ordering for selecting SFINAE overloads.
That is, we resolve ambiguity by using the following inheritance hierarchy:
template<unsigned I> struct choice : choice<I+1>{};
choice<0> has a higher ordering than choice<1>, and we can therefore use choice<0> as a function parameter to make is_multiple_of<N, 15> a better overload, thereby resolving the ambiguity.
The complete fizzbuzz example:
#include <type_traits>
#include <iostream>template<class C, class T = int>
using enable_if_t = typename std::enable_if<C::value, T>::type;
template<int N, int M>
struct is_multiple_of : std::integral_constant<bool, N % M == 0>{};
//-------------------------------
template<unsigned I> struct choice : choice<I+1>{};
template<> struct choice<10>{}; // suitably high terminating condition
struct otherwise{ otherwise(...){} };
struct select_overload : choice<0>{};
//-------------------------------
template<unsigned N, enable_if_t< is_multiple_of<N, 15> >...>
void print_fizzbuzz(choice<0>) { std::cout << "fizzbuzz\n"; }
template<unsigned N, enable_if_t< is_multiple_of<N, 3> >...>
void print_fizzbuzz(choice<1>) { std::cout << "fizz\n"; }
template<unsigned N, enable_if_t< is_multiple_of<N, 5> >...>
void print_fizzbuzz(choice<2>) { std::cout << "buzz\n"; }
template<unsigned N>
void print_fizzbuzz(otherwise){ std::cout << N << "\n"; }
template<unsigned N = 1>
void do_fizzbuzz()
{
print_fizzbuzz<N>(select_overload{});
do_fizzbuzz<N+1>();
}
template<>
void do_fizzbuzz<50>()
{
print_fizzbuzz<50>(select_overload{});
}
//-------------------------------
int main()
{
do_fizzbuzz();
}
This excellent technique by Xeo, as described here
Using function template default parameters to elegantly create SFINAE overloads
Having read Remastered enable_if, some implementation was left as an exercise for the reader.
The article explains how to make use of function template default parameters to elegantly create SFINAE overloads.
Below is my implementation.
#include <iostream>
#include <type_traits>
// true iff all conditions ::values are true
template <typename Head, typename... Tail>
struct all
{
static constexpr bool value = Head::value && all<Tail...>::value;
};
template <typename Head>
struct all<Head>
{
static constexpr bool value = Head::value;
};
//---------------------------------
// scoped enum to allow for differentiation between enable/disable
namespace detail { enum class enabler {}; }
template <typename... Condition>
using enable_if_t = typename std::enable_if<all<Condition...>::value, detail::enabler>::type;
template <typename... Condition>
using disable_if_t = typename std::enable_if<!all<Condition...>::value>::type;
//---------------------------------
// example function showing SFINAE overloads
template <typename T,
enable_if_t< std::is_arithmetic<T>
, std::is_integral<T>
>...>
T twice(T t)
{
return 2*t;
}
template <typename T,
disable_if_t< std::is_arithmetic<T>
, std::is_integral<T>
>...>
T twice(T t)
{
return t + t;
}
//---------------------------------
int main()
{
std::cout << twice(5) << std::endl;
std::cout << twice(std::string("Hello world")) << std::endl;
return 0;
}
Read more here
The article explains how to make use of function template default parameters to elegantly create SFINAE overloads.
Below is my implementation.
#include <iostream>
#include <type_traits>
// true iff all conditions ::values are true
template <typename Head, typename... Tail>
struct all
{
static constexpr bool value = Head::value && all<Tail...>::value;
};
template <typename Head>
struct all<Head>
{
static constexpr bool value = Head::value;
};
//---------------------------------
// scoped enum to allow for differentiation between enable/disable
namespace detail { enum class enabler {}; }
template <typename... Condition>
using enable_if_t = typename std::enable_if<all<Condition...>::value, detail::enabler>::type;
template <typename... Condition>
using disable_if_t = typename std::enable_if<!all<Condition...>::value>::type;
//---------------------------------
// example function showing SFINAE overloads
template <typename T,
enable_if_t< std::is_arithmetic<T>
, std::is_integral<T>
>...>
T twice(T t)
{
return 2*t;
}
template <typename T,
disable_if_t< std::is_arithmetic<T>
, std::is_integral<T>
>...>
T twice(T t)
{
return t + t;
}
//---------------------------------
int main()
{
std::cout << twice(5) << std::endl;
std::cout << twice(std::string("Hello world")) << std::endl;
return 0;
}
Read more here
Tuesday, 29 April 2014
Exiting recursive function templates without using helper class templates and partial specialisation
Contrived examples follow, but they serve to illustrate partial specialisation of class templates vs exiting recursive function templates using a branch.
The old way: use a helper class template, partially specialise it with the recursion exit case, and call a static function on this class template from a function template:
#include <iostream>
// helper class template
template<typename T, unsigned idx>
struct foo_impl
{
static void fn(T t)
{
std::cout << t << " " << idx << std::endl;
foo_impl<T, idx - 1>::fn(t); // recursively call fn
}
};
// partial specialisation of helper class template for exit case
template<typename T>
struct foo_impl<T, 0>
{
static void fn(T) {} // do nothing exit case
};
// function template which uses helper class templates
template<unsigned idx, typename T>
void foo(T t)
{
foo_impl<T, idx>::fn(t);
}
int main()
{
foo<5>("Hello world");
return 0;
}
The old way: use a helper class template, partially specialise it with the recursion exit case, and call a static function on this class template from a function template:
#include <iostream>
// helper class template
template<typename T, unsigned idx>
struct foo_impl
{
static void fn(T t)
{
std::cout << t << " " << idx << std::endl;
foo_impl<T, idx - 1>::fn(t); // recursively call fn
}
};
// partial specialisation of helper class template for exit case
template<typename T>
struct foo_impl<T, 0>
{
static void fn(T) {} // do nothing exit case
};
// function template which uses helper class templates
template<unsigned idx, typename T>
void foo(T t)
{
foo_impl<T, idx>::fn(t);
}
int main()
{
foo<5>("Hello world");
return 0;
}
The new way: have the special case branch in the function template itself, and recursively call the function template from itself.
To prevent the compiler from barfing when instantiating the recursive path, the trick is to recursively call the function template with the same parameters for the special case. Even though this code path will never actually execute, it is needed so the compiler can parse the template.
#include <iostream>
template<unsigned idx, typename T>
void foo(T t)
{
if (idx == 0) // special exit case
return;
std::cout << t << " " << idx << std::endl;
// recursively call the function
foo<idx - (idx ? 1 : 0)>(t); // note recursion returns itself in special exit case (code path will actually never be reached)
}
int main()
{
foo<5>("Hello world");
return 0;
}
template<unsigned idx, typename T>
void foo(T t)
{
if (idx == 0) // special exit case
return;
std::cout << t << " " << idx << std::endl;
// recursively call the function
foo<idx - (idx ? 1 : 0)>(t); // note recursion returns itself in special exit case (code path will actually never be reached)
}
int main()
{
foo<5>("Hello world");
return 0;
}
Friday, 11 April 2014
Create a local git repo, a remote github repo and sync them from the command line
Create a local repo
follow the instructions here.
Install hub
# install dependencies
sudo apt-get install rake
# clone repo
cd /tmp
git clone https://github.com/github/hub.git
# install hub
cd hub
sudo rake install prefix=/usr/local
follow the instructions here.
Install hub
# install dependencies
sudo apt-get install rake
# clone repo
cd /tmp
git clone https://github.com/github/hub.git
# install hub
cd hub
sudo rake install prefix=/usr/local
# alias git to hub
echo 'alias git=hub' >> ~/.bashrc
. !$
# check it works
git version
# expected output:
# git version 1.8.3.2
# hub version 1.12.0-6-g8150ddb
Create a remote repo on github
# create a repo with the name of the current directory
git create -d "My description"
# push to github
git push origin master
# create a repo with the name of the current directory
git create -d "My description"
# push to github
git push origin master
Monday, 31 March 2014
MEAN stack build system
MEAN stack build system
The MEAN stack is [M]ongo, [E]xpress, [A]ngular, [N]ode.Back end:
The server is built using Express, which is built on top of Node.
The database used by the server is MongoDB.
Front end:
The client is built using Angular.
The less version of Bootstrap is used for CSS.
I use Yeoman and the angular-fullstack generator to generate my MEAN stack scaffold.
yo angular-fullstack [app]
Development tools: npm is used for the server dependencies; these are listed in
package.json
Bower is used for the client dependencies; these are listed in
bower.json
Grunt is used for the build system. The configuration is stored in
Gruntfile.js
The file system structure is as follows:
site/
app/ # client source code
lib/ # server source code
test/ # tests
public/ # client production build destination
Gruntfile.js # Grunt configuration
package.json # server dependencies
bower.json # client dependencies
server.js # Express server entry-point
Grunt plugins:grunt-bower-install:
When new dependencies are added with
bower
, grunt-bower-install will automatically inject their css and js into index.html
.In the
grunt.initConfig
block of Gruntfile.js
:'bower-install': {
app: {
html: '<%= yeoman.app %>/index.html',
ignorePath: '<%= yeoman.app %>/'
}
},
grunt-file-blocks: When new front end components added in the source tree, grunt-file-blocks will automatically inject the javascript into
index.html
and the less into main.less
.In the
grunt.initConfig
block of Gruntfile.js
:fileblocks: {
options: {
removeFiles: true,
templates: {
less: '@import \'${file}\';'
}
},
js: {
src: 'app/index.html',
blocks: {
components: { cwd: 'app', src: 'components/**/*.js' }
}
},
less: {
src: 'app/css/main.less',
blocks: {
components: { cwd: 'app', src: 'components/**/*.less' }
}
}
},
Development workflow:File are monitored for changes and the front or back end are reloaded as required.
nodemon
and LiveReload
are run concurrently:In the
grunt.initConfig
block of Gruntfile.js
:concurrent: {
dev: {
tasks: ['nodemon', 'watch'],
options: {
logConcurrentOutput: true
}
}
},
Back end changes are monitored by nodemon. If a file required by the server is changed, node
will be relaunched, restarting the server.In the
grunt.initConfig
block of Gruntfile.js
:nodemon: {
dev: {
options: {
file: 'server.js',
ignoredFiles: ['app', 'test', 'node_modules'],
watchedExtensions: ['js']
}
}
},
Front end changes are monitored by LiveReload. If a file required by the client is changed, assets will be rebuild (eg less
converted to css
) and the browser will be refreshedIn the
grunt.initConfig
block of Gruntfile.js
:watch: {
app: {
options: { livereload: true },
files: [
'app/index.*',
'app/css/*',
'app/components/**/*.js',
'app/images/*'
]
},
less: {
tasks: ['less'],
files: ['app/css/main.less', 'app/components/**/*.less']
}
},
less: {
build: {
files: { 'app/css/main.css': 'app/css/main.less' }
}
}
This article is inspired by Shawn Dahlen’s post on the build system 4dashes uses.
Subscribe to:
Posts (Atom)