Browse Source

Adding in a gitignore and clang-format

Sam Jaffe 5 years ago
parent
commit
6da1ebd6bf
6 changed files with 215 additions and 97 deletions
  1. 108 0
      .clang-format
  2. 40 0
      .gitignore
  3. 60 86
      include/variant/variant.hpp
  4. 1 3
      test/gtest_variant_matchers.h
  5. 5 7
      test/gtest_variant_printers.h
  6. 1 1
      test/variant_test.cxx

+ 108 - 0
.clang-format

@@ -0,0 +1,108 @@
+---
+Language:        Cpp
+# BasedOnStyle:  LLVM
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Right
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: true
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: true
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: false
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:   
+  AfterClass:      false
+  AfterControlStatement: false
+  AfterEnum:       false
+  AfterFunction:   false
+  AfterNamespace:  false
+  AfterObjCDeclaration: false
+  AfterStruct:     false
+  AfterUnion:      false
+  BeforeCatch:     false
+  BeforeElse:      false
+  IndentBraces:    false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: true
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Attach
+BreakBeforeInheritanceComma: false
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeColon
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit:     80
+CommentPragmas:  '^ IWYU pragma:'
+CompactNamespaces: true
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: false
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+ForEachMacros:   
+  - foreach
+  - Q_FOREACH
+  - BOOST_FOREACH
+IncludeCategories: 
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
+    Priority:        3
+  - Regex:           '.*'
+    Priority:        1
+IncludeIsMainRegex: '(Test)?$'
+IndentCaseLabels: false
+IndentWidth:     2
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: All
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 60
+PointerAlignment: Middle
+ReflowComments:  true
+SortIncludes:    true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeParens: ControlStatements
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard:        Cpp11
+TabWidth:        8
+UseTab:          Never
+...
+

+ 40 - 0
.gitignore

@@ -0,0 +1,40 @@
+# C++.gitignore
+# Prerequisites
+*.d
+
+# Compiled Object files
+*.slo
+*.lo
+*.o
+*.obj
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Compiled Dynamic libraries
+*.so
+*.dylib
+*.dll
+
+# Fortran module files
+*.mod
+*.smod
+
+# Compiled Static libraries
+*.lai
+*.la
+*.a
+*.lib
+
+# Executables
+*.exe
+*.out
+*.app
+
+# Gcov.gitignore
+# gcc coverage testing tool files
+
+*.gcno
+*.gcda
+*.gcov

+ 60 - 86
include/variant/variant.hpp

@@ -11,191 +11,165 @@
 #pragma once
 
 #include <cstdlib>
-#include <utility>
 #include <memory>
+#include <utility>
 
 // Max of a list of values
 template <size_t T, size_t... Ts> struct static_max;
 
-template <size_t T>
-struct static_max<T> {
+template <size_t T> struct static_max<T> {
   static const constexpr size_t value = T;
 };
 
-template <size_t T1, size_t T2, size_t... Ts>
-struct static_max<T1, T2, Ts...> {
-  static const constexpr size_t value = T1 > T2 ? static_max<T1, Ts...>::value : static_max<T2, Ts...>::value;
+template <size_t T1, size_t T2, size_t... Ts> struct static_max<T1, T2, Ts...> {
+  static const constexpr size_t value =
+      T1 > T2 ? static_max<T1, Ts...>::value : static_max<T2, Ts...>::value;
 };
 
 // Type index in a list
 template <typename F, typename... Ts> struct type_index;
 
-template <typename F>
-struct type_index<F> {};
+template <typename F> struct type_index<F> {};
 
 template <typename F, typename T, typename... Ts>
 struct type_index<F, T, Ts...> {
   static const constexpr size_t value = type_index<F, Ts...>::value;
 };
 
-template <typename F, typename... Ts>
-struct type_index<F, F, Ts...> {
+template <typename F, typename... Ts> struct type_index<F, F, Ts...> {
   static const constexpr size_t value = sizeof...(Ts);
 };
 
 template <typename... Ts> struct variant_helper;
 
-template<typename F, typename... Ts>
-struct variant_helper<F, Ts...> {
-  inline static void destroy(size_t id, void * data)
-  {
+template <typename F, typename... Ts> struct variant_helper<F, Ts...> {
+  inline static void destroy(size_t id, void * data) {
     if (id == sizeof...(Ts))
-      reinterpret_cast<F*>(data)->~F();
+      reinterpret_cast<F *>(data)->~F();
     else
       variant_helper<Ts...>::destroy(id, data);
   }
-  
-  inline static void move(size_t old_t, void * old_v, void * new_v)
-  {
+
+  inline static void move(size_t old_t, void * old_v, void * new_v) {
     if (old_t == sizeof...(Ts))
-      new (new_v) F(std::move(*reinterpret_cast<F*>(old_v)));
+      new (new_v) F(std::move(*reinterpret_cast<F *>(old_v)));
     else
       variant_helper<Ts...>::move(old_t, old_v, new_v);
   }
-  
-  inline static void copy(size_t old_t, const void * old_v, void * new_v)
-  {
+
+  inline static void copy(size_t old_t, const void * old_v, void * new_v) {
     if (old_t == sizeof...(Ts))
-      new (new_v) F(*reinterpret_cast<const F*>(old_v));
+      new (new_v) F(*reinterpret_cast<const F *>(old_v));
     else
       variant_helper<Ts...>::copy(old_t, old_v, new_v);
-  }   
+  }
 };
 
-template<> struct variant_helper<>  {
-  inline static void destroy(size_t, void *) { }
-  inline static void move(size_t, void *, void *) { }
-  inline static void copy(size_t, const void *, void *) { }
+template <> struct variant_helper<> {
+  inline static void destroy(size_t, void *) {}
+  inline static void move(size_t, void *, void *) {}
+  inline static void copy(size_t, const void *, void *) {}
 };
 
-template <typename... Ts>
-struct variant {
+template <typename... Ts> struct variant {
 private:
   static const constexpr size_t num_types = sizeof...(Ts);
   static const constexpr size_t data_size = static_max<sizeof(Ts)...>::value;
   static const constexpr size_t data_align = static_max<alignof(Ts)...>::value;
-  
+
   using data_t = typename std::aligned_storage<data_size, data_align>::type;
-  
+
   using helper_t = variant_helper<Ts...>;
-  
-  static inline size_t invalid_type() {
-    return 0xFFFFFFFF;
-  }
-  
-  template <typename T>
-  static inline size_t get_type_index() {
+
+  static inline size_t invalid_type() { return 0xFFFFFFFF; }
+
+  template <typename T> static inline size_t get_type_index() {
     return num_types - type_index<T, Ts...>::value - 1;
   }
-  
+
   size_t type_id;
   data_t data;
+
 public:
   template <size_t I>
   using at = typename std::tuple_element<I, std::tuple<Ts...>>::type;
-  
-  variant() : type_id(invalid_type()), data() {   }
-  
+
+  variant() : type_id(invalid_type()), data() {}
+
   template <typename T>
   variant(T const & value) : type_id(invalid_type()), data() {
     set<T>(value);
   }
-  
-  variant(const variant<Ts...>& old) : type_id(old.type_id), data() {
+
+  variant(const variant<Ts...> & old) : type_id(old.type_id), data() {
     helper_t::copy(num_types - type_id - 1, &old.data, &data);
   }
-  
-  variant(variant&& old) : type_id(old.type_id), data() {
+
+  variant(variant && old) : type_id(old.type_id), data() {
     helper_t::move(num_types - type_id - 1, &old.data, &data);
     old.type_id = invalid_type();
   }
-  
-  variant& operator=(variant const & old) {
+
+  variant & operator=(variant const & old) {
     helper_t::destroy(num_types - type_id - 1, &data);
     type_id = old.type_id;
     helper_t::copy(num_types - type_id - 1, &old.data, &data);
     return *this;
   }
 
-  variant& operator=(variant&& old) {
+  variant & operator=(variant && old) {
     helper_t::destroy(num_types - type_id - 1, &data);
     type_id = old.type_id;
     helper_t::move(num_types - type_id - 1, &old.data, &data);
     old.type_id = invalid_type();
     return *this;
   }
-  
-  template<typename T>
-  inline bool is() const {
+
+  template <typename T> inline bool is() const {
     return (type_id == get_type_index<T>());
   }
-  
-  inline bool valid() const {
-    return (type_id != invalid_type());
-  }
-  
-  template<typename T, typename... Args>
-  void set(Args&&... args)
-  {
+
+  inline bool valid() const { return (type_id != invalid_type()); }
+
+  template <typename T, typename... Args> void set(Args &&... args) {
     // First we destroy the current contents
     helper_t::destroy(num_types - type_id - 1, &data);
     new (&data) T(std::forward<Args>(args)...);
     type_id = get_type_index<T>();
   }
-  
-  template<typename T>
-  T& get()
-  {
+
+  template <typename T> T & get() {
     // It is a dynamic_cast-like behaviour
     if (is<T>())
-      return *reinterpret_cast<T*>(&data);
+      return *reinterpret_cast<T *>(&data);
     else
       throw std::bad_cast();
   }
-  
-  template<typename T>
-  T const& get() const
-  {
+
+  template <typename T> T const & get() const {
     // It is a dynamic_cast-like behaviour
     if (is<T>())
-      return *reinterpret_cast<T const*>(&data);
+      return *reinterpret_cast<T const *>(&data);
     else
       throw std::bad_cast();
   }
-  
+
   size_t index() const { return type_id; }
-  
-  template <size_t I>
-  auto get() const -> at<I> const & {
-    return get<at<I>>();
-  }
 
-  template <size_t I>
-  auto get() -> at<I> & {
-    return get<at<I>>();
-  }
+  template <size_t I> auto get() const -> at<I> const & { return get<at<I>>(); }
 
-  ~variant() {
-    helper_t::destroy(num_types - type_id - 1, &data);
-  }
+  template <size_t I> auto get() -> at<I> & { return get<at<I>>(); }
+
+  ~variant() { helper_t::destroy(num_types - type_id - 1, &data); }
 };
 
 namespace std {
   template <size_t I, typename... Ts>
-  auto get(variant<Ts...> const & var) -> typename variant<Ts...>::template at<I> const & {
+  auto get(variant<Ts...> const & var) ->
+      typename variant<Ts...>::template at<I> const & {
     return var.template get<I>();
   }
-  
+
   template <size_t I, typename... Ts>
   auto get(variant<Ts...> & var) -> typename variant<Ts...>::template at<I> & {
     return var.template get<I>();

+ 1 - 3
test/gtest_variant_matchers.h

@@ -10,9 +10,7 @@
 
 #include <gmock/gmock.h>
 
-MATCHER(IsValid, "") {
-  return arg.valid();
-}
+MATCHER(IsValid, "") { return arg.valid(); }
 
 MATCHER_P(IsStoringType, type_instance, "") {
   return arg.template is<decltype(type_instance)>();

+ 5 - 7
test/gtest_variant_printers.h

@@ -21,20 +21,18 @@ std::ostream & operator<<(std::ostream & os, std::map<K, V> const & value) {
   return os << " }";
 }
 
-template <std::size_t I, typename ...Ts>
+template <std::size_t I, typename... Ts>
 void PrintTo(variant<Ts...> const & value, std::ostream * os) {
-  if (value.index() == I) {
-    (*os) << value.template get<I>();
-  }
+  if (value.index() == I) { (*os) << value.template get<I>(); }
 }
 
-template <typename ...Ts, std::size_t ...Is>
+template <typename... Ts, std::size_t... Is>
 void PrintTo(variant<Ts...> const & value, std::ostream * os,
              std::index_sequence<Is...>) {
-  [[maybe_unused]] auto l = { (PrintTo<Is>(value, os), 0)... };
+  [[maybe_unused]] auto l = {(PrintTo<Is>(value, os), 0)...};
 }
 
-template <typename ...Ts>
+template <typename... Ts>
 void PrintTo(variant<Ts...> const & value, std::ostream * os) {
   if (value.valid()) {
     PrintTo(value, os, std::make_index_sequence<sizeof...(Ts)>());

+ 1 - 1
test/variant_test.cxx

@@ -14,8 +14,8 @@
 
 #include <gmock/gmock.h>
 
-#include "gtest_variant_printers.h"
 #include "gtest_variant_matchers.h"
+#include "gtest_variant_printers.h"
 
 using test_variant = variant<int, bool, std::string, std::map<int, int>>;