فهرست منبع

script to separate .h into .h and .cpp pair

Former-commit-id: 177a5398becd707f024da44c7f431bbf9bec95e7
jalec 13 سال پیش
والد
کامیت
cfb91ba04f
5فایلهای تغییر یافته به همراه100 افزوده شده و 13 حذف شده
  1. 0 8
      example_fun.cpp
  2. 1 5
      example_fun.h
  3. 88 0
      h2pair.sh
  4. 7 0
      igl_inline.h
  5. 4 0
      readme.txt

+ 0 - 8
example_fun.cpp

@@ -10,14 +10,6 @@ IGL_INLINE bool igl::example_fun(const Printable & input)
 }
 
 #ifndef IGL_HEADER_ONLY
-
 template bool igl::example_fun<double>(const double& input);
 template bool igl::example_fun<int>(const int& input);
-
-//// List all useful instanciations here
-//namespace igl_explicit_instanciations
-//{
-//  bool (*example_fun_A)(const double &) = &igl::example_fun<double>;
-//  bool (*example_fun_B)(const int &) = &igl::example_fun<int>;
-//}
 #endif

+ 1 - 5
example_fun.h

@@ -1,11 +1,7 @@
 #ifndef IGL_EXAMPLE_FUN_H
 #define IGL_EXAMPLE_FUN_H
 
-#ifdef IGL_HEADER_ONLY
-#  define IGL_INLINE inline
-#else
-#  define IGL_INLINE
-#endif
+#include "igl_inline.h"
 
 namespace igl
 {

+ 88 - 0
h2pair.sh

@@ -0,0 +1,88 @@
+#!/bin/bash
+
+# http://tipstricks.itmatrix.eu/?p=305
+tac () {
+awk '1 { last = NR; line[last] = $0; } END { for (i = last; i > 0; i--) { print line[i]; } }'
+}
+
+function replace_inline
+{
+  result="$1"
+  # replace inlines with IGL_INLINEs, first those that begin lines
+  result=`echo "$result" | sed -e 's/^inline/IGL_INLINE/g'`
+  # then also those that begin words
+  result=`echo "$result" | sed -e 's/ inline/ IGL_INLINE/g'`
+  echo "$result"
+}
+
+# Convert well organized .h file to a .h/.cpp pair
+for i in $*
+do
+  # Only operate on .h files
+  filename=$(basename $i)
+  extension=${filename##*.}
+  filename=${filename%.*}
+  FILENAME=`echo $filename | tr '[a-z]' '[A-Z]'`
+  if [ ! "$extension" = "h" ];
+  then
+    echo "Skipping $i because it is not a .h file"
+    continue;
+  fi
+
+  if ! grep -q "^\/\/ Implementation$" "$i"
+  then
+    echo "Skipping $i because it does not match ^\/\/ Implementation$ "
+    continue;
+  fi
+
+  if [[ `grep -c "^\#endif" "$i"` > 1 ]];
+  then
+    echo "Warning $i contains multple matches to ^#endif"
+  fi
+
+  before=`sed -n '/^\/\/ Implementation$/q;p' "$i"`;
+
+  if ! echo "$before" | grep -q "^\#ifndef IGL_${FILENAME}_H$"
+  then
+    echo "Skipping $i because it does not match ^#ifndef IGL_${FILENAME}_H$ "
+    continue;
+  fi
+
+  if ! echo "$before" | grep -q "^\#define IGL_${FILENAME}_H$"
+  then
+    echo "Skipping $i because it does not match ^#define IGL_${FILENAME}_H$ "
+    continue;
+  fi
+
+  before=`replace_inline "$before"`
+  # prepend #include "igl_inline.h"
+  before=`echo "$before" | sed -e 's/^\(#define IGL_'${FILENAME}'_H\)/\1\'$'\n''#include \"igl_inline.h\"'/`
+  after=`sed '1,/^\/\/ Implementation$/d' $i`;
+  after=`replace_inline "$after"`
+  # reverse file
+  after=`echo "$after" | tac`
+  # everything after first (last) endif 
+  after=`echo "
+$after" | sed '1,/endif/d'`;
+  # reverse file
+  after=`echo "$after" | tac`
+  # append empty template code
+  if grep -q "template" "$i"
+  then
+    after=`echo "$after
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template specialization
+#endif"` 
+  fi
+  echo "$before
+
+#ifdef IGL_HEADER_ONLY
+#  include \"$filename.cpp\"
+#endif
+
+#endif">before-"$filename".h
+  echo "#include \"$filename.h\"
+
+$after">after-"$filename".cpp
+done

+ 7 - 0
igl_inline.h

@@ -0,0 +1,7 @@
+// This should *NOT* be contained in a IGL_*_H ifdef, since it may be defined
+// differently based on when it is included
+#ifdef IGL_HEADER_ONLY
+#  define IGL_INLINE inline
+#else
+#  define IGL_INLINE
+#endif

+ 4 - 0
readme.txt

@@ -113,4 +113,8 @@ description of each template, input and output in each prototype.
 
     grep -L "^#ifndef IGL_" *
 
+  Find .cpp files that contain ^using namespace
+
+    grep -l "^using namespace" *.cpp
+