Visual C++ and Sorting std::list

Today I just feel like bloging (I think this is my 3rd today) so I feel lucky just see I had 2 comments on my blog this is best for today, so I’ll keep writing cause I think this is working (Donot try to disappoint me just if these helps post me comment :D)!!!
 
so If you look at my last blog in this Catagory youill see some Utils/ Wrapper on "std::list , std::hash_map , std::pair , std::string" some of them of slower than the MFC counterparts and some of them are lot faster (String is 1500% faster WOW! but List is slower but I like it!) but by being faster or slower I mean about 1000000 time operation which you never make :D.
 
Anyway today I’m gonna show you 2 new Utils:
 
1) First the way to sort the List of Referenece types like: List<int *>
 
This is the way:
 
    template<class Type>
    struct ReferenceTypeLess
      : public std::less<Type>
    {
      virtual bool operator()(const Type _Left, const Type _Right) const
      {
        return *_Left < *_Right;
      }
    };
 
just use it in this way:
 
  List<int *> listOfInts;
  … // (fill it here)
  listOfInts.sort(ReferenceTypeLess<int *>());
 
It will also work for References to class/ struct like: List<MyClass *> , just remember to override the "operator <" in the "MyClass".
 
 
2) The second is nothing special (DO you think all the rest was special?? :D) but helped me, hope help you to:
 
    //—————————————————————————
    template <class ValueType1, class ValueType2 = ValueType1, class ValueType3 = ValueType1, class ValueType4 = ValueType1>
    struct Rectangle
    {
      ValueType1 top;
      ValueType2 begin;
      ValueType3 bottom;
      ValueType4 end;
      Rectangle() { }
      Rectangle(ValueType1 top, ValueType2 begin, ValueType3 bottom, ValueType4 end) : top(top), begin(begin), bottom(bottom), end(end) { }
    };
    //—————————————————————————
    template <class ValueType>
    struct SymmetricalRectangle
      : public Rectangle<ValueType, ValueType, ValueType, ValueType>
    {
      SymmetricalRectangle() { }
      SymmetricalRectangle(ValueType top, ValueType begin, ValueType bottom, ValueType end) : Rectangle(top, begin, bottom, end) { }
      SymmetricalRectangle(ValueType value)
      {
        SetAll(value);
      }
      void SetAll(ValueType value)
      {
        top = value;
        begin = value;
        bottom = value;
        end = value;
      }
    };
    //—————————————————————————
    struct FloatRectangle
      : public SymmetricalRectangle<float>
    {
    public:
      FloatRectangle() : SymmetricalRectangle(0.0f) { }
      float Width() { return end – begin; }
      float Height() { return bottom – top; }
    };
    //—————————————————————————
    struct IntRectangle
      : public SymmetricalRectangle<int>
    {
    public:
      IntRectangle() : SymmetricalRectangle(0) { }
      int Width() { return end – begin; }
      int Height() { return bottom – top; }
    };
    //—————————————————————————
Those were Rectangle of everything.
 
 
Hope these will help,
 
Sadjad Bahmanpour
 

Microsoft ForeFront Client Security

Back in a week I’ve downloaded the "Microsoft ForeFront Client Security RTM Evaluation Edition" I tired to install the Client using:
 "<CD Drive>:Clientclientsetup.exe /nonmom" it installed and also gets updates from WSUS 3.0 which I installed lately.
 
Everything is all right after a week and there is no Evaluation like thing in this part of this great product so I tried to spread it to all of my computers so I’ve tried:
 
Windows Server 2003 Enterprise: OK.
Windows XP Professional: OK.
Windows Vista Ultimate Edition: OK.
Windows Server 2008 Enterprise Edition (32-bits): OK.
 
WOW! The best part is that in Windows Vista and also Windows Server 2008 it will disable the Windows Defender and make itself the DEFENDER of Windows for Malwares, Viruses, Trojans, … so "Windows Security Center" will be satisfied.
 
May be you will try this, and let me know if you see any evlatuation in CLIENT part (Cause the server part is very large for me, I’ve installed the Public Beta, and it was too LARGE for my organization!!)
 
Sadjad Bahmanpour.

Windows Server 2008 (Longhorn Server) Beta 3

I downloaded the Enterprise Edition of Windows Server 2008 Beta 3 and installed it back in 4-5 days, It is good, but it unlike the Windows Server 2003 it consume 500+ MB of my memory which I do not know why!!!!
 
After some searches in this great product, I found out that there is feature that you can enable named "Windows Desktop Experience" which will make it just like Windows Vista, I’ve mentioned it also install Windows SideBar or Windows SideShow (WOW!) The only thing I left behind is Windows Media Center which I think can be copied from Vista Ultimate, And you Server 2008 would be greatest for Developement (I would never use Vista or XP as my devepolement envirounment, never!!!!!).
 
I’ve installed the lastest Drive From NVidia and Realtek and Also Marvell, all of them was drivers made for Vista and none of had problem, I also tried to install "Microsoft ForeFront Client Security" on it and there was no problem (I’am going the write blog about this!).
 
The only problem of "Windows Server 2008 Beta 3" is that the "Windows Photo Galery" will hang every time you open it!!!
 
Sadjad.

Windows Sharepoint Sevices 2.0 Bug

Today I tried to change the Project Portal Page on my TFS which is based on WSS 2.0 SP2, after deleting some Web Parts I tried to add them again but it redirect me to the ErrorPage.aspx with an exception like this "Invalid postback or callback argument.", I can add web parts to newly created pages but not the main pages which is unghosted page (Modified or installed from a Template), so I searched the web and I found 3 workarounds:
 
1) Contact the Microsoft Support by Calling them and ask for Post SP2 hotfix mentioned in http://support.microsoft.com/kb/915746 , which I donot want
 
2) Wait for SP3 of the WSS 2.0 , which I never wait!!!
 
3) The workaroung which worked for me (and mostly invented by me :D), is just set the ASP.NET 1.1 for all the WSS Virtual Directory, and the Default Web Site (Port 80), but then set the Reports Virtual Directories (Reports & ReportServer) back to ASP.NET 2.0, and run:
 
stsadm.exe -o upgrade -forceupgrade -url http://YOURSERVER
which located under: "C:Program FilesCommon FilesMicrosoft Sharedweb server extensions60BIN"
 
Then Restart the IIS, and everything must be as great as it was.
 
Sadjad Bahmanpour

Internet Explorer Enhanced Security Configuration

Tonight after 2 days I’ve installed the "Visual Studio 2005 – Team Foundation Server" (TFS), and now I love microsoft more and more, this is the best CASE TOOLS I’ve ever seen and worked with (believe me I’ve worked with most of them, I was in a team name "HEROES" working on a great CASE TOOLS ever made, the project never came out comercially but I’ve learn alot in that year and half).
 
But to install the Team Explorer first you need to disable "Internet Explorer Enhanced Security" which placed under "Add/Remove Programs -> Windows Components -> Internet Explorer Enhanced Security Configuration", and after that everything would be as great as a DREAM for a programmer (I have a Visual C++ Solution which has 14 Projects WOW in it, and it worked like a charm for me!!!!).
 
So after that I’ve experienced alot changes in IE 7.0 (under Windows 2003 Enterprise Edition):
 
1) The Internet Explorer download page faster (or I donot know may be just render page faster) (I mean something faster that you cannot imagine).
 
2) Work faster with AJAX based sites like live.com , SO MUCH FASTER, as I’ve thought the internet I’ve used is upgraded to something else
 
3) Now the "Windows Live Sign-in Assistant" works, I’ve always thought that this problem of Windows 2003 but now I know how to solve this!!!
the problem was I always see the last signed in account, just like I never installed the component via Windows Live Messenger.
 
If you are using Windows Server 2003 I recomend you to just try this tweak and see what will happend next.
 
I’ve always thought how IE 7.0 works faster under Windows Vista, or why it is slower under Windows 2003, now I know the answer.
 
The only thing that still amazed me in Windows Vista is the speed of network; If I found something I’ll let you all know about it.
 
Sadjad.

Visual C++ and Hashmap

This is my first entry on "Programming Tips" which I think there will be more on this, cause I think I more expert on this than anything else in computer science.
 
Anyway, today I tired to use some of MFC classes to build some Hashtable & List & Pair just equal to the C#. I has lots of problem which MFX so I tried to use the STD counterparts. The most problemy thing about Hashmap (std::hash_map) is that you cannot have a hash_map<CString> I mean having a Hashmap/ hashtable which key is CString, so this is how I wrote it (It is not all by me!):
 
    //—————————————————————————
    template <class KEY, class ARG_KEY = KEY, class TRAITS = std::less<KEY> >
    class HashCompare
      : public stdext::hash_compare<KEY, TRAITS>
    {
    public:
      using stdext::hash_compare<KEY, TRAITS>::operator();
      size_t operator()(const KEY& Key) const
      {
        return static_cast<size_t>(HashKey<ARG_KEY>(Key));
      }
    };
    //—————————————————————————
    template <class KEY, class VALUE, class HashCompareARG_KEY = KEY>
    class HashMap
      : public stdext::hash_map<KEY, VALUE, HashCompare<KEY, HashCompareARG_KEY> >
    {
    public:
      HashMap() : hash_map() { }
      virtual bool Add(KEY key, VALUE value)
      {
        std::pair<iterator, bool> pr;
        pr = insert(value_type(key, value));
        return pr.second;
      }
       // Be aware of that you have to delete the replaced value by yourself
      virtual bool Replace(KEY key, VALUE newValue)
      {
        iterator iter = find(key);
        if (iter != end()) // key found
        {
          iter->second = newValue;
          return true;
        }
        else
          return false;
      }
      virtual value_type *Find(KEY key)
      {
        iterator iter = find(key);
        if (iter != end()) // key found
          return &(*iter);
        else
          return NULL;
      }
    };
    //—————————————————————————
    template <class VALUE>
    class StringHashMap
      : public HashMap<CString, VALUE, LPCTSTR>
    {
    public:
      StringHashMap() : HashMap() { }
      virtual value_type *FindCaseInsensitive(CString key)
      {
        iterator iter = begin();
        while (iter != end())
        {
          if (!iter->first.CompareNoCase(key))
            return &(*iter);
          iter++;
        }
        return NULL;
      }
    };
    //—————————————————————————
    typedef StringHashMap<CString> StringStringHashMap;
    //—————————————————————————
After that I create the my own list which is something betwean std:list and CList:
 
    //—————————————————————————
    template<class _Ty, class FindKey = _Ty, class _Ax = std::allocator<_Ty>>
    class List
      : public std::list<_Ty, _Ax>
    {
    public:
      List() : list() { }
      virtual _Ty *Find(FindKey findKey)
      {
        for (iterator iter = begin(); iter != end(); iter++)
          if ((*iter) == findKey)
            return &(*iter);
        return NULL;
      }
      virtual _Ty *operator[](const unsigned int& index)
      {
        if (index < size())
        {
          int cnt = 0;
          for (iterator iter = begin(); iter != end(); iter++, cnt++)
            if (cnt == index)
              return &(*iter);
        }
        return NULL;
      }
      virtual iterator Add(const _Ty &element)
      {
        push_back(element);
        return –end();
      }
      virtual iterator Add(const List<_Ty, FindKey, _Ax> &list)
      {
        insert(end(), list.begin(), list.end());
        return –end();
      }
      virtual bool Remove(FindKey findKey, bool removeAllInstances = false)
      {
        bool retVal = false;
        iterator iterEnd = end();
        for (iterator iter = begin(); iter != iterEnd; iter++)
          if ((*iter) == findKey)
          {
            erase(iter);
            retVal = true;
            if (!removeAllInstances)
              break;
          }
        return retVal;
      }
    };
    //—————————————————————————
    template<class _Ty, class FindKey = _Ty, class _Ax = std::allocator<_Ty>>
    class SortedList
      : public List<_Ty, FindKey, _Ax>
    {
    public:
      iterator Add(const _Ty &element) override
      {
        iterator iterEnd = end();
        for (iterator iter = begin(); iter != iterEnd; iter++)
          if ((*iter) < element)
            return insert(iter, element);
        return List::Add(element);
      }
    };
    //—————————————————————————
    class StringList
      : public List<CString>
    {
    public:
      StringList() : List() { }
      virtual CString *FindCaseInsensitive(CString findKey)
      {
        iterator iterEnd = end();
        for (iterator iter = begin(); iter != iterEnd; iter++)
          if ((*iter).CompareNoCase(findKey))
            return &(*iter);
        return NULL;
      }
    };
    //—————————————————————————
    class SortedStringList
      : public SortedList<CString>
    {
    public:
      SortedStringList() : SortedList() { }
      virtual CString *FindCaseInsensitive(CString findKey)
      {
        iterator iterEnd = end();
        for (iterator iter = begin(); iter != iterEnd; iter++)
          if ((*iter).CompareNoCase(findKey))
            return &(*iter);
        return NULL;
      }
    };
    //—————————————————————————
 
and then Pair:
 
    //—————————————————————————
    template<class Type1, class Type2>
    class Pair
      : public std::pair<Type1, Type2>
    {
    public:
     Pair() : pair() { }
      Pair(const Type1& _Val1, const Type2& _Val2) : pair(_Val1, _Val2) { }
      virtual bool operator==(const Type1 &other) const
      {
        return (first == other);
      }
      virtual bool operator < (const Type1& other) const
      {
        return (first < other);
      }
    };
    //—————————————————————————
    class StringPair
      : public Pair<CString, CString>
    {
    public:
      StringPair() : Pair() { }
      StringPair(CString first, CString second) : Pair(first, second) { }
      virtual int CompareNoCase(CString psz)
      {
        return first.CompareNoCase(psz);
      }
    };
    //—————————————————————————
and the Pair list:
 
    //—————————————————————————
    class StringPairList
      : public List<StringPair, CString>
    {
    public:
      StringPairList() : List() { }
      virtual StringPair *FindCaseInsensitive(CString findKey)
      {
        iterator iterEnd = end();
        for (iterator iter = begin(); iter != iterEnd; iter++)
          if ((*iter).CompareNoCase(findKey))
            return &(*iter);
        return NULL;
      }
    };
    //—————————————————————————
 
 
with some more attention you’ll find out how to make a struct in the list to be sorted on one key (That’s way I use the findkey).
 
And the last but not the least my best String (which is 1500% faster than CString):
 
    //—————————————————————————
    class String
//      : public std::basic_string<wchar_t>
      : public std::basic_string<char>
    {
    protected:
      enum { _MAX_CHARS = 256 };
      value_type buffer[_MAX_CHARS + 1];
    public:
      String() : basic_string() { }
     
      String(const String& arg) : basic_string(arg) { }
      String(const value_type arg) : basic_string() { (*this) = arg;  }
      String(const value_type* arg) : basic_string(arg) { }
/*     String& operator=(const String& arg)     { assign(arg.c_str()); return (*this); }
      String& operator=(const value_type pArg) { assign(pArg); return (*this); }
      String& operator=(const value_type* pArg) { assign(pArg); return (*this); }*/
      //—————————————————————————
      virtual bool operator==(const String &other)     { return (this->compare(other) == 0); }
      virtual bool operator==(const value_type *other) { return (this->compare(other) == 0); }
      virtual bool operator> (const String &other)     { return (this->compare(other) >  0); }
      virtual bool operator> (const value_type *other) { return (this->compare(other) >  0); }
      virtual bool operator>=(const String &other)     { return (this->compare(other) >= 0); }
      virtual bool operator>=(const value_type *other) { return (this->compare(other) >= 0); }
      virtual bool operator< (const String &other)     { return (this->compare(other) <  0); }
      virtual bool operator< (const value_type *other) { return (this->compare(other) <  0); }
      virtual bool operator<=(const String &other)     { return (this->compare(other) <= 0); }
      virtual bool operator<=(const value_type *other) { return (this->compare(other) <= 0); }
      virtual operator const value_type *()            { return c_str(); }
      virtual String &operator+=(const String &other)     { append(other); return (*this); }
      virtual String &operator+=(const value_type other) { append(1, other); return (*this); }
      virtual String &operator+=(const value_type *other) { append(other); return (*this); }
      virtual String &operator+=(const int &other)        { AppendFormat("%d", other); return (*this); }
      //—————————————————————————
      virtual void ToUpper()                        { transform(begin(), end(), begin(), toupper); }
     virtual void ToLower()                        { transform(begin(), end(), begin(), tolower); }
      virtual bool IsEmpty() { return (this->compare(_T("")) == 0); }
      virtual void Format(const value_type *szFormat, …)
     {
      va_list argList;
      va_start(argList, szFormat); // Initialize variable arguments.
//        int ret = _vsnwprintf_s(buffer, _MAX_CHARS, _MAX_CHARS, szFormat, argList);
      int ret = _vsnprintf_s(buffer, _MAX_CHARS, _MAX_CHARS, szFormat, argList);
        if (ret > 0)
        assign(buffer, ret);
      va_end(argList); // Reset variable arguments.
     }
      //—————————————————————————
      virtual void AppendFormat(const value_type* szFormat, …)
     {
      va_list argList;
      va_start(argList, szFormat); // Initialize variable arguments.
//      int ret = _vsnwprintf_s(buffer, _MAX_CHARS, _MAX_CHARS, szFormat, argList);
      int ret = _vsnprintf_s(buffer, _MAX_CHARS, _MAX_CHARS, szFormat, argList);
        if (ret > 0)
          append(buffer, ret);
        va_end(argList); // Reset variable arguments.
     }
      //—————————————————————————
    };
    //—————————————————————————
    inline String operator+(const String& s1, const String& s2)
    {
      String retVal(s1);
      retVal += s2;
      return retVal;
    }
    inline String operator+(const String& s1, const String::value_type s2)
    {
      String retVal(s1);
      retVal += s2;
      return retVal;
    }
    inline String operator+(const String& s1, const String::value_type *s2)
    {
      String retVal(s1);
      retVal += s2;
      return retVal;
    }
    inline String operator+(const String::value_type s1, const String& s2)
    {
      String retVal(s1);
      retVal += s2;
      return retVal;
    }
    inline String operator+(const String::value_type *s1, const String& s2)
    {
      String retVal(s1);
      retVal += s2;
      return retVal;
    }
    //—————————————————————————
 
Hope these are will help,
just leave a message if you need help on those,
 
Sadjad Bahmanpour

Team Foundation Server and IIS problem

Last night I to upgrade my WSUS (Windows Server Update Services) from 2.0 into WSUS 3.0, and everything was right and I tried to manage it from my cliend computer via supplied console (At frist I hate microsoft for switching everything into MMC but after some hours of teseting the new console which is based on MMC 3.0, now I love microsoft again, cause it is so fast) and everthing was good too.
 
So I tried to install the Visual Studio 2005 – Team Foundation Server (VSTFS/ TFS 2005) and then the SP1 and everything was good, so after that I tried to connect to the WSUS 3.0 and I gets errors and there was lots of erros in event viewer (I just remember one of them "Self-update is not working" Event ID: 13031 Source: Windows Server Update, there was some error about "Unsupported format request" on all of the web services used by WSUS), after an hour of research 😀 (At last I just uninstall the TFS) I just found out that the TFS set the IIS to just respond to the 127.0.0.1 and I just do not know WHY?????
 
So today I’m going to reinstall the TFS and see what I can do to have them both on a single server.
 
Sadjad