Integer Emulation

This is a discussion on Integer Emulation within the C++ Programming forums, part of the General Programming Boards category; In response to cpjust and King Mir and my own style, I fiddled with creating this class in response to ...

  1. #1
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543

    Integer Emulation

    In response to cpjust and King Mir and my own style, I fiddled with creating this class in response to fulfilling everyone's needs.
    The goal was to get rid of get/set for every variable to make it easy to access and set and get, while still maintaining the advantages of a set/get.
    Hence, here is a integer/floating emulation class.
    All other operators are implemented, as well as a logging variable. I also created a thread-safe version (using Critical Sections) of the class.
    I imagine there are quite a few bugs left in the implementation, though. And besides from the missing operator, there is no logging (though it is supported).

    Now, those who wish can help to perfect this utility and all can be happy
    There are two files:
    CPPType.h -> contains the definition of the CCPPType (not thread safe) and CThreadCPPType (thread safe) class.
    CPPTypeImpl.h -> contains the actual implementation.
    Attached Files Attached Files
    Last edited by Elysia; 03-17-2008 at 05:11 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    I think you forgot about const-correctness and overloading operator>> and operator << for istream and ostream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    UPDATE:
    Added const correctness.
    Added filestream overloads.
    Made operator T virtual, so polymorphism is now possible (required for filestream operators). Perhaps all other operators should be made virtual, as well.
    Fixed bug in a CThreadClassType constructor (didn't initialize critical section).

    UPDATE2:
    Added 3 more operators.
    Added (hopefully) complete const correctness.
    Fixed a few bugs (compile warnings & errors).

    All tests now pass except for

    MyInt3 = MyInt2++;
    MyInt3 = MyInt2--;

    Where

    MyInt2 = CClassType<int>
    MyInt3 = CThreadClassType<int>

    They give unambiguous errors. Not sure what to do about this.
    Some more exotic syntax such as

    MyInt = MyInt + 100;

    May also fail with an unambiguous error.

    New files uploaded.
    Last edited by Elysia; 03-16-2008 at 06:47 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Bug here:
    Code:
    TT CThreadClassType<T>::CThreadClassType(CClassType& Data)
    {
    	InitializeCriticalSection(&m_Sync); // BUG!!!!
    	EnterCriticalSection(&m_Sync);
    	m_bLogging = Data.m_bLogging;
    	m_Data = Data.m_Data;
    	LeaveCriticalSection(&m_Sync);
    }
    You're missing unary operator +.

    I dislike your Lock and Lock2 macros.

    I would perfer the two parameter friend function version of operators like binary + so that both sides are implicitly convertable.

    If you want to prevent copying then your copy constructor and copy assignment operator need to take const references.

    Spelling mistake: MinEqualThenData.
    Actually, why do you give the parameter a different name each time? Why not just pick something like rhs and use that for all of them. Seems to me it would make your life a lot easier.

    Operator << should take Data as a const reference.

    #pragma once is less portable than propper include guards. Some use both.
    Last edited by iMalc; 03-16-2008 at 11:52 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by iMalc View Post
    Bug here:
    Code:
    TT CThreadClassType<T>::CThreadClassType(CClassType& Data)
    {
    	InitializeCriticalSection(&m_Sync); // BUG!!!!
    	EnterCriticalSection(&m_Sync);
    	m_bLogging = Data.m_bLogging;
    	m_Data = Data.m_Data;
    	LeaveCriticalSection(&m_Sync);
    }
    Bug? How do you figure? It's a constructor, so it needs to initialize the critical section.

    Quote Originally Posted by iMalc View Post
    I would perfer the two parameter friend function version of operators like binary + so that both sides are implicitly convertable.
    You mean such as
    Code:
    operator + (T, const CDataType<T>& )
    I think?

    Quote Originally Posted by iMalc View Post
    Spelling mistake: MinEqualThenData.
    Actually, why do you give the parameter a different name each time? Why not just pick something like rhs and use that for all of them. Seems to me it would make your life a lot easier.
    Actually, no, it's not a mistake. There is a word "then" and there is a word "than". If this equals that, then do this. If this is less than that, do this.
    Dunno. I just find making a little descriptive names better. Anyway, I changed them all to "Data" instead.

    Quote Originally Posted by iMalc View Post
    You're missing unary operator +.
    Quote Originally Posted by iMalc View Post
    If you want to prevent copying then your copy constructor and copy assignment operator need to take const references.
    Quote Originally Posted by iMalc View Post
    Operator << should take Data as a const reference.
    Missed those.

    Uploaded new files with more fixes.

    UPDATE: Proper include guard.
    Got rid of macros.

    UPDATE2:
    Re-ordered operators a little.
    Added global operators. All tests pass!
    Yes, even this:
    Code:
    MyInt = MyInt + MyInt - MyInt * MyInt / MyInt
    	&#37; MyInt & MyInt << MyInt >> MyInt ^ MyInt
    	| MyInt %= MyInt &= MyInt *= MyInt += MyInt 
    	-= MyInt /= MyInt <<= MyInt >>= MyInt ^= MyInt
    	|= MyInt;
    Last edited by Elysia; 03-17-2008 at 01:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    UPDATE2:
    Re-ordered operators a little.
    Added global operators. All tests pass!
    Yes, even this:
    Code:
    MyInt = MyInt + MyInt - MyInt * MyInt / MyInt
    	&#37; MyInt & MyInt << MyInt >> MyInt ^ MyInt
    	| MyInt %= MyInt &= MyInt *= MyInt += MyInt 
    	-= MyInt /= MyInt <<= MyInt >>= MyInt ^= MyInt
    	|= MyInt;
    How would you know?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    It compiles at least, which it should! I tried it.
    But there is more work to be done. I should template all the operators, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My point was more that the value is undefined in C or C++ for regular variables, so it's unlikely to expect a different implementation to be predictable. [As this is the same thing as the "i++ * i++ * i++" that we had a few days back - just because you use += or *= doesn't change it].

    It's good that it compiles of course, but it's kind of meaningless.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Absolutely. I don't think the value is defined, but it should compile, because otherwise it breaks code, and destroys the "emulation," which it shouldn't.
    It's not directly a "safe" class. You still have to have common knowledge and know the rules of the language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    This looks weird:
    Code:
    TT std::istream& operator >> (std::istream& Stream, const CClassType<T>& Data);
    Logically, reading into an object changes it, so it cannot be passed by const reference, thus it should be:
    Code:
    TT std::istream& operator >> (std::istream& Stream, CClassType<T>& Data);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    I don't think I tested that one. Assignment operator isn't const, so it should definitely give a compile error. I'll fix that.

    UPDATE:
    Templated all operators.
    Added more global operators.
    Renamed class to CCPPType and CThreadCPPType.
    I'm having trouble with unambiguous operators, however. It conflicts with the class and global operators for some and some between two global operators. But without all of them (if you comment out one), it will give other ambiguous errors. I'm not sure how to proceed.
    Last edited by Elysia; 03-17-2008 at 03:46 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Mind attaching your test suite?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Sure, these ones are causing problems atm:

    Code:
    	CThreadCPPType<int> MyInt;
    	CCPPType<int> MyInt2;
    	MyInt = 100;
    	MyInt = MyInt + 100;
    	MyInt = MyInt - 100;
    	MyInt = MyInt * 2;
    	MyInt = MyInt / 2;
    	MyInt -= 100;
    	MyInt += 100;
    	MyInt *= 100;
    	MyInt /= 100;
    	CThreadCPPType<int> MyInt3 = MyInt++;
    	CThreadCPPType<int> MyInt4 = ++MyInt;
    	MyInt3 = MyInt--;
    	MyInt4 = --MyInt;
    	MyInt = MyInt & 0xFFFF;
    	MyInt &= 0xFFF;
    	MyInt = MyInt | 0xFFFF;
    	MyInt |= 0xFFFFFFFF;
    	MyInt = MyInt ^ 0xFFFF;
    	MyInt ^= 0xF;
    	MyInt = -MyInt;
    	MyInt = !MyInt;
    	MyInt = MyInt % 100;
    	MyInt %= 2;
    	MyInt = MyInt >> 10;
    	MyInt = MyInt << 10;
    	MyInt >>= 2;
    	MyInt <<= 2;
    	MyInt = ~MyInt;
    	bool b1 = (MyInt == MyInt);
    	bool b2 = (MyInt != MyInt);
    	bool b3 = (MyInt > MyInt);
    	bool b4 = (MyInt < MyInt);
    	bool b5 = (MyInt <= MyInt);
    	bool b6 = (MyInt >= MyInt);
    	bool b7 = MyInt;
    	int n = MyInt;
    	int* p = &MyInt;
    	MyInt = +MyInt;
    	MyInt = MyInt + 100 / MyInt & MyInt + !MyInt;
    	MyInt += MyInt += MyInt += MyInt;
    	MyInt = MyInt + MyInt - MyInt * MyInt / MyInt % MyInt
    		& MyInt << MyInt >> MyInt ^ MyInt | MyInt 
    		%= MyInt &= MyInt *= MyInt += MyInt -= MyInt 
    		/= MyInt <<= MyInt >>= MyInt ^= MyInt |= MyInt;
    
    	MyInt2 = 100;
    	MyInt2 = MyInt2 + 100;
    	MyInt2 = MyInt2 - 100;
    	MyInt2 = MyInt2 * 2;
    	MyInt2 = MyInt2 / 2;
    	MyInt2 -= 100;
    	MyInt2 += 100;
    	MyInt2 *= 100;
    	MyInt2 /= 100;
    	MyInt3 = MyInt2++;
    	MyInt4 = ++MyInt2;
    	MyInt3 = MyInt2--;
    	MyInt4 = --MyInt2;
    	MyInt2 = MyInt2 & 0xFFFF;
    	MyInt2 &= 0xFFF;
    	MyInt2 = MyInt2 | 0xFFFF;
    	MyInt2 |= 0xFFFFFFFF;
    	MyInt2 = MyInt2 ^ 0xFFFF;
    	MyInt2 ^= 0xF;
    	MyInt2 = -MyInt2;
    	MyInt2 = !MyInt2;
    	MyInt2 = MyInt2 % 100;
    	MyInt2 %= 2;
    	MyInt2 = MyInt2 >> 10;
    	MyInt2 = MyInt2 << 10;
    	MyInt2 >>= 2;
    	MyInt2 <<= 2;
    	MyInt2 = ~MyInt2;
    	b1 = (MyInt2 == MyInt2);
    	b2 = (MyInt2 != MyInt2);
    	b3 = (MyInt2 > MyInt2);
    	b4 = (MyInt2 < MyInt2);
    	b5 = (MyInt2 <= MyInt2);
    	b6 = (MyInt2 >= MyInt2);
    	b7 = MyInt2;
    	n = MyInt2;
    	p = &MyInt2;
    	MyInt2 = +MyInt2;
    	MyInt2 = MyInt2 + MyInt2 - MyInt2 * MyInt2 / MyInt2 % MyInt2
    		& MyInt2 << MyInt2 >> MyInt2 ^ MyInt2 | MyInt2 %= 
    		MyInt2 &= MyInt2 *= MyInt2 += MyInt2 -= MyInt2 /= MyInt2 
    		<<= MyInt2 >>= MyInt2 ^= MyInt2 |= MyInt2;
    
    	CThreadCPPType<int> MyInt5 = MyInt2;
    	MyInt5 = MyInt2;
    
    	{
    		std::ofstream MyOut("C:\\test.txt");
    		MyOut << MyInt << MyInt2;
    	}
    	{
    		std::ifstream MyIn("C:\\test.txt");
    		MyIn >> MyInt >> MyInt2;
    	}
    Memmap can also be a trouble, the way it handles integers.
    Btw, I also typedef'd standard typedefs such as uint64_t to the CPPType class, so MemMap now uses the CPPType class.

    Code:
    // 7.18.1.1 Exact-width integer types
    typedef CCPPType<__int8>            int8_t;
    typedef CCPPType<__int16>           int16_t;
    typedef CCPPType<__int32>           int32_t;
    typedef CCPPType<__int64>           int64_t;
    typedef CCPPType<unsigned __int8>   uint8_t;
    typedef CCPPType<unsigned __int16>  uint16_t;
    typedef CCPPType<unsigned __int32>  uint32_t;
    typedef CCPPType<unsigned __int64>  uint64_t;
    
    typedef CThreadCPPType<__int8>            tint8_t;
    typedef CThreadCPPType<__int16>           tint16_t;
    typedef CThreadCPPType<__int32>           tint32_t;
    typedef CThreadCPPType<__int64>           tint64_t;
    typedef CThreadCPPType<unsigned __int8>   tuint8_t;
    typedef CThreadCPPType<unsigned __int16>  tuint16_t;
    typedef CThreadCPPType<unsigned __int32>  tuint32_t;
    typedef CThreadCPPType<unsigned __int64>  tuint64_t;
    Attached Files Attached Files
    Last edited by Elysia; 03-17-2008 at 03:56 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My immediate concern is that the tests only test that "it compiles". There is no (automated) test to verify correct calculation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    That's a given to add, I agree with that. Unfortunately, the project won't link at the moment, due to template mismatch. I never liked how you make templates. They're powerful, I love them, but they're far too advanced to implement.
    Figures. The compiler was screwing up. I did a rebuild and now I'm getting more errors. Yay
    UPDATE: Hey! It compiles. It links. No errors. No warnings! No ambiguous errors! Now just to implement the rest.
    Last edited by Elysia; 03-17-2008 at 04:26 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21